WCF Part 6 : Address
We’ll return once again to the WCF ABC and in this part we’ll examine what we can do with the address of our service. There are many options on how to specify the address of your service, especially when you start combining options. But I’ll discuss the three main options. Don’t be scared by the size of this article, it’s easy to understand. 🙂
As seen in part 3 about configuration, we configured the address of our service explicitly in the address attribute of our service’s endpoints; one for our service itself and one for metadata. What we haven’t done is setup an endpoint for WSDL discovery. If we want to enable http discovery, we need to set this up at the serviceMetadata behavior. Because we’re not using relative addresses, we need to set it explicitly as well.
Now the wsdl is immediately accessible from the root of our service or by adding /?wsdl to the url.
Relative (preferred option)
You’re better of using relative addresses though, for various reasons. One of them is administration, as all addresses aren’t scattered throughout your config and application. To support relative addresses, the WCF team introduced base addresses to the configuration, after a lot of requests from users. Using a base address, you’d get the following.
As you can see, the host node was added with one base address. Next article we’ll introduce other bindings and have multiple base addresses. Both the service endpoint, the MEX endpoint and the WSDL document (enabled with httpGetEnabled) don’t need an address anymore. Their addresses are respectively:
This is the option that was used in the past, before the base address was introduced. When creating a ServiceHost object, you can pass in the base addresses for it. Of course you can do this hardcoded, but we just don’t do that anymore in 2006 as we all know it’d take a recompile and redeploy to change the address. So we grab it from our configuration file. Here’s the config.
And then we can use it in our code and pass it into our ServiceHost constructor, like this.
Multiple base addresses
Although we’re not going to talk about bindings and need multiple addresses, we are talking about addresses, so I want to show you how this is done. First, we’ll use the programmatic version. Of course our config file holds these addresses under different keys. The code would be this.
So we can do this in config too? Of course. In the relative addresses method, the code doesn’t have to change. Isn’t that just great? Look at the changes between the configuration below and the one from the relative paragraph. And please note, that the MEX endpoint has information for both http and net-tcp endpoints.