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. 🙂

Explicitly
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.

<serviceBehaviors>

  <behavior name=MyServiceBehavior>

    <serviceMetadata httpGetEnabled=true httpGetUrl=http://localhost:8080/Hello/ />

  </behavior>

</serviceBehaviors>

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.

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service behaviorConfiguration="MyServiceBehavior" name="Hello">
      <endpoint
        address="Hello"
        binding="basicHttpBinding"
        contract="IHello" />
      <endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8080/"/>
        </baseAddresses>
      </host>
    </service>
  </services>
</system.serviceModel>

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:

  • http://localhost:8080/Hello/
  • http://localhost:8080/MEX/
  • http://localhost:8080/?wsdl

Programmatically
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.

<appSettings>
  <add key="httpBindingAddress" value="http://localhost:8080/" />
</appSettings>

And then we can use it in our code and pass it into our ServiceHost constructor, like this.

Type type = typeof(Hello);
 
string httpBindingAddress = ConfigurationManager.AppSettings["httpBindingAddress"];
Uri[] baseAddresses = new Uri[] { new Uri(httpBindingAddress) };
 
using (ServiceHost host = new ServiceHost(type))
{
  host.Open();
}

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.

string httpBindingAddress = ConfigurationManager.AppSettings["httpBindingAddress"];
string nettcpBindingAddress = ConfigurationManager.AppSettings["nettcpBindingAddress"];
 
Uri httpUri = new Uri(httpBindingAddress);
Uri nettcpUri = new Uri(nettcpBindingAddress);
 
Uri[] baseAddresses = new Uri[] { httpUri, nettcpUri };
 
using (ServiceHost host = new ServiceHost(type, baseAddresses))
{

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.

<service behaviorConfiguration="MyServiceBehavior" name="Hello">
  <endpoint
    address="httpHello"
    binding="basicHttpBinding"
    contract="IHello" />
  <endpoint
    address="nettcpHello"
    binding="netTcpBinding"
    contract="IHello" />
  <endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
 
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:8080/"/>
      <add baseAddress="net.tcp://localhost:8090/"/>
    </baseAddresses>
  </host>
 
</service>

[Go to the WCF series article index]

You may also like...

6 Responses

  1. V says:

    The code of Programmatically must apply the baseAddress
    like this:
    using (ServiceHost host = new ServiceHost(type, baseAddresses)){}
    But you only do it like this
    using (ServiceHost host = new ServiceHost(type))

  2. V, you’re right, thanks…

  3. kakalot says:

    Thanks.It’s useful to me.

  4. I propose not to hold back until you get big sum of money to order different goods! You can get the loan or just sba loan and feel yourself free

  5. Do not want to be accused in plagiarism? Want to submit cearly unique papers? Choose plagiarism check and be assured that your stuff are unique.

  6. VVX says:

    Thanks for a cogent succinct explanation. Much appreciated.

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.