none
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" ?>
    <configuration>
    	<!-- 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. -->
    	<startup>
    		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    	</startup>
    	<system.serviceModel>
    		<services>
    			<service behaviorConfiguration="MyServiceBehavior" name="MyServiceLibrary.MyService">
    				<endpoint
    					name="MyServiceLibrary_IMyService"
    					address="http://localhost:8733/MyService"
    					binding="basicHttpBinding"
              bindingConfiguration="MyEndpointBinding"
              contract="MyServiceLibrary.IMyService"
    				/>
    				<endpoint
    					name="mex"
    					address="mex"
    					binding="mexHttpBinding"
    					contract="IMetadataExchange"
    				/>
    				<host>
    					<baseAddresses>
    						<add baseAddress="http://localhost:8733/MyService/" />
    					</baseAddresses>
    				</host>
    			</service>
    		</services>
    		<behaviors>
    			<serviceBehaviors>
    				<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"/>
    				</behavior>
    			</serviceBehaviors>
    		</behaviors>
    		<bindings>
    			<basicHttpBinding>
    				<binding name="MyEndpointBinding"
             sendTimeout="00:01:00"
    				 maxReceivedMessageSize="2147483647"
    				 transferMode="Streamed"
    				 messageEncoding="Mtom">
    					<security mode="None">
    						<transport clientCredentialType="None" />
    					</security>
    				</binding>
    			</basicHttpBinding>
    		</bindings>
    		<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
    			<baseAddressPrefixFilters>
    				<add prefix="http://localhost:8733"/>
    			</baseAddressPrefixFilters>
    		</serviceHostingEnvironment>
    	</system.serviceModel>
    	<appSettings>
    		<add key="countoffiles" value="7" />
    		<add key="logfilelocation" value="abc.txt" />
    	</appSettings>
    </configuration>

    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);
    	serviceHost.Open();
    }

    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

Answers

  • 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...)
    	// https://stackoverflow.com/questions/17699469/hosting-wcf-service-in-a-windows-service-and-start-some-stuff-at-the-beginning
    	serviceHost = new ServiceHost(typeof(MyService));
    
    	// Open the ServiceHostBase to create listeners and start listening for messages.
    	serviceHost.Open();
    }

    Wednesday, August 15, 2018 6:26 PM