none
Not able to set Endpoints through Code when hosting WCF service in a Windows Service RRS feed

  • Question

  • Hi all,

    I have to host a WCF service which is configured by the host through C# code without configuration file, i.e the endpoints are set dynamically. When I use a console application to host it the service hosts properly and I am able to invoke the methods of the service through WCF Test Client, everything is fine. But my requirement is that, it should be hosted in a windows service and when I am trying to use a Windows Service to host it, the same code does not run, the Service Starts fine but I am not able to invoke any methods in the service. PFB the code :-

    C# method for the OnStart in Console App :-

    protected static void OnStart(string[] args)
            {
                //string ipAddress = "107.108.32.75";
                string ipAddress = GetSystemIp();
                const int port = 8089;
                string strAdd = "http://" + ipAddress + ":" + port +
                                "/TestSample/service";
                var baseAddressUri = new Uri(strAdd);
    
                //create HTTP binding
                var wsHttpBindingIPerfMon = new BasicHttpBinding();
                wsHttpBindingIPerfMon.BypassProxyOnLocal = false;
                wsHttpBindingIPerfMon.MessageEncoding = WSMessageEncoding.Text;
                wsHttpBindingIPerfMon.TextEncoding = Encoding.UTF8;
                wsHttpBindingIPerfMon.UseDefaultWebProxy = true;
    
                //create security mode
                wsHttpBindingIPerfMon.Security.Mode = BasicHttpSecurityMode.None;
                wsHttpBindingIPerfMon.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
    
    
                using (var host = new ServiceHost(typeof(DemoService), baseAddressUri))
                {
                    var serviceMetadataBehavior = new ServiceMetadataBehavior
                    {
                        HttpGetEnabled = true,
                        HttpGetUrl = new Uri(strAdd)
                    };
                    host.Description.Behaviors.Add(serviceMetadataBehavior);
                    host.AddServiceEndpoint((typeof(IDemo)), wsHttpBindingIPerfMon,
                        "test");
                    host.Open();
                    Console.WriteLine("Host has started at {0}", DateTime.Now.ToShortTimeString());
                    Console.ReadLine();
                }
            }

    App.config of the Console App :-

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <services>
                <service name="DemoServiceLibrary.DemoService">
                </service>          
            </services>          
        </system.serviceModel>
    </configuration>

    The same method in Windows Service shows the below error :-

    Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata

    Error: Cannot obtain Metadata from http://107.108.32.75:8089/TestSample/service If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://107.108.32.75:8089/TestSample/service    Metadata contains a reference that cannot be resolved: 'http://107.108.32.75:8089/TestSample/service'.    There was no endpoint listening at http://107.108.32.75:8089/TestSample/service that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.    Unable to connect to the remote server    No connection could be made because the target machine actively refused it 107.108.32.75:8089HTTP GET Error    URI: http://107.108.32.75:8089/TestSample/service    There was an error downloading 'http://107.108.32.75:8089/TestSample/service'.    Unable to connect to the remote server    No connection could be made because the target machine actively refused it 107.108.32.75:8089

    I have also added another endpoint to solve it but still no help :-

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="DemoServiceLibrary.DemoService">      
            <!--<host>
              <baseAddresses>
                <add baseAddress="http://localhost:8732/Design_Time_Addresses/DemoServiceLibrary/Service1/" />
              </baseAddresses>
            </host>-->
            <endpoint address="http://localhost/IISHostedService/MyService.svc"
    	binding="wsHttpBinding" contract="DemoServiceLibrary.IDemo"></endpoint>
          </service>
        </services>
      </system.serviceModel>
    </configuration>

    Let me know if there are any known solutions? Thanks in advance.

    Regards

    Jashobanta
    jashobantac@gmail.com
    jashobantac@outlook.com
    jashobantac@hotmail.com
    jashobantac@yahoo.com


    Friday, May 9, 2014 9:32 AM

Answers

  • Thanks for the reply Amy. However I spotted out the error today. Everything is OK in this code except the below line :-

     using (var host = new ServiceHost(typeof(DemoService), baseAddressUri))
                {

     So what was happening was the host was getting closed automatically as it was coming out of the USING statement and hence it was not running, hence the endpoints were not accessible. So, what I did was removed the using statement and created a new host instance and it worked. :-

    var host = new ServiceHost(typeof(DemoService), baseAddressUri);
    ....
    ....
    ....
    host.Open();

    Thanks for the answer though.

    Regards
    Jashobanta

    Monday, May 12, 2014 3:07 PM

All replies

  • Hi,

    I wonder that if you have installed windows service very well. And please make sure that the windows service is running when you calling the wcf service.

    For more information, please try to refer to the following articles:
    #Windows Service Hosting-WCF:
    http://www.wcftutorial.net/WCF-Windows-Service-Hosting.aspx .

    #WCF Hosting with Windows Service:
    http://www.codeproject.com/Articles/653493/WCF-Hosting-with-Windows-Service .

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 12, 2014 7:12 AM
    Moderator
  • Thanks for the reply Amy. However I spotted out the error today. Everything is OK in this code except the below line :-

     using (var host = new ServiceHost(typeof(DemoService), baseAddressUri))
                {

     So what was happening was the host was getting closed automatically as it was coming out of the USING statement and hence it was not running, hence the endpoints were not accessible. So, what I did was removed the using statement and created a new host instance and it worked. :-

    var host = new ServiceHost(typeof(DemoService), baseAddressUri);
    ....
    ....
    ....
    host.Open();

    Thanks for the answer though.

    Regards
    Jashobanta

    Monday, May 12, 2014 3:07 PM