Search code examples
ajaxwcfajaxcontroltoolkitautocompleteextender

Calling an Ajax-enabled WCF Application from AutoCompleteExtender


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.


Solution

  • 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.