Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a single WCF service support both SOAP, REST and WSDL

Tags:

rest

c#

soap

wsdl

wcf

I'm trying to build a C# service in .NET 3.5 that supports both SOAP - and shows the WSDL - and REST.

The SOAP service and WSDL generation was easy enough to do using the ServiceHost and a BasicHttpBinding classes. Got that working and the client was happy.

Because the SOAP calls all used simple parameters, the client developers requested a REST interface for some of the commands. So I changed the ServiceHost class to a WebServiceHost, added necessary WebInvoke and WebGet attributes, added a WebHttpBinding class, and bingo - REST and SOAP were both working out of one service. Way cool, change one interface and both REST and SOAP got the new stuff.

But one problem - the WSDL no longer gets generated. I couldn't browse to http://server/service?wsdl and get the WSDL file. Checking the MSDN docs, that appears to be behavior for a default WebServiceHost.

Question: can I override this behavior so that the WSDL could be obtained? Doesn't have to the same URL as before - it can change - but I just need to have some URL into service to get the WSDL for those SOAP developers.

like image 794
Jason Swager Avatar asked Nov 13 '09 19:11

Jason Swager


People also ask

What is the difference between rest and soap in WCF?

Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.). Exposing a WCF service with both SOAP and REST endpoints, requires just a few updates to the codebase and configuration.

What is SOAP service in WCF?

SOAP services: in WCF programming model support interoperability between systems that are built with Java, other platforms, and those that use messaging standards that are supported by Microsoft®. These also support transports such as HTTP, TCP, named pipes, and MSMQ.

How to create two service contracts in WCF?

STEP 1: Create a new WCF service application. STEP 2:  Let us create two service contracts . One for SOAP and one for REST Service. I have created a Service Contract IService1 for SOAP and Service Contract IService2for REST as shown below, IService1.cs [ServiceContract]     publicinterfaceIService1         [OperationContract]

How to execute the greeting method using rest in WCF?

Click the “Service1.svc” service file. Now, let us call the REST service. We have successfully executed the Greeting method using REST. In this post, we have created a WCF service application and executed the default SOAP Service in the client application.


2 Answers

When you say "added a WebHttpBinding class", it sounds like you are doing a lot of the configuration in code as opposed to in configuration files.

If this is the case, you could try moving the configuration to the configuration file. Then create 2 endpoints for the contract one REST and one SOAP, with 2 different addresses and bindings.

like image 115
Shiraz Bhaiji Avatar answered Nov 15 '22 23:11

Shiraz Bhaiji


But one problem - the WSDL no longer gets generated. I couldn't browse to http://server/service?wsdl and get the WSDL file. Checking the MSDN docs, that appears to be behavior for a default WebServiceHost.

Yes - that's one of the drawbacks of REST - no more WSDL, no more machine-readable service description. You need to hope the service provider gives you a usable and up to date documentation on what you can do.

There's no WSDL for REST - period. Can't be turned on or anything - it just doesn't exist.

There are some efforts under way to provide something similar - called WADL (Web Application Description Language), but as far as I know, it's still far from an established standard by any means. Also see: Do we need WADL?

like image 26
marc_s Avatar answered Nov 15 '22 22:11

marc_s