从 AutoCompleteExtender 调用启用 Ajax 的 WCF 应用程序

发布于 2024-08-18 09:21:26 字数 2586 浏览 4 评论 0原文

我正在尝试使用 WCF 服务来完成 MS Ajax AutoCompleteExtender 完成列表。我尝试了两种选择。如果我在网站项目中添加 WCF 服务,AutoCompleteExtender 会通过 POST 调用它并且工作正常。

然后我决定创建一个单独的 WCF 应用程序,并将支持 AJAX 的 WCF 服务添加到新应用程序中。我还复制了我网站的 Web.config 中有关 servicemodel 的部分内容。但这不起作用!首先,自动完成功能使用 GET 调用服务,而不是 POST。我更改了服务的 WebInvokeAttributeWebGet 以接受 GET。现在,服务向扩展程序发送了正确的响应(我使用 Fiddler 观看了这一点),但扩展程序未填充完成列表。

扩展器定义如下(act 是 AjaxControlToolkit 的标记):

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBox1" runat="server" autocomplete = "off"></asp:TextBox>
    <act:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
        DelimiterCharacters="" Enabled="True" ServiceMethod="GetNames" 
        ServicePath="http://localhost:4227/Service1.svc" TargetControlID="TextBox1">
    </act:AutoCompleteExtender>
    <asp:Button ID="Button1"
        runat="server" Text="Button" />
</div>
 <act:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</act:ToolkitScriptManager>
</form>

WCF 服务工作在端口 4227 上。它由 Visual Studio 运行。在第一种情况下,ServicePath 是 Service1.svc

Web.Config 以这样的方式定义 sevicemodel:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
            <endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="WcfService1.Service1" />
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="WcfService1.Service1AspNetAjaxBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="WcfService1.Service1Behavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

那么,我有两个问题:

  1. 为什么在这种情况下 Autocomplete 使用不同的动词来发送请求?
  2. 为什么在第二种情况下不起作用?

我上传了示例解决方案来重现问题。

I'm trying to use a WCF service to fulfill MS Ajax AutoCompleteExtender completion list. I tried two alternatives. If I add a WCF service in my website project, AutoCompleteExtender calls it thriugh POST and it works fine.

Then I decided to make a separate WCF Application and add my AJAX-enabled WCF service to new application. I also copied part of Web.config of my site concerning servicemodel. And it doesn't work! First of all, autocomplete calls a service uing GET, not POST. I changed WebInvokeAttribute and WebGet of my service to accept GET. Now the service sends a correct response to extender (I watched this using Fiddler) but extender doesn't fill completion list.

The extender is defined as follows (act is a tag for AjaxControlToolkit):

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBox1" runat="server" autocomplete = "off"></asp:TextBox>
    <act:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
        DelimiterCharacters="" Enabled="True" ServiceMethod="GetNames" 
        ServicePath="http://localhost:4227/Service1.svc" TargetControlID="TextBox1">
    </act:AutoCompleteExtender>
    <asp:Button ID="Button1"
        runat="server" Text="Button" />
</div>
 <act:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</act:ToolkitScriptManager>
</form>

WCF service works on port 4227. It is running by Visual Studio. In the first case ServicePath is Service1.svc.

Web.Config defines sevicemodel in a such way:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
            <endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="WcfService1.Service1" />
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="WcfService1.Service1AspNetAjaxBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="WcfService1.Service1Behavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

So, I have two auestions:

  1. Why in this cases Autocomplete uses different verbs to send a request?
  2. Why it doesn't work in the second case?

I uploaded a sample solution to reproduce problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-08-25 09:21:26

为什么第二个不起作用
案例?

AutoCompleteExtender 使用 AJAX 来获取数据。不允许跨域 AJAX 请求。您的 Web 服务托管在 localhost:4227 上,您的 Web 应用程序托管在 localhost:XXXX 上,其中 XXXX 与 4227 不同。

更多信息同源政策

Why it doesn't work in the second
case?

AutoCompleteExtender uses AJAX to fetch data. Cross domain AJAX requests are not allowed. Your web service is hosted on localhost:4227 and your web application is hosted on localhost:XXXX where XXXX is different than 4227.

More info on Same origin policy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文