views:

774

answers:

1

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.

+1  A: 

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.

Darin Dimitrov
Do you mean that `example.com:xx` and `example.com:yy` are different domains? And domains `example.com` and `test.example.com` are also different?
flashnik
The `Same Origin Policy` imposes that **protocol**, **second-level domain** and **port** number of the request URI be the same to grant access from one resource to another. More info here: http://en.wikipedia.org/wiki/Same_origin_policy
Darin Dimitrov
Ok, thank you. Could you give advice how I can call from AJAX my webservice? I want it to be deployed not as part of website project and to use it (if hosted under IIS) in other aplication pool then website.
flashnik
What would you advise to choose: deploy WCF Service as application in Website (in this case I need to add reference to website assembly?) or use port sharing?
flashnik
I would advice you to create a bridge which is create an ASP.NET generic handler (.ashx) that will be part of the web site and that will perform the call to the actual web service. Then the `AutoCompleteExtender` could use this generic handler to fetch the data.
Darin Dimitrov
Ok, thank you for the help!
flashnik