none
How to expose 2 service binding in a console host application RRS feed

  • Question

  • Dear all,

    I have a define a test service that I try host on a test console application as TCP and http endpoint

    The corresponsing config file is as below :

     <services>
          <service name="SOAP_REST_Service.TestService" behaviorConfiguration="serviceDefaultbehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost"/>
              </baseAddresses>
            </host>
            <!-- Soap endpoint configuration-->
            <endpoint  binding="basicHttpBinding" address="soap" contract="SOAP_REST_Service.IDataService"
                        bindingConfiguration="WinSecurebasicHttp"/>
            <!-- Rest endpoint configuration webHttpBinding-->
            <endpoint address="test"  binding="webHttpBinding" contract="SOAP_REST_Service.IDataService" 
                      behaviorConfiguration="restbehavior"
                      bindingConfiguration="WinSecureHttp"/>
            <!-- TCP endpoint configuration-->
            
            <!--<endpoint address="net.tcp://localhost:6000" bindingConfiguration="tcpSecureTransport" 
                      binding="netTcpBinding"
                      contract="SOAP_REST_Service.IDataService"/>-->
            <!-- end point use with tcp binding for data exposure-->
            <endpoint address="mex"  binding="mexHttpBinding" contract="IMetadataExchange"/>
    
          </service>
        </services>

    In bold are the 2 enpoint that I need to lopen in my console host app which is as below :

     //define here your service initialisation using ServiceHost class
                Uri baseAddress = new Uri("net.tcp://localhost:6000");
                _tcpServiceHost = new ServiceHost(typeof(TestService), baseAddress);
                // Open the ServiceHost to start listening for messages.
                _tcpServiceHost.Open();
    
                 baseAddress = new Uri("http://localhost/test");
                _httpServiceHost = new ServiceHost(typeof(TestService), baseAddress);
                // Open the ServiceHost to start listening for messages.
                _httpServiceHost.Open();
    
                // The service can now be accessed.
                Console.WriteLine("The service is ready.");

    If I comment the second http adress to open all is fine, my tcp service gets open and I can access it.

    But I nee to expose the service on 2 enpoint tcp and http. So as soon as I try to open both endpoint, then at the time the http gets initialise by the line : 

    _httpServiceHost = new ServiceHost(typeof(TestService), baseAddress);

    I get an exception as below :

    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'
    

    NOTE : Same thing happen if I comment the tcp initialisation

    Any idea whts is wrong in my config or the wa I open the host ?

    By setting the multipleSiteBindingsEnabled=true does not help in this case

    Thnaks for your help

    regards

    Tuesday, September 18, 2018 8:52 AM

Answers

  • Hi wakefun,

    According to your error details,

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

    And your code.

    >>  <host>
              <baseAddresses>
                <add baseAddress="http://localhost"/>
              </baseAddresses>
            </host>
    >>    baseAddress = new Uri("http://localhost/test");
                _httpServiceHost = new ServiceHost(typeof(TestService), baseAddress);

    The problem is caused by the fact that the base address list contains multiple HTTP schemes. But if one is HTTP scheme and the other is net.tcp scheme, there will be no conflict.

    I have made a completed configuration. Wish it is useful to you.

      <system.serviceModel>
    
        <services>
    
          <service name="sv">
    
            <endpoint address="basic" binding="basicHttpBinding" contract="isv"></endpoint>
    
            <endpoint address="web" binding="webHttpBinding" contract="isv" behaviorConfiguration="myweb"></endpoint>
    
            <endpoint address="net.tcp://localhost:1800" binding="netTcpBinding" contract="isv"></endpoint>
    
            <endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
    
            <endpoint address="MEX" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>
    
            <host>
    
              <baseAddresses>
    
                <add baseAddress="http://localhost:1700"/>
    
                <add baseAddress="net.tcp://localhost:1800"/>
    
              </baseAddresses>
    
            </host>
    
          </service>
    
        </services>
    
        <behaviors>
    
          <serviceBehaviors>
    
            <behavior>
    
              <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
    
            </behavior>
    
          </serviceBehaviors>
    
          <endpointBehaviors>
    
            <behavior name="myweb">
    
              <webHttp />
    
            </behavior>
    
          </endpointBehaviors>
    
        </behaviors>
    
      </system.serviceModel>


    Host.

     static void Main(string[] args)
    
            {
    
                using (ServiceHost sh = new ServiceHost(typeof(MyService)))
    
                {
    
                    Console.WriteLine("Service is ready....");
    
                    sh.Open();
    
                    Console.ReadLine();
    
                    sh.Close();
    
                }
    
            }
    
        }
    
        [ServiceContract(Namespace ="mydomain",ConfigurationName ="isv")]
    
        public interface IService
    
        {
    
            [OperationContract]
    
            [WebGet]
    
            string SayHello();
    
        }
    
        [ServiceBehavior(ConfigurationName ="sv")]
    
        public class MyService : IService
    
        {
    
            public string SayHello()
    
            {
    
                return "Hello Stranger";
    
            }
    
        }

    If you have any further questions, Feel free to contact me.

    Best Regards

    Abraham

    • Marked as answer by wakefun Friday, September 21, 2018 6:56 AM
    Thursday, September 20, 2018 7:38 AM
    Moderator

