Andrey Barykin
Andrey Barykin

Reputation: 21

WCF and Windows service communication

Please, can somebody advise me? I create WCF library that realizes endpoint net-pipe for job with external WCF client.I placed lib in Windows Service. But I dont know how to make data exchange from library without usage of WCF client-server mechanism. How can I put or get data from running WCF service in the windows service? For example I want to register WCF errors in Eventlog or give parameters to service for transfer them for clients connected to WCF.

In my project i need to create Silverlight or WPF client for industrial monitoring in my plant in the real time. For this I would like to use duplex channel in wcf service on the my OPC client

I have two objects in my Windows Service project. One object is OPC client and database client. If i create this object in code - everything works good. Another object is WCF service library with net.pipe binding for external connection to this service. Ok. I define in my code:

// window service class   
 public partial class SyncSiemensService : ServiceBase
    {
        private OpcServiceClass opcServer = new OpcServiceClass();

        public SyncSiemensService()
        {
        InitializeComponent();
        }

    protected override void OnStart(string[] args)
    {
              // OPC client- database client object
              opcServer.Start();

             // WCF interface for external communication
             // with this windows service
              using (ServiceHost host = new ServiceHost(typeof(DuplexPipeWcfService)))
                         {
                             host.Open();
                             eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);
                         }
    }
}

WCF service

    [OperationContract(IsOneWay = true)]
    void GetActValuesFromLine(string line);

string line - external data FOR opc object

and his callback

public interface IDuplexPipeWcfServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void ActualValuesUpdate(WcfDuplexActualValues value);
}

but "WcfDuplexActualValues value" in mine occurrence - data from OPC object for external connection in WCF. And now i dont know, how retrieve data from opc object without usage client-service communication between OPC object and WCF service library.

Thanks a lot

Upvotes: 0

Views: 1597

Answers (2)

flayn
flayn

Reputation: 5322

I am not 100% sure that i understand your question correctly, but i think you are trying to exchange data between your OpcServiceClass instance and your DuplexPipeWcfService without using WCF.

You can use another way to host your service, by creating the instance yourself:

protected override void OnStart(string[] args)
 {
          // OPC client- database client object
          opcServer.Start();

          var serviceInstance = new DuplexPipeWcfService();
          _host = new ServiceHost(serviceInstance)

         _host.Open();
        eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);
 }

Now you have both instances and you can exchange data between by passing the references. For example you could change the constructor of the OpcServiceClass:

protected override void OnStart(string[] args)
 {
          var serviceInstance = new DuplexPipeWcfService();
          opcServer.Start(serviceInstance );

          _host = new ServiceHost(serviceInstance)

         _host.Open();
        eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);
 }

You only have to change the signature of your OpcServiceClass.Start() method to take an object of the type IDuplexPipeWcfServiceCallback. This way you can communicate with the DuplexPipeWcfService but without the WCF overhead.

Change the signature like this:

OpcServiceClass.Start(IDuplexPipeWcfServiceCallback wcfService)

Upvotes: 1

flayn
flayn

Reputation: 5322

Your OnStart method opens the host and then closes it again straight away.

 public partial class SyncSiemensService : ServiceBase
    {
        private ServiceHost _host;
        private OpcServiceClass opcServer = new OpcServiceClass();

        public SyncSiemensService()
        {
        InitializeComponent();
        }

 protected override void OnStart(string[] args)
 {
          // OPC client- database client object
          opcServer.Start();

         // WCF interface for external communication
         // with this windows service
          _host = new ServiceHost(typeof(DuplexPipeWcfService)))

         _host.Open();
        eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);

 }

}

You need to keep it open and only dispose it when shutting down the service.

Upvotes: 2

Related Questions