WCF Windows Service: OnStart() throws "Service cannot be started. System.ArgumentException: This collection already contains an address with scheme http." RRS feed

  • Question

  • I have a WCF Windows Service Library started from my Windows Service Application. When I go to start the service, I receive the following error message.

    Service cannot be started. System.ArgumentException: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection. If your service is being hosted in IIS you can fix the problem by setting 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' to true or specifying 'system.serviceModel/serviceHostingEnvironment/baseAddressPrefixFilters'.
    Parameter name: item
       at System.ServiceModel.UriSchemeKeyedCollection.InsertItem(Int32 index, Uri item)
       at System.Collections.Generic.SynchronizedCollection`1.Add(T item)
       at System.ServiceModel.ServiceHostBase.AddBaseAddress(Uri baseAddress)
       at System.ServiceModel.ServiceHostBase.LoadConfigurationSectionHelper(Uri baseAddress)
       at System.ServiceModel.Description.ConfigLoader.LoadHostConfig(ServiceElement serviceElement, ServiceHostBase host, Action`1 addBaseAddress)
       at System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host, ServiceDescription description, Ser...

    Service/Library App.Config

    <?xml version="1.0" encoding="utf-8" ?>
    	<!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
    		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    			<service behaviorConfiguration="MyServiceBehavior" name="MyServiceLibrary.MyService">
    						<add baseAddress="http://localhost:8733/MyService/" />
    				<behavior name="MyServiceBehavior">
    					<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
    					<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
    					<!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment  to avoid disclosing exception information -->
    					<serviceDebug includeExceptionDetailInFaults="true" />
    					<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    				<binding name="MyEndpointBinding"
    					<security mode="None">
    						<transport clientCredentialType="None" />
    		<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
    				<add prefix="http://localhost:8733"/>
    		<add key="countoffiles" value="7" />
    		<add key="logfilelocation" value="abc.txt" />

    Here is the code, which I use to start the service via the Windows Service OnStart() event.

    protected override void OnStart(string[] args)
    	// Start: My Service Library
    	Uri baseAddress = new Uri("http://localhost:8733/MyService");
    	serviceHost = new ServiceHost(typeof(MyService), baseAddress);

    I should add that the service and the library are both in the same folder. Additionally, both configuration files are identical.

    I am distributing the MyService.exe.config (App.config) file with the exe, so the service definitely reads the App.Config file.

    What is the problem?

    Wednesday, August 15, 2018 6:13 PM


  • I again resolved my own problem. I am leaving these two WCF questions here, as hopefully it will help someone else or myself later on.

    I did not open a ServiceHostBase properly. The Uri specified in the question conflicted with the one specified in the application's App.config file. Here is a SO article discussing the topic a bit.

    The correct startup code is:

    protected override void OnStart(string[] args)
    	// Start: My Service Library (Create a ServiceHost for WCF Service Library, and then...)
    	serviceHost = new ServiceHost(typeof(MyService));
    	// Open the ServiceHostBase to create listeners and start listening for messages.

    Wednesday, August 15, 2018 6:26 PM