I’ve seen this question to many times in different forums, and answered in many different ways that I decided to write a post with more details about it. The title says it all, but I want to reiterate the answer that I had to give so many times:
(*) I know, they’re not technically “REST” endpoints. They’re officially called "WCF Web HTTP” endpoints, not REST ones, since the term REST means a lot more than responding to plain-old HTTP requests using JSON or XML. But many people call (correctly or not) them WCF REST endpoints (or WCF REST services), so if they find an issue and search for it, I hope this is one of the links they get.
By a WCF Web HTTP endpoint, I mean an endpoint which uses the WebHttpBinding (or an equivalent custom binding), and the WebHttpBehavior (<webHttp/> if using config). The WebScriptEnablingBehavior (<enableWebScript/>) is a subclass of WebHttpBehavior, so that’s also covered. Also, endpoints defined using the WebServiceHostFactory class (<%ServiceHost Service=”your-service-class” Factory=”System.ServiceModel.Activation.WebServiceHostFactory”%> in a .svc file). Any of those are “web” endpoints, and I can’t repeat enough – using svcutil.exe and Add Service Reference to create a proxy for them does not work.
Why doesn’t it work?
In order for the tools to be able to generate a client which can consume the service, the service must expose data about itself, or metadata. That metadata includes things such as the endpoint ABCs (address, binding, contract) for all the endpoints exposed by the service. WCF supports exposing metadata using either the WSDL (Web Service Description Language), also known as “HTTP GET metadata”, or using the WS-MetadataExchange protocol (the two are fairly the same). Those are protocols are defined by the W3 consortium for SOAP Services, and are fairly widely adopted by most SOAP stacks in the industry, and WCF also implements them.
WCF Web HTTP services (endpoints) are not SOAP services (endpoints), so WSDL doesn’t work for them. There is a proposal for a description for resource-based services, called WADL (Web Application Description Language), but the W3C doesn’t have any plans to work on this specification, it has never really been widely adopted and WCF also hasn’t supported it in its service description. Even if we could create a metadata extension to the service to expose its information in that format, the proxy-generation tools (svcutil.exe and Add Service Reference) do not understand them. So this is why it doesn’t work, regardless of what people want to believe.
But I used it and it generated a proxy class and configuration! What gives?
Ok, if you still don’t believe, keep reading. Let’s look at the actual service definition of a WCF service with Web HTTP endpoints. The code below defines 3 services: one only with a Web endpoint, one with a SOAP endpoint, and one with both. In each case, we’ll save the WSDL produced by the service will be saved to a local file.
Ok, let’s now run svcutil.exe to generate a proxy for the Web endpoint case. It generates the following proxy class (some classes removed). Notice that it has all the data contracts, and even the service contract with all the operations as we defined. Isn’t it working?
No, it’s not working. Notice that although the service contract and all the operations are there, none of the WebInvokeAttribute / WebGetAttribute attributes are, so if we try to use that proxy to call the service, it will fail. Also, it didn’t generate any configuration file (where the address and binding information are stored by the tool).
Looking at the WSDLs which were saved when running the tool sheds some information on that. Using a diff tool (such as WinDiff, which is on the path on the Visual Studio Command Prompt), compare the files called HttpOnly.xml and SoapOnly.xml. The files are exactly the same up to a certain point. Then, the HttpOnly file has an empty <wsdl:service> element, while the SoapOnly.xml file has a lot more information about the service – namely, the binding information and the endpoint address (see below). With that information, the tool is able to create the configuration file which contains that information and lets the client call the service correctly. Also notice that the contract definition (<wsdl:message> and <wsdl:portType> elements) are exactly the same, without any annotation for things such as the WebInvokeAttribute / WebGetAttribute properties – so the tool has no information about what they should be. Which is, as I think I already mentioned, it doesn’t work.
Another interesting thing to notice: the files SoapOnly.xml and HttpAndSoap.xml are exactly the same. The contract is the same for both endpoints, and if you had more endpoints, they would show up as different bindings (<wsdl:binding>) or different addresses (wsdl:port) – and that information doesn’t exist for web endpoints.
Ok, I’m convinced. It doesn’t work. But why does the WCF provide an incomplete metadata, if it doesn’t work?
The answer to this question is simple, it does because we told WCF to do so, by adding the service metadata behavior. We asked WCF to provide metadata for the service, and it happily did its best effort for that. I think the main question is, should it throw when the service host is opening if there is a Web endpoint in the service, since that metadata is useless?
Well, it’s not really useless. If there are multiple endpoints in the service, one Web endpoint and a SOAP one, having metadata in that case is perfectly valid. With that, SOAP clients can consume it to create a proxy to talk to the service, and the contract for the Web endpoint is somehow shared out-of-band between the client and the service.
Ok, what if there is only one endpoint in the service description, shouldn’t the service then throw in this case? Well, no again. Remember that WCF is (very) extensible, so it’s possible that someone wanted to actually implement a WADL metadata extension and expose that information for their service. We don’t want to block that scenario. The WCF-specific tools (svcutil.exe and Add Service Reference) don’t understand them, but WCF was made to be consumed by any clients, not only WCF (or even .NET) ones, so it’s possible that someone liked the WADL specification enough that they created a tool for their favorite language to create a proxy for it.