SOAP or REST this is always been a debated topic. In both the approach we have certain disadvantages as well as advantages. Internet is flooded with such an articles, but what if we want to keep both parallel, yes of course we can do this because WCF gives us all the freedom to use both the Approach using the same Service Contract. There is a very nice article which covers this topic you can find here.
Now what if we want to use Service Factory (I Assume reader of this article is familiar with WSSF 2010, if not then you can refer this section, you can get every thing you need to get started here.) . Service Factory is designed to help you quickly and consistently construct WCF Web services that adhere to well known architecture. But along with this WSSF does not support REST Service out of the box. We may either have to customize the WSSF Templates or may have to think something really out of the box and that's what I am going to show in this article.
There are couple of reason why I personally preferred the second approach and not to customize the template is for the design perspective if I keep both SOAP and REST in the same Contract it will be tightly coupled and SOAP and REST works in different way. And the same Service contract will have Dependency on both REST and SOAP. Also when we have to change something specific to REST or SOAP we will end up in changing for both even if we don't want to do so, this may force us to test both the clients of REST and SOAP. But this is just for case to case basis and I am leaving this on the reader. And now I am closing this design issues here and jumping directly to the implementation of the above.
Again I am not going to cover the basics of SOAP or REST or WSSF. I am going to create a WSSF guidance package with Service contract, data contract and host models, canvas diagrams of the same is given below.
For this example I am going to use the Northwind Database and to keep this simple and precise I am using only Customer table. With the above diagrams I hope you can now figure out my Service Contract, Data Contract and Message Contract. You can download the Northwind Database from here.
For the ORM tool I have used Entity Framework to keep this example simple, you may use your own Data Layer if you want. Generated solution structure looks like the one below, where I have done few addition for REST highlighted in RED.
Here in the example we have defined separate Service contract for Rest Service called NorthwindRestService which implements the contract INorthwindRestServiceContract. These are the files which are not generated by WSSF. Now lets get more into the codes of these files below.
In the code of Rest Service contract you can see I have used the WebGet and WebInvoke attributes to expose my service as a REST Service. And for my Service implementation I have used the codes below.
As you can notice this service implementation is used for just passing the response and request objects from Service to Business Logic and Back to service. So we are reusing our common Business Logic, Data Contract and Business entities which are the heart of the service and we had put maximum effort in these layers. The only effort we are putting now in just creating the Service Contract and Service Implementation which is just few lines of codes. Now lets have a quick look into our Data Layers and Business Layers. The data model of our Database is given as below.
and we had used Repository Pattern to Query our models. You can get a quick look into the codes below for my CustomerRepository and ICustomerRepository respectively.
Above code is just for reference only to be used for this article, in real scenarios we may have huge amount of codes in these places and also much more files for repository and also for BusinessLogic. So if you have understood till now, these codes are common for both REST and SOAP service. We had also created a translator for converting the Business entities to Data Contract and from Data Contract back to Business Entities. Which is very much required if we are following this Service Factory Patterns. If you have any queries on basics of WSSF you can go back to the link of WSSF Tutorials which I have already given above earlier.
With these codes our Service is almost ready to consume, now the only thing which we are missing is the hosting of these services for our example I am using ASP.NET development server hosting but you can use any other hosting also as per your requirements. And also the endpoints. Since for SOAP we can use various amount of bindings which corresponds to WS-* standards, but for REST we have to use webHttpBinding. Code of my web.config of REST and SOAP Service is given below.
and for SOAP Service
So with this I guess my Service is 100% ready to host, for SOAP Service WSSF Provides with generated Test Client for my REST Service I have created a similar project only with few difference. To keep the article focussed I have included the Test Projects with Complete Source code in the download Section. Hosting model diagram is given below.
We can also test this using the browser with the following URL’s formats. Please note these URL’s are for indication only actual URL’s Server names may not be same for you environment
For Save Operation, this method will pass a new customer for addition into the DB http://localhost:3188/WssfRestNorthwind.Rest.Host/NorthwindService.svc/Customer/Add
For those who want to have a Bird’s eye view of the Solution structure can click on the image to get the snapshot of the Entire Solution.
To get the feel of the above article and get more into the implementation logic I recommend you to download the complete source from here.
My objective of this article to show you the approach, so I am not getting into the details of few things like What is WSSF, REST, WCF, SOAP, etc.
This is my just a one approach from my point of view, if the reader of this article has a better idea or suggestion please feel free to comment. Thanks in advance.