I have a Windows WCF service with the endpoint specified in the configuration file for the service.
<baseAddresses> <add baseAddress="net.tcp://localhost:9000/MyEndpoint"/> </baseAddresses>
Everything is working fine. However, in some cases, port 9000 may already be in use, which causes ServiceHost to crash on Open (). I need to override the default base address specified in the configuration file in the code. for example, suppose the environment variable contains the port number to be used.
Is there any way to do this programmatically?
After creating the ServiceHost, I can see the BaseAddresses property, which returns a list of Uri taken from the configuration file. However, this assembly is read-only, so it cannot be used to change the default values.
If I specify a Uri replacement in the ServiceHost constructor, I get
This collection already contains an address with the net.tcp schema. There can be no more than one address according to the scheme in this collection. If your service is in IIS, you can fix the problems by setting 'System.serviceModel / serviceHostingEnvironment / multipleSiteBindingsEnabled' true or specifying 'System.serviceModel / serviceHostingEnvironment / baseAddressPrefixFilters'.
If I create a CustomServiceHost and try to set the replacement base address, I get the same error.
class CustomHost : ServiceHost { public CustomHost(Type serviceType) : base (serviceType) { } protected override void ApplyConfiguration() { base.ApplyConfiguration(); this.AddBaseAddress(new Uri("net.tcp://localhost:9010/MyEndpoint")); } }
I know that if I leave the base addresses of the configuration file empty and pass the base address to the ServiceHost constructor, then this will work fine - that is, I can specify new databases. However, I want to use a configuration file to specify a default value (instead of hard coding).
Mark d jackson
source share