1
votes

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( );
        }
    }
}
1
In what sense are you trying to communicate between a WCF service and a windows service - BugFinder
Well, WCF is basically an abstraction layer around the transport mechanism. You define what data needs to be exchanged and it takes care of how to exchange that. So no problem at all to use WCF to communicate between front-end app and a Windows service. @BugFinder - José Augustinho
then your own question is mute! I was asking what you expected for 2 and 3 and you've just said you can do that... <confuse> - BugFinder
To exchange custom data. The same type from 1 to 2. - José Augustinho

1 Answers

1
votes

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...
        }
    }
}