How to set WCF base addresses in code when using Configure method
For pure code-based WCF configuration in .NET 4.0, it is possible to configure the base address for the service when instantiating the ServiceHost (assuming a self-contained script that suits my needs). While it is still possible to do this with .NET 4.5, MSDN implies that using the new method Configure()
is best practice because it simplifies self-hosted code-based configuration and much easier for web hosting (see Configuring WCF Services in Code ).
The problem is that although the object you have to work with inside the Configure (System.ServiceModel.ServiceConfiguration) method has a property BaseAddresses
, it is read-only. Surely there must be a way to set base addresses in code? My web searches appeared not only in this particular question, but in articles or posts about this Configure method in general, with the exception of one MSDN page I linked to!
source to share
Looking at the reflector, which has the property BaseAddresses
:
// System.ServiceModel.ServiceConfiguration
public ReadOnlyCollection<Uri> BaseAddresses
{
get
{
return this.host.BaseAddresses;
}
}
I was unable to find any other method in ServiceConfiguration working with ServiceHost.BaseAddresses
An instance ServiceConfiguration
is created using an instance ServiceHost
:
// System.ServiceModel.ServiceConfiguration
internal ServiceConfiguration(ServiceHost host)
{
ServiceConfiguration.CheckArgument<ServiceHost>(host, "host");
this.host = host;
}
This means that it ServiceConfiguration
is simply an extension of the standard setting method ServiceHost
. So I would suggest that any old way of setting it up BaseAddress
is still considered best practice (programmatically or using configuration).
source to share