问题描述:

For pure code-based WCF configuration, in .NET 4.0 one could configure a base address for a service when creating an instance of a ServiceHost (assuming a self-hosted scenario, which is suitable for my needs). While one could still do this with .NET 4.5, MSDN implies that using the new Configure() method is a better practice because it makes such code-based configuration a bit easier for self-hosted and a lot easier for web hosted scenarios (see Configuring WCF Services in Code).

The problem, however, is that though the object you have to work with inside the Configure method (a System.ServiceModel.ServiceConfiguration) has a BaseAddresses property it is read-only. Surely there must be a way to set the base addresses in code? My web searches have turned up not only nothing about this specific question, but no articles or posts about this Configure method at all, except for the single MSDN page I have referenced!

网友答案:

Looking in reflector what BaseAddresses property does:

// 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

Instance of ServiceConfiguration is created using ServiceHost instance:

// System.ServiceModel.ServiceConfiguration
internal ServiceConfiguration(ServiceHost host)
{
    ServiceConfiguration.CheckArgument<ServiceHost>(host, "host");
    this.host = host;
}

This means that ServiceConfiguration is just an extension to standard way of configuring ServiceHost. So I would suggest any old way of setting BaseAddress is still considered as the best practice (programmatic or using configuration).

相关阅读:
Top