2

I have taken over development on a .NET WCF project. Among other things, the project contains 3 files:

  • IApi.cs <= Defining the interfaces
  • JsonApi.svc.cs <= Implementing interface for JSON
  • SoapApi.svc.cs <= Implementing interface for SOAP

The two implementation files are almost identical - at least all the code in the implementation of the methods is identical. I am quite new to WCF programming, but it strikes me as odd, that we need to duplicate the code, just to implement JSON as well as SOAP.

Is there a way to merge this into one implementation and let the framework decide if data is to be transported by SOAP or JSON?

/ Carsten

3
  • 1
    SOAP is a transport protocol - its counterpart would be REST. JSON is a data format - its counterpart might be XML. You're comparing apples to soap :-) (pun intended) Commented May 11, 2011 at 15:22
  • Correct me if I am wrong .Do you want to return xml or json from same service? Commented May 11, 2011 at 16:07
  • Sorry about that - yes I was mixing terms. But I still want my implementation to be independent of transport protocol and data-format. Commented May 11, 2011 at 19:53

1 Answer 1

4

Defines two endpoints, with the same contract for your service implementation. Defines the first to use SOAP, then the second to use JSon :

<service name="YourService"> <endpoint address="rest" binding="webHttpBinding" contract="IYourService" behaviorConfiguration="RestBehavior"/> <endpoint address="soap" binding="wsHttpBinding" contract="IYourService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> <endpointBehaviors> <behavior name="RestBehavior"> <webHttp/> </behavior> </endpointBehaviors> 

Then there will be an endpoint at http://.../yourservice.svc/soap and another at http://.../yourservice.svc/rest

[edit] to answer to your comment, what I said is to replace this section :

<services> <service name="WebApi.SoapApi" behaviorConfiguration="ApiBehavior"> <endpoint address="basic" bindingNamespace="http://api.myservice.dk/Basic" contract="WebApi.IApi" binding="basicHttpBinding" bindingConfiguration="ApiBinding" /> </service> <service name="WebApi.JsonApi" behaviorConfiguration="ApiBehavior"> <endpoint address="web" bindingNamespace="http://api.myservice.dk/Web" contract="WebApi.IApi" binding="webHttpBinding" bindingConfiguration="ApiBinding" behaviorConfiguration="JsonBehavior" /> </service> </services> 

by :

<services> <service name="WebApi.UniqueApi" behaviorConfiguration="ApiBehavior"> <endpoint address="basic" bindingNamespace="http://api.myservice.dk/Basic" contract="WebApi.IApi" binding="basicHttpBinding" bindingConfiguration="ApiBinding" /> <endpoint address="web" bindingNamespace="http://api.myservice.dk/Web" contract="WebApi.IApi" binding="webHttpBinding" bindingConfiguration="ApiBinding" behaviorConfiguration="JsonBehavior" /> </service> </services> 

One service, with two endpoints

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.