locked
"net.local" transport in Windows Service RRS feed

  • Question

  • Hello,

    I have been trying the LocalChannel (Channels Extensibility) sample provided with the RC version of .NET 4.0.

    For some reason, the channel extension code provided by the sample does not work when hosted in a Windows (NT) service. The WCF client sends the message to the WCF server, but the ServiceHost does not even instantiate a Service instance.

    The sample runs fine when hosted in a Console application. What is the reason it does not work in a Windows service?

    Thanks,
    Satyen

    Tuesday, April 13, 2010 11:19 PM

Answers

  • Hi Satyen,

    Regarding on the LocalChannel sample in WCF 4.0, I think there isn't much difference between windows service or console host since both of them are of self-hosting scenario. The only obvious difference I can get is that the execution process account may be different. Console application runs under your logon user account while windows service can be deployed to run under a ceratin low privileged service account(such as localservice or networkservice).

    For your case, I suggest you try deploy the windows service under a admin or localsystem acount to see whether it works. Also, have you got any definite exception when it failed in windows servcie, if the service stoped immediately after you try starting it, it is possible that there occurs some unhandled exception, you can try checking the windows event log for any error entries with your windows service.

    Here is the test windows service  code I used in my local environmenet which works:

    public partial class SimpleNTService : ServiceBase  {    const string baseAddress = "net.local://localhost:8080/GetPriceService";    ServiceHost _svcHost = null;    public SimpleNTService()    {      InitializeComponent();    }    protected override void OnStart(string[] args)    {            // Start the service host      _svcHost = new ServiceHost(typeof(GetPrice), new Uri(baseAddress));      _svcHost.AddServiceEndpoint(typeof(IGetPrice), new LocalBinding(), "");      _svcHost.Open();      // Invoke the local channel service for the first time      InvokeLocalChannelService();    }    protected override void OnStop()    {      if (_svcHost != null && _svcHost.State == CommunicationState.Opened)      {        _svcHost.Close();        _svcHost = null;      }    }    protected void InvokeLocalChannelService()    {      ChannelFactory<IGetPrice> channelFactory        = new ChannelFactory<IGetPrice>(new LocalBinding(), baseAddress);      IGetPrice proxy = channelFactory.CreateChannel();      // Log the local service call      StreamWriter sw = new StreamWriter(@"c:\temp\logs\wcfntsvc.log", true);      sw.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"        , 101, proxy.GetPriceForProduct(101));      sw.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"        , 202, proxy.GetPriceForProduct(202));      sw.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"        , 303, proxy.GetPriceForProduct(303));      sw.Close();      //channelFactory.Close();    }  }




    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, April 15, 2010 6:01 AM

All replies

  • Hi Satyen,

    Regarding on the LocalChannel sample in WCF 4.0, I think there isn't much difference between windows service or console host since both of them are of self-hosting scenario. The only obvious difference I can get is that the execution process account may be different. Console application runs under your logon user account while windows service can be deployed to run under a ceratin low privileged service account(such as localservice or networkservice).

    For your case, I suggest you try deploy the windows service under a admin or localsystem acount to see whether it works. Also, have you got any definite exception when it failed in windows servcie, if the service stoped immediately after you try starting it, it is possible that there occurs some unhandled exception, you can try checking the windows event log for any error entries with your windows service.

    Here is the test windows service  code I used in my local environmenet which works:

    public partial class SimpleNTService : ServiceBase  {    const string baseAddress = "net.local://localhost:8080/GetPriceService";    ServiceHost _svcHost = null;    public SimpleNTService()    {      InitializeComponent();    }    protected override void OnStart(string[] args)    {            // Start the service host      _svcHost = new ServiceHost(typeof(GetPrice), new Uri(baseAddress));      _svcHost.AddServiceEndpoint(typeof(IGetPrice), new LocalBinding(), "");      _svcHost.Open();      // Invoke the local channel service for the first time      InvokeLocalChannelService();    }    protected override void OnStop()    {      if (_svcHost != null && _svcHost.State == CommunicationState.Opened)      {        _svcHost.Close();        _svcHost = null;      }    }    protected void InvokeLocalChannelService()    {      ChannelFactory<IGetPrice> channelFactory        = new ChannelFactory<IGetPrice>(new LocalBinding(), baseAddress);      IGetPrice proxy = channelFactory.CreateChannel();      // Log the local service call      StreamWriter sw = new StreamWriter(@"c:\temp\logs\wcfntsvc.log", true);      sw.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"        , 101, proxy.GetPriceForProduct(101));      sw.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"        , 202, proxy.GetPriceForProduct(202));      sw.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"        , 303, proxy.GetPriceForProduct(303));      sw.Close();      //channelFactory.Close();    }  }




    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, April 15, 2010 6:01 AM
  • Steven,

    Thank you for your quick reply!

    I tried your code, and it worked just fine for me too. So, I tried to understand what was different with the code I was using before I reported this issue?

    The significant difference, and the one causing the problem, was that I was hosting the WCF server and client inside a "Hybrid" Windows Service (http://hws.codeplex.com/). I have been using this pattern for some time now when working on Windows Services, to help debug interactively. I have even used it to host WCF services. Apparently, it isn't able to host both a WCF server and a WCF client talking to each other.

    After identifying hws as the culprit, I also tried it with a NetNamedPipeBinding. That didn't work either. I wish I had tried this before I reported this issue :-(

    For now, I am happy that the LocalChannel code works inside a Windows Service. However, I noticed that this sample has been dropped from the final release of WCF 4.0. Do you have any insight into the reason for this? If there are any gotchas with using the code in the sample, I would be grateful if you can pass those along.

    Thanks again,
    Satyen

    Friday, April 16, 2010 3:15 PM
  • Steven,

    Thank you for your quick reply!

    I tried your code, and it worked just fine for me too. So, I tried to understand what was different with the code I was using before I reported this issue?

    The significant difference, and the one causing the problem, was that I was hosting the WCF server and client inside a "Hybrid" Windows Service (http://hws.codeplex.com/). I have been using this pattern for some time now when working on Windows Services, to help debug interactively. I have even used it to host WCF services. Apparently, it isn't able to host both a WCF server and a WCF client talking to each other.

    After identifying hws as the culprit, I also tried it with a NetNamedPipeBinding. That didn't work either. I wish I had tried this before I reported this issue :-(

    For now, I am happy that the LocalChannel code works inside a Windows Service. However, I noticed that this sample has been dropped from the final release of WCF 4.0. Do you have any insight into the reason for this? If there are any gotchas with using the code in the sample, I would be grateful if you can pass those along.

    Thanks again,
    Satyen

    Friday, April 16, 2010 3:15 PM
  • Hi Satyen,

    Glad that you've found the issue.

    BTW, how did you find that the sample is droped from the windows sdk samples for WCF/WF 4.0?

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, April 19, 2010 1:59 AM
  • I could swear the online documentation at MSDN did not list this sample under .NET 4.0 at the time I was facing this issue (it did when I was working with the RC version of the bits). I did not go so far as to download the samples package, and see if it contained the LocalChannel sample.

    I see that the online doc has it now, as does the samples package. So, I stand corrected, again :-(

    Once again, I appreciate your help.

    Monday, April 19, 2010 3:03 AM
  • Satyen,

    Did you build Hybrid Windows Service for 4.0 or use the dll provided?

    If you get a chance to try it against a 4.0 verison I would apprciate it so I can update the downloads notes for Hybrid Windows Service.

    Simon.


    Simon B .::. Unified Communications | Collaboration | Gadgets | www.evangelyze.net. Please mark forum responses as answers if they help you out. Thanks!
    Wednesday, September 29, 2010 4:14 PM