最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call simple WCF service using jQuery or simple js - Stack Overflow

programmeradmin3浏览0评论

I have a very simple hello world WCF service as given below. When I call it via asp 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>

I have a very simple hello world WCF service as given below. When I call it via asp 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>
Share Improve this question edited Feb 5, 2016 at 8:19 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jul 19, 2010 at 10:11 AdeemAdeem 1,3561 gold badge17 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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./en-us/magazine/dd315413.aspx

Endpoint.TV screencasts:

  • Building RESTful services with WCF (Part 1) http://channel9.msdn./shows/Endpoint/endpointtv-Screencast-Building-RESTful-Services-with-WCF/
  • Building RESTful services with WCF (Part 2) http: //channel9.msdn./shows/Endpoint/endpointtv-Screencast-Building-RESTful-Services-with-WCF-Part-2/
  • Calling RESTful services in WCF http: //channel9.msdn./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./shows/Endpoint/

Consuming ASP 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.

发布评论

评论列表(0)

  1. 暂无评论