Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST / SOAP endpoints for a WCF service

Tags:

rest

soap

wcf

I have a WCF service and I want to expose it as both a RESTfull service and as a SOAP service. Anyone has done something like this before?

like image 305
Wessam Zeidan Avatar asked Oct 09 '08 10:10

Wessam Zeidan


People also ask

How do I enable rest and SOAP on the same WCF service?

So how to enable both SOAP and REST services in the same WCF service. STEP 1: Create a new WCF service application. Configure service for both basicHttpBinding and webHttpBinding, So in Web. Config file define two endpoints - one each for SOAP and REST.

Is it possible to use RESTful services using WCF?

You can use WCF to build RESTful services in . NET. REST (Representational State Transfer) is an architecture paradigm that conforms to the REST architecture principles. The REST architecture is based on the concept of resources: It uses resources to represent the state and functionality of an application.

What are WCF service endpoints?

Endpoints provide clients access to the functionality offered by a WCF service. Each endpoint consists of four properties: An address that indicates where the endpoint can be found. A binding that specifies how a client can communicate with the endpoint. A contract that identifies the operations available.

How many endpoints can a WCF Service have?

The service configuration has been modified to define two endpoints that support the ICalculator contract, but each at a different address using a different binding.


2 Answers

You can expose the service in two different endpoints. the SOAP one can use the binding that support SOAP e.g. basicHttpBinding, the RESTful one can use the webHttpBinding. I assume your REST service will be in JSON, in that case, you need to configure the two endpoints with the following behaviour configuration

<endpointBehaviors>   <behavior name="jsonBehavior">     <enableWebScript/>   </behavior> </endpointBehaviors> 

An example of endpoint configuration in your scenario is

<services>   <service name="TestService">     <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>     <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="ITestService"/>   </service> </services> 

so, the service will be available at

  • http://www.example.com/soap
  • http://www.example.com/json

Apply [WebGet] to the operation contract to make it RESTful. e.g.

public interface ITestService {    [OperationContract]    [WebGet]    string HelloWorld(string text) } 

Note, if the REST service is not in JSON, parameters of the operations can not contain complex type.

Reply to the post for SOAP and RESTful POX(XML)

For plain old XML as return format, this is an example that would work both for SOAP and XML.

[ServiceContract(Namespace = "http://test")] public interface ITestService {     [OperationContract]     [WebGet(UriTemplate = "accounts/{id}")]     Account[] GetAccount(string id); } 

POX behavior for REST Plain Old XML

<behavior name="poxBehavior">   <webHttp/> </behavior> 

Endpoints

<services>   <service name="TestService">     <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>     <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>   </service> </services> 

Service will be available at

  • http://www.example.com/soap
  • http://www.example.com/xml

REST request try it in browser,

http://www.example.com/xml/accounts/A123

SOAP request client endpoint configuration for SOAP service after adding the service reference,

  <client>     <endpoint address="http://www.example.com/soap" binding="basicHttpBinding"       contract="ITestService" name="BasicHttpBinding_ITestService" />   </client> 

in C#

TestServiceClient client = new TestServiceClient(); client.GetAccount("A123"); 

Another way of doing it is to expose two different service contract and each one with specific configuration. This may generate some duplicates at code level, however at the end of the day, you want to make it working.

like image 57
4 revs, 3 users 98% Avatar answered Sep 23 '22 00:09

4 revs, 3 users 98%


This post has already a very good answer by "Community wiki" and I also recommend to look at Rick Strahl's Web Blog, there are many good posts about WCF Rest like this.

I used both to get this kind of MyService-service... Then I can use the REST-interface from jQuery or SOAP from Java.

This is from my Web.Config:

<system.serviceModel>  <services>   <service name="MyService" behaviorConfiguration="MyServiceBehavior">    <endpoint name="rest" address="" binding="webHttpBinding" contract="MyService" behaviorConfiguration="restBehavior"/>    <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>    <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="MyService"/>   </service>  </services>  <behaviors>   <serviceBehaviors>    <behavior name="MyServiceBehavior">     <serviceMetadata httpGetEnabled="true"/>     <serviceDebug includeExceptionDetailInFaults="true" />    </behavior>   </serviceBehaviors>   <endpointBehaviors>    <behavior name="restBehavior">     <webHttp/>    </behavior>   </endpointBehaviors>  </behaviors> </system.serviceModel> 

And this is my service-class (.svc-codebehind, no interfaces required):

    /// <summary> MyService documentation here ;) </summary> [ServiceContract(Name = "MyService", Namespace = "http://myservice/", SessionMode = SessionMode.NotAllowed)] //[ServiceKnownType(typeof (IList<MyDataContractTypes>))] [ServiceBehavior(Name = "MyService", Namespace = "http://myservice/")] public class MyService {     [OperationContract(Name = "MyResource1")]     [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "MyXmlResource/{key}")]     public string MyResource1(string key)     {         return "Test: " + key;     }      [OperationContract(Name = "MyResource2")]     [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource/{key}")]     public string MyResource2(string key)     {         return "Test: " + key;     } } 

Actually I use only Json or Xml but those both are here for a demo purpose. Those are GET-requests to get data. To insert data I would use method with attributes:

[OperationContract(Name = "MyResourceSave")] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource")] public string MyResourceSave(string thing){     //... 
like image 29
Tuomas Hietanen Avatar answered Sep 20 '22 00:09

Tuomas Hietanen