A Base Address works an important role, when you are creating a Service with the Windows Communication Foundation API. This allows define the root address to parse the EndpointAddress, if the specified EndpointAddress Uri is a relative Uri. This is, only when the Endpoint Binding Protocol Scheme and the Base Address Uri Scheme match.
Then if you are creating a ServiceHost, you have the posibility of specify Service Base Addresses directly into your code, then you can add the respective endpoints in the host application configuration file. For Example:
// Sets the Base Address
Uri[] baseAddresses = new Uri[]{
new Uri("net.tcp://localhost:1234/Intranet"),
new Uri("http://localhost:5678/Internet")
};
ServiceHost AccountingHost =
new ServiceHost(typeof(AccountingImplementation), baseAddresses);
<configuration>
<system.serviceModel>
<services>
<service name="HostSample.AccountingImplementation">
<endpoint address="Ecuador"
binding="netTcpBinding"
contract="HostSample.IAccounting" />
<endpoint address="USA"
binding="wsHttpBinding"
contract="HostSample.IAccounting" />
<endpoint address="England"
binding="wsHttpBinding"
contract="HostSample.IAccounting" />
</service>
</services>
</system.serviceModel>
</configuration>
In a company that need to change regularly the service base addresses, to specify base addresses directly in code is a bad practice, because this are static by default, and they all need be dynamics address!. In some cases i look many developers using the AppConfig Section in a config file or a custom store, to resolve the problem, as this Example:
// Gets the Uri Addresses from an AppSetting element
Uri[] baseAddresses = new Uri[]{
new Uri(ConfigurationManager.AppSettings["tcpBaseAddress"],
new Uri(ConfigurationManager.AppSettings["httpBaseAddress"]
};
ServiceHost AccountingHost =
new ServiceHost(typeof(AccountingImplementation),
baseAddresses);
<configuration>
<appSettings>
<add key="tcpBaseAddress" value="net.tcp://localhost:1234/Intranet"/>
<add key="httpBaseAddress" value="http://localhost:4567/Internet"/>
</appSettings>
<system.serviceModel>
<services>
<service name="HostSample.AccountingImplementation">
<endpoint address="Ecuador"
binding="netTcpBinding"
contract="HostSample.IAccounting" />
<endpoint address="USA"
binding="wsHttpBinding"
contract="HostSample.IAccounting" />
<endpoint address="England"
binding="wsHttpBinding"
contract="HostSample.IAccounting" />
</service>
</services>
</system.serviceModel>
</configuration>
Ok, the service really works fine, returning the expected results when use this implementation, and my IT friends are happy. But, What happens if i have more than one service in the current app domain or were added new contracts using a new base addresess for a MSMQ Address for example?, Do you have an answer for it?
Mmm... I need add a lot of elements added into appSettings Sections, or if i use a custom store, add the base addresses and endpoints dynamicly, then load it before that the service will be started. This is not hard to design and develope, but will be waste important time for me.
But, WCF have a good solution for it, is more faster and more easy to implement that any other way, because this load the baseAddreses when read the service elements from the host configuration file. This is an example:
// I'm not add the service base addreses because they are
// dynamicly loaded from the configuration file
ServiceHost AccountingHost = new ServiceHost(typeof(AccountingImplementation));
<configuration>
<system.serviceModel>
<services>
<host>
<baseAddreses>
<add baseAddress="net.tcp://localhost:1234/Intranet"/>
<add baseAddress="http://localhost:4567/Internet"/>
</baseAddreses>
</host>
<service name="HostSample.AccountingImplementation">
<endpoint address="Ecuador"
binding="netTcpBinding"
contract="HostSample.IAccounting" />
<endpoint address="USA"
binding="wsHttpBinding"
contract="HostSample.IAccounting" />
<endpoint address="England"
binding="wsHttpBinding"
contract="HostSample.IAccounting" />
</service>
</services>
</system.serviceModel>
</configuration>
The Service Base Address and Endpoint Address are completly dynamic, using directly a configuration file, it is simply the best.
NOTE: You can register only one base address by service endpoint binding type.