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

crestsoapwcfwsdl

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.

Best Answer

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.