none
System.ServiceModel.CommunicationObjectFaultedException

    Frage

  • Hello,

    I have a self-hosted WF service installed and running as a Windows service.  No issues with the service as such but there are times when the service fails to read from the queue (MSMQ endpoint).  This happens at random.  This is a long running service that runs for about 18 hours.  The service gets called once daily to perform this long running task.  We have a scheduler that picks up new data from the DB and queues it, which then gets picked up by the WF service.  The service will be running fine for 2-3 days and then it will stop reading from the queue.  I end up having to go to the Services console and stopping and then starting this service.

    When I stop the service from the console, it throws an error that the service could not be stopped.  It stops at the second attempt.  Looking at the event log, I see this:

     

    Failed to stop service. System.ServiceModel.CommunicationObjectFaultedException: The communication object, System.ServiceModel.Activities.WorkflowServiceHost, cannot be used for communication because it is in the Faulted state.

       at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)

       at System.ServiceModel.Channels.CommunicationObject.Close()

       at MyService.Workflow.Master.Host.WorkflowMasterService.OnStop()

       at System.ServiceProcess.ServiceBase.DeferredStop()

    I searched for this and found this in these forums about client.Close() and client.Abort():

     

    http://social.msdn.microsoft.com/forums/en-US/wcf/thread/b95b91c7-d498-446c-b38f-ef132989c154/

     

    The service host code looks some thing like this (I have taken this from the book Pro WF .NET 4 by Bruce Bukovics):

     

    namespace MyService.Workflow.Master.Host
    {
      static class Program
      {
        private static List<WorkflowServiceHost> _hosts = new List<WorkflowServiceHost>();
    
        static void Main(string[] args)
        {
          bool consoleMode = false;
    
          foreach (string arg in args)
            if (0 == string.Compare(arg, "-consolemode", true))
              consoleMode = true;
    
          if (!consoleMode)
          {
            ServiceBase[] ServicesToRun = new ServiceBase[]
            {
              new WorkflowMasterService()
            };
            ServiceBase.Run(ServicesToRun);
          }
          else
          {
            try
            {
              CreateServiceHost(ConfigurationManager.AppSettings["WorkflowServiceFile"]);          
    
              foreach (WorkflowServiceHost host in _hosts)
              {
                host.WorkflowExtensions.Add(new ScottsTrackingParticipant());
                host.Open();
                foreach (var ep in host.Description.Endpoints)
                {
                  Console.WriteLine("Contract {0} at {1}", ep.Contract.Name, ep.Address);
                }
              }
              Console.WriteLine("Press any key to stop hosting and exit");
              Console.ReadLine();
            }
            catch (Exception ex)
            {
              Console.WriteLine("Service exception {0}", ex.ToString());
              Console.ReadLine();
            }
            finally
            {
              Console.WriteLine("Closing services");
              foreach (WorkflowServiceHost host in _hosts)
              {
                host.Close();
              }
              Console.WriteLine("Services closed");
              _hosts.Clear();
            }
          }
        }
    
        private static WorkflowServiceHost CreateServiceHost(String xamlxName)
        {
          WorkflowService wfService = LoadService(xamlxName);
          WorkflowServiceHost host = new WorkflowServiceHost(wfService);
    
          _hosts.Add(host);
    
          return host;
        }
    
        private static WorkflowService LoadService(String xamlxName)
        {      
          WorkflowService service = XamlServices.Load(Path.Combine(ConfigurationManager.AppSettings["WorkflowServicePath"], ConfigurationManager.AppSettings["WorkflowServiceFile"])) as WorkflowService;
    
          if (service != null)
          {
            return service;
          }
          else
          {
            throw new NullReferenceException(String.Format("Unable to load service definition from {0}", Path.Combine(ConfigurationManager.AppSettings["WorkflowServicePath"], ConfigurationManager.AppSettings["WorkflowServiceFile"])));
          }
        }
      }
    }
    

     

    Questions:

    How can I change the above code so the service does not go in faulted state?  What causes a service to go in to faulted state?  Would adding SQL tracking help identify why the service goes in to faulted state?

    Thanks!


    Mittwoch, 6. April 2011 21:12

Antworten