1

I have a Timeout after this timeout I have a send a message between windows service and windows form with a WCF.

I can already send a message from windows form to a windows service :

WCF

public class Interact : IInteract
    {
        private Func<string, int> callback;

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

        public void SendRequest(string name)
        {
            var output = this.callback(name + " callback");
        }

        public string AskIfAlive()
        {
            return "ask";
        }

    }

Service windows

public partial class LMService : ServiceBase
{
    private ServiceHost host;

    public LMService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        Class1.Init();

        Interact instance = new Interact(ReceiveMsg);
        host = new ServiceHost(instance);
        host.Open();

    }

    private int ReceiveMsg(string data)
    {
        // Message from Windows form to windows service
        Log.writeEventLog(data);
        return 1;
    }
}

Timer

public static class Class1
{
    static int Timeout = 0;
    static Timer tm = new Timer();

    public static void Init()
    {
        tm.Interval = 1000;
        tm.Elapsed += Tm_Elapsed;
        tm.Start();
    }

    private static void Tm_Elapsed(object sender, ElapsedEventArgs e)
    {
        Timeout++;
        if(Timeout >= 10)
        {
            // SEND MESSAGE TO WINDOWS FORM
            tm.Stop();
        }
    }

}

I want to send something to the windows form after the timeout in windows service but I don't know how to do it someone can help me ?

user10863293
  • 770
  • 4
  • 11
  • 32

2 Answers2

0

You can use a Duplex Service to allow for the service to also send messages to the Windows Forms app. See this link for details and a sample.

A duplex service is a WCF service that receives a callback that it can use to send messages to the client. In addition to the service contract, you also need to create a callback contract that is used by the service.

Please note that the callback is tied to the WCF operation context. You might need to change the static Init method of your sample to an instance method and create a new instance of Class1 per call/client.

Markus
  • 20,838
  • 4
  • 31
  • 55
  • I'm not sure if I understand but I will try your sample, thank you for your help. – user10863293 Oct 23 '19 at 11:51
  • I try it but I couldn't start my service. My service always stop and I couldn't test it. – user10863293 Oct 23 '19 at 15:25
  • @user10863293 you need to debug your service startup. See this link for details: https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-debug-the-onstart-method?view=vs-2019 Hope this helps. – Markus Oct 24 '19 at 07:45
0

Whether the service we designed is duplex or request/response model. The initiator of the service is client-side, therefore we should host the service in Windows form application instead of the Windows NT service.
Besides, your Windows NT service seems to send a message to the Windows Form application once. Therefore, I don’t think it is necessary to use the duplex service.
About the duplex service, I have ever made an example. Wish it is useful for you.
TimeOut exception in WCF while implementing duplex
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • Thank you for your help I will see your example. I will just explain you my exchange, so client-side will ask something to the windows service to work so the service will send something if it works and every 10 minutes the server will ask if the client-side is working and the client side will answer something. – user10863293 Oct 24 '19 at 12:20
  • I create a new project and I just copy and paste your example but I have an error : System.ServiceModel.AddressAccessDeniedException: 'HTTP could not register URL http://+:3333/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).' – user10863293 Oct 24 '19 at 14:22
  • Because machine port occupancy is managed by the OS driver, Http.SYS component. It needs OS to allot the permission of the port to that user. we simply run the console application with an administrator account to solve this issue. Furthermore, if the server-side and the client-side are not the same computers, we should provide authentication credentials on the client-side. – Abraham Qian Oct 25 '19 at 01:57
  • factory.Credentials.Windows.ClientCredential.UserName = "Serverwindowsuser"; factory.Credentials.Windows.ClientCredential.Password="123456": Feel free to let me know if there is anything I can help with. – Abraham Qian Oct 25 '19 at 01:58
  • Moreover I don't understand how with the duplex from the windows service I can ask to the client to do an action – user10863293 Oct 25 '19 at 13:41
  • What do you mean that it doesn’t work? What are the error details? I would like to know what you have done after running into the last error. The console application hosting the service should be running under the administrator account. Besides, the communication between the server-side and the client-side is accomplished by the service contract and callback contract. – Abraham Qian Oct 28 '19 at 01:40
  • Yes when I try to run the application under the administrator account it works but hen I do it on my project with my windows service and my windows form it doesn't work with the error "System.ServiceModel.CommunicationException: 'The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.'" – user10863293 Nov 04 '19 at 08:36