José Augustinho
José Augustinho

Reputation: 110

Using WCF to communicate over Windows service and Windows forms applications

Currently, I have 2 solutions in Visual Studio 2017:

  1. A Windows forms application
  2. A Windows service application hosting a WCF service class library (.dll)

And I need to communicate between them, in a cyclic way, as followed by the image below. The numbers represent the order.

enter image description here

The problem is, I'm actually able to communicate between WF and WCF application by using the request-replay operation contract (represented by numbers 1 and 4). But I'm not sure how to acomplhish steps 2 and 3.

Code for WCF interface:

namespace SmithWcfService {
    [ServiceContract]
    public interface ISmithWcfService {
        [OperationContract]
        void SendRequest( ); //Operation called by Windows Forms
    }
}

Code for WCF interface implementation

namespace SmithWcfService {
    public class SmithWcfService : ISmithWcfService {
        public void SendRequest( ) {
            //Ok, now I need to call Windows service application
        }
    }
}

Code for Windows service

namespace SmithWindowsService {
    static class Program {
        static void Main( ) {
            ServiceBase[ ] ServicesToRun;
            ServicesToRun = new ServiceBase[ ] {
                new SmithWindowsService( )
            };
            ServiceBase.Run( ServicesToRun );
        }
    }
}

namespace SmithWindowsService {    
    public partial class SmithWindowsService : ServiceBase {
        private ServiceHost host;

        public SmithWindowsService( ) {
            InitializeComponent( );
        }

        protected override void OnStart( string[ ] args ) {
            host = new ServiceHost( typeof( SmithWcfService.SmithWcfService ) );
            host.Open( );
        }
    }
}

Upvotes: 1

Views: 1249

Answers (1)

nvoigt
nvoigt

Reputation: 77285

If the windows service hosts your WCF service, you can simply pass anything it needs (callbacks, values, settings) at service start. You could pass a method of the windows service as Func<Input2, Output3> that the WCF service should call.

Without your code it's hard to tell where you need to put it though. Normally, it goes into your custom ServiceHostFactory.


Example of service with callback:

namespace SmithWcfService 
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
    public class SmithWcfService : ISmithWcfService 
    {
        private Func<string, int> callback;

        public SmithWcfService(Func<string, int> callback)
        {
            this.callback = callback;
        }

        public void SendRequest() 
        {
            //Ok, now I need to call Windows service application:
            var output = this.callback("input");
        }
    }
}

Example of hosting:

namespace SmithWindowsService 
{    
    public partial class SmithWindowsService : ServiceBase 
    {
        private ServiceHost host;

        public SmithWindowsService( ) 
        {
            InitializeComponent( );
        }

        protected override void OnStart(string[] args) 
        {
            var instance = new SmithWcfService.SmithWcfService(this.SomeMethodYouWantToCallIn);
            host = new ServiceHost(instance, new Uri("your.url.com"));
            host.Open( );
        } 

        private int SomeMethodYouWantToCall(string data)
        {
            // do things...
        }
    }
}

Upvotes: 1

Related Questions