none
WCF Service best practice to keep my services alive RRS feed

  • Question

  • I have several WCF Services in my WPF application, I open them using this method:

    private void StartSpecificWCFService(IService service, string url, Type serviceInterfaceType)
    {
        ServiceHost serviceHost = new ServiceHost(service, address);
        serviceHost.AddServiceEndpoint(serviceInterfaceType, new NetNamedPipeBinding(), url);
        serviceHost.Open();
        //sign to serviceHost.Faulted ??
        _wcfServicesHolder.Add(serviceHost); //A dictionary containing all my services
    }
    the services attributes are:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    The services are logging service and event service, they get many calls from other processes.. I use namedpipes since it is the fastest and the processes run on SAME computer.

    My question is - How do i maintain these services to be up all time ?

    Poll timer that iterate _wcfServicesHolder and check if service is opened
    sign to serviceHost.Faulted event.
    And after a service is in faulted state, does the client (on different process) must be re-created ? or it can still broadcast message on same channel ?

    The exception i receive is:

    There was no endpoint listening at net.pipe://localhost/LoggingService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details
    Thursday, May 23, 2013 3:28 PM

Answers

  • Hi,

    >>My question is - How do i maintain these services to be up all time ?

    >>And after a service is in faulted state, does the client (on different process) must be re-created ? or it can still broadcast message on same channel ?

    As per my knowledge, if you want to make the client and server keep connection for a long period, you need set receiveTimeout as large as possible, you can wrap the call in a try catch and if it throws an exception, recreate the channel from the existing channel factory. And if there is a communication problem between the client and server, WCF will raise a Faulted event, you can handle the fault at the client side.

    If the channel become faulted, message cannot been sent.

    For the fault handling, you can take a look at the reply in this thread, http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/bbd6207f-ad04-4e5b-9a99-d18b390e3fbb/

    >>There was no endpoint listening at net.pipe://localhost/LoggingService that could accept the message.

    Please check as the error message mentioned, "There was no endpoint listening at ...".

    Below is a test sample of self hosting a WCF service using NetNamedPipeBinding, for your information.

     static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(
                typeof(WcfServiceLibrary1.Service1NetPipe),
    
                    new Uri("net.pipe://localhost/service") ))
                {
                    host.AddServiceEndpoint(
                        typeof(WcfServiceLibrary1.IService1NetPipe),
                        new NetNamedPipeBinding(),
                        "PipeReverse");
    
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = false;
                    host.Description.Behaviors.Add(smb);
    
                    host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex");
                    host.Opened += delegate
                    {
                        Console.WriteLine("service started!");
                    };
                    host.Open();
                    Console.Read();
    
                }

     

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 24, 2013 7:08 AM
    Moderator

All replies

  • Hi,

    >>My question is - How do i maintain these services to be up all time ?

    >>And after a service is in faulted state, does the client (on different process) must be re-created ? or it can still broadcast message on same channel ?

    As per my knowledge, if you want to make the client and server keep connection for a long period, you need set receiveTimeout as large as possible, you can wrap the call in a try catch and if it throws an exception, recreate the channel from the existing channel factory. And if there is a communication problem between the client and server, WCF will raise a Faulted event, you can handle the fault at the client side.

    If the channel become faulted, message cannot been sent.

    For the fault handling, you can take a look at the reply in this thread, http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/bbd6207f-ad04-4e5b-9a99-d18b390e3fbb/

    >>There was no endpoint listening at net.pipe://localhost/LoggingService that could accept the message.

    Please check as the error message mentioned, "There was no endpoint listening at ...".

    Below is a test sample of self hosting a WCF service using NetNamedPipeBinding, for your information.

     static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(
                typeof(WcfServiceLibrary1.Service1NetPipe),
    
                    new Uri("net.pipe://localhost/service") ))
                {
                    host.AddServiceEndpoint(
                        typeof(WcfServiceLibrary1.IService1NetPipe),
                        new NetNamedPipeBinding(),
                        "PipeReverse");
    
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = false;
                    host.Description.Behaviors.Add(smb);
    
                    host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex");
                    host.Opened += delegate
                    {
                        Console.WriteLine("service started!");
                    };
                    host.Open();
                    Console.Read();
    
                }

     

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 24, 2013 7:08 AM
    Moderator
  • Hi, let me understand basic thing, I have WCF service and WCF client, My communication is set to be one way, fire and forget.. i dont need to set any Sendtimeout since i dont want to block the sending thread until message is queued on service side.

    My question is how to detect a service went down, and to bring it back up again. Do i need to do ServiceHost.Open() in order to acheive this ? and afterwards, do i need to recreate the client channel ?

    Friday, May 24, 2013 10:24 AM
  • Hi,

    According to your description, you can consider using WCF discovery and announcement for service detection and monitoring. Or implement a custom approach to polling the state of the service(available or not) to detect if the service is alive.

    For more information about WCF Discovery, refer: http://msdn.microsoft.com/en-us/library/dd456782.aspx

    #How to: Programmatically Add Discoverability to a WCF Service and Client

    http://msdn.microsoft.com/en-us/library/dd456782.aspx

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 27, 2013 1:52 PM
    Moderator