WCF Json GET Service: Verify EndpointAddresses Sender and Recipient Match

I have been working in .NET for a while, but I am new to WCF. I am trying to create my first WCF service using JSON. I thought that I would start really, very simply, and then I would build from there. But I somehow managed to spoil even the simplest services. Here is what I still have.

Web.Config:

<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="MarathonInfo.MarathonInfoService"> <endpoint address="http://localhost:10298/MarathonInfoService.svc" binding="webHttpBinding" contract="MarathonInfo.IMarathonInfo" /> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="false" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> 

Then in the service file:

 namespace MarathonInfo { public class MarathonInfoService : IMarathonInfo { public String GetData() { return "Hello World"; } } } 

And in the interface:

 namespace MarathonInfo { [ServiceContract] public interface IMarathonInfo { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GetData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] String GetData(); } } 

So, when I go to this URL:

 http://localhost:10298/MarathonInfoService.svc/GetData 

I get this error:

A message with To 'http: // localhost: 10298 / MarathonInfoService.svc / GetData' cannot be processed at the receiver due to an AddressFilter mismatch on the EndpointDispatcher. Make sure the sender and receiver of the Endpoint Addresses agree.

I can run the service just fine through Visual Studio in debug mode. But in the browser, I get only this error.

What am I doing wrong?

Thanks!

Casey

+6
source share
1 answer

If you want to create a WebHTTP WCF endpoint (that is, one that returns JSON and uses the [WebGet] / [WebInvoke] attributes), the endpoint must have the <webHttp/> behavior associated with it.

 <system.serviceModel> <services> <service name="MarathonInfo.MarathonInfoService"> <endpoint address="http://localhost:10298/MarathonInfoService.svc" binding="webHttpBinding" contract="MarathonInfo.IMarathonInfo" behaviorConfiguration="Web"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="Web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="false" /> </system.serviceModel> 
+15
source

Source: https://habr.com/ru/post/916728/


All Articles