All replies

  • fine the way to do by adding different based adress in config file
    Wednesday, September 19, 2018 6:54 AM
  • Hi wakefun,

    According to your error details,

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

    And your code.

    >>  <host>
              <baseAddresses>
                <add baseAddress="http://localhost"/>
              </baseAddresses>
            </host>
    >>    baseAddress = new Uri("http://localhost/test");
                _httpServiceHost = new ServiceHost(typeof(TestService), baseAddress);

    The problem is caused by the fact that the base address list contains multiple HTTP schemes. But if one is HTTP scheme and the other is net.tcp scheme, there will be no conflict.

    I have made a completed configuration. Wish it is useful to you.

      <system.serviceModel>
    
        <services>
    
          <service name="sv">
    
            <endpoint address="basic" binding="basicHttpBinding" contract="isv"></endpoint>
    
            <endpoint address="web" binding="webHttpBinding" contract="isv" behaviorConfiguration="myweb"></endpoint>
    
            <endpoint address="net.tcp://localhost:1800" binding="netTcpBinding" contract="isv"></endpoint>
    
            <endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
    
            <endpoint address="MEX" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>
    
            <host>
    
              <baseAddresses>
    
                <add baseAddress="http://localhost:1700"/>
    
                <add baseAddress="net.tcp://localhost:1800"/>
    
              </baseAddresses>
    
            </host>
    
          </service>
    
        </services>
    
        <behaviors>
    
          <serviceBehaviors>
    
            <behavior>
    
              <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
    
            </behavior>
    
          </serviceBehaviors>
    
          <endpointBehaviors>
    
            <behavior name="myweb">
    
              <webHttp />
    
            </behavior>
    
          </endpointBehaviors>
    
        </behaviors>
    
      </system.serviceModel>


    Host.

     static void Main(string[] args)
    
            {
    
                using (ServiceHost sh = new ServiceHost(typeof(MyService)))
    
                {
    
                    Console.WriteLine("Service is ready....");
    
                    sh.Open();
    
                    Console.ReadLine();
    
                    sh.Close();
    
                }
    
            }
    
        }
    
        [ServiceContract(Namespace ="mydomain",ConfigurationName ="isv")]
    
        public interface IService
    
        {
    
            [OperationContract]
    
            [WebGet]
    
            string SayHello();
    
        }
    
        [ServiceBehavior(ConfigurationName ="sv")]
    
        public class MyService : IService
    
        {
    
            public string SayHello()
    
            {
    
                return "Hello Stranger";
    
            }
    
        }

    If you have any further questions, Feel free to contact me.

    Best Regards

    Abraham

    • Marked as answer by wakefun Friday, September 21, 2018 6:56 AM
    Thursday, September 20, 2018 7:38 AM
    Moderator
  • Thanks Abraham, got the point !
    Friday, September 21, 2018 6:56 AM