调用从AutoCompleteExtender支持Ajax的WCF应用 [英] Calling an Ajax-enabled WCF Application from AutoCompleteExtender

查看:202
本文介绍了调用从AutoCompleteExtender支持Ajax的WCF应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个WCF服务,以满足MS阿贾克斯AutoCompleteExtender完成列表。我尝试了两种备选方案。如果我在我的网站项目中添加一个WCF服务,AutoCompleteExtender称之为thriugh POST和正常工作。

然后,我决定做一个独立的WCF应用程序并添加我的支持AJAX的WCF服务,以新的应用程序。我也复制的一部分的Web.config 我的网站关于 servicemodel 的。而这是行不通的!首先,自动完成调用服务uing GET ,不是 POST 。我改变了 WebInvokeAttribute WebGet 我服务的接受 GET 。现在,该服务发送一个正确的响应扩展(我看到这个使用招),但增量不填写完成列表。

的扩展定义如下(的行为是一个AjaxControlToolkit中标记):

 <表格ID =Form1的=服务器>
< D​​IV>
    < ASP:文本框ID =TextBox1的=服务器自动完成=关闭>< / ASP:文本框>
    <行为:AutoCompleteExtender ID =TextBox1_AutoCompleteExtender=服务器
        DelimiterCharacters =启用=真ServiceMethod =GetNames
        ServicePath =HTTP://本地主机:4227 / Service1.svc的TargetControlID =TextBox1的>
    < / ACT:AutoCompleteExtender>
    < ASP:按钮的ID =Button1的
        =服务器文本=按钮/>
< / DIV>
 <行为:ToolkitScriptManager ID =ToolkitScriptManager1=服务器>
< / ACT:ToolkitScriptManager>
< /形式GT;
 

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

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

 < system.serviceModel>
    < serviceHostingEnvironment aspNetCompatibilityEnabled =真/>
    <服务>
        <服务名称=WcfService1.Service1behaviorConfiguration =WcfService1.Service1Behavior>
            <端点地址=behaviorConfiguration =WcfService1.Service1AspNetAjaxBehavior绑定=的WebHttpBinding合同=WcfService1.Service1/>
        < /服务>
    < /服务>
    <行为>
        < endpointBehaviors>
            <行为NAME =WcfService1.Service1AspNetAjaxBehavior>
                < enableWebScript />
            < /行为>
        < / endpointBehaviors>
        < serviceBehaviors>
            <行为NAME =WcfService1.Service1Behavior>
                < serviceMetadata httpGetEnabled =真/>
                < serviceDebug includeExceptionDetailInFaults =FALSE/>
            < /行为>
        < / serviceBehaviors>
    < /行为>
< /system.serviceModel>
 

所以,我有两个auestions:

  1. 为什么在这个情况下,自动完成使用不同的动词来发送一个请求?
  2. 为什么不能在第二种情况下工作?

我上传重现问题的一个样品溶液。

解决方案
  

为什么它不会在第二工作   这样吗?

AutoCompleteExtender使用AJAX来获取数据。跨域AJAX请求是不允许的。 Web服务托管在本地主机:4227 和Web应用程序托管在本地主机:XXXX ,其中 XXXX 比4227有所不同。

同源策略的更多信息。

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.

解决方案

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.

这篇关于调用从AutoCompleteExtender支持Ajax的WCF应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