Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call simple WCF service using jQuery or simple js

I have a very simple hello world WCF service as given below. When I call it via asp.net project by adding web service reference it works perfectly fine. But when I call it using jQuery or standard js ajax call (using XMLHttpRequest) it calls back the success function but returns null data.

When I tried to access it via firefox browser using this address: http://localhost:8282/Test/TestService.svc/HelloWorld

It returned an error with code "a:ActionNotSupported" and error detail as

The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).

If I change binding to wsHttpBinding then it returns nothing even in Firefox.

Here is the code:

File "Test/ITestService.svc":

[ServiceContract(Namespace = "http://localhost:8282/")]
public interface ITestService
{

    [OperationContract]
    string HelloWorld();
}

File "Test/TestService.svc":

public class TestService : ITestService
{
    public string HelloWorld()
    {
        return "This is echo from server. Hello World";
    }
}

File "web.config"

<system.serviceModel>

    <services>
    <service name="radMLRPC.Test.TestService" behaviorConfiguration="radMLRPC.Test.TestServiceBehavior"
        <endpoint address="HelloWorld" binding="webHttpBinding" contract="radMLRPC.Test.ITestService">
            <identity>
            <dns value="localhost"/>
            </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
    <serviceBehaviors>
        <behavior name="radMLRPC.Test.TestServiceBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>
like image 757
Adeem Avatar asked Jul 19 '10 10:07

Adeem


2 Answers

with code above service only allows soap requests so to allow Get http requests we have to modify code as given below:

In interface:

    [WebGet(UriTemplate="helloworld")]
    [OperationContract]
    string HelloWorld();

in web config:

  • add behaviorConfiguration:

    <endpoint address="" binding="webHttpBinding" contract="radMLRPC.Test.ITestService" behaviorConfiguration="webBehav">
    
  • then in behaviors add following tag:

    < endpointBehaviors > < behavior name="webBehav" > < webHttp /> < /behavior > < /endpointBehaviors >

"please remove extra spaces from above. it was not showing the tags without extra spaces"


Check out some resources for that:

An Introduction To RESTful Services With WCF http: //msdn.microsoft.com/en-us/magazine/dd315413.aspx

Endpoint.TV screencasts:

  • Building RESTful services with WCF (Part 1) http://channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Building-RESTful-Services-with-WCF/
  • Building RESTful services with WCF (Part 2) http: //channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Building-RESTful-Services-with-WCF-Part-2/
  • Calling RESTful services in WCF http: //channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Calling-RESTful-Services-in-WCF/

Endpoint.TV in general has really good coverage for WCF and WCF REST stuff. http: //channel9.msdn.com/shows/Endpoint/

like image 167
Adeem Avatar answered Sep 27 '22 16:09

Adeem


Consuming ASP.net WebServices, WCF Services and static Page methods from JavaScript (sans MS AJAX)

For webHttp and client script, a mex binding is not useful. drop it for now.

The identity may be causing you some grief, drop it for now.

You have HelloWorld as your address, in order to call the HelloWorld method on your service you would need to call http://localhost:8282/Test/TestService.svc/HelloWorld/HelloWorld. drop it.

<services>
    <service name="radMLRPC.Test.TestService" behaviorConfiguration="radMLRPC.Test.TestServiceBehavior"
    <endpoint address="" binding="webHttpBinding" contract="radMLRPC.Test.ITestService"/>
  </service>
</services>
<behaviors>
    <serviceBehaviors>
    <behavior name="radMLRPC.Test.TestServiceBehavior">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Now that may not get you all the way there but will give us a better starting point. I am willing to help you resolve this.

Compare what we have now with the working example shown in the linked article

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="AjaxEndpointBehavior">
        <enableWebScript />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="ClientScriptServices.Service1Behavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service behaviorConfiguration="ClientScriptServices.Service1Behavior" name="ClientScriptServices.Service1">
      <endpoint behaviorConfiguration="AjaxEndpointBehavior" binding="webHttpBinding" contract="ClientScriptServices.Service1" />
    </service>
  </services>
</system.serviceModel> 

And see if we can get your config shaped similarly and we can cover some finer points of tuning your input and output formats.

like image 34
Sky Sanders Avatar answered Sep 27 '22 16:09

Sky Sanders