Recently I developed a WCF Web service and deployed it to a server with SSL, however, when clients try to add the service reference in their Visual Studio IDE, the endpoint URL address generated in the web.config or app.config file points to a wrong URL address: it points to the internal machine name instead of the production URL address. Because our SSL certificate is registered with the production URL, this wrong endpoint URL address causes the security exception until clients manually change the endpoint address to the correct production URL in the web.config or app.config file.
After some research online, I found out that the WSDL file generated by WCF somehow shows the wrong URL address and it is the reason when client retrieves the service information in Visual Studio, the wrong URL gets pulled. I could manually change the WSDL file to have the correct URL address, but it would be a pain in the neck because I would have to remember to make the change every time a new version of the service is deployed. There must be a easier way to it.
First, I need to figure out why the WSDL file generates the wrong URL in the first place and then I might be able to fix the root cause and would not need to change the WSDL file manually. And thanks to this post, I was able to find the root cause is that my service is hosted via SSL and WCF failed to retrieve correct URI information because of this. And according to Microsoft’s KB article, I need to force WCF to generate the correct URI by using the “Host” HTTP header of the incoming metadata request, but since the metadata contains HTTP URIs, I need to specify the correct port number for the request. All these need to be specified in the <behaviors> section of the web.config file of the WCF service as follows:
Once I have this in place, the generated WSDL file shows the correct URL and my customer does not need to manually change their web.config or app.config when adding the service reference in Visual Studio.
Hope this helps.