【问题标题】:How to communicate between a windows service and windows form with a WCF?如何使用 WCF 在 Windows 服务和 Windows 窗体之间进行通信?
【发布时间】:2020-02-19 12:45:26
【问题描述】:

我有一个 超时 在此超时之后我有一个使用 WCF 在 Windows 服务和 Windows 窗体之间发送消息。

我已经可以从 windows 窗体向 windows 服务发送消息了:

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";
        }

    }

服务窗口

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

定时器

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();
        }
    }

}

我想在 windows 服务超时后向 windows 窗体发送一些东西,但我不知道该怎么做,有人可以帮助我吗?

【问题讨论】:

    标签: c# winforms wcf windows-services


    【解决方案1】:

    您可以使用 双工服务 以允许该服务也将消息发送到 Windows 窗体应用程序。 有关详细信息和示例,请参阅此link

    双工服务是一种 WCF 服务,它接收可用于向客户端发送消息的回调。除了服务契约,还需要创建服务使用的回调契约。

    请注意,回调与 WCF 操作上下文相关联。您可能需要将示例的静态 Init 方法更改为实例方法,并为每个调用/客户端创建一个新的 Class1 实例。

    【讨论】:

    • 我不确定我是否理解,但我会试试你的样品,谢谢你的帮助。
    • 我试了一下,但我无法启动我的服务。我的服务总是停止,我无法对其进行测试。
    • @user10863293 你需要调试你的服务启动。有关详细信息,请参阅此链接:docs.microsoft.com/en-us/visualstudio/debugger/…希望这会有所帮助。
    【解决方案2】:

    我们设计的服务是双工还是请求/响应模型。服务的发起者是客户端,因此我们应该将服务托管在 Windows 窗体应用程序中,而不是 Windows NT 服务。
    此外,您的 Windows NT 服务似乎向 Windows 窗体应用程序发送了一次消息。因此,我认为没有必要使用双工服务。
    关于双工服务,我曾经做过一个例子。希望对你有用。
    TimeOut exception in WCF while implementing duplex
    如果有什么可以帮助的,请随时告诉我。

    【讨论】:

    • 感谢您的帮助,我会看到您的示例。我只是向你解释我的交换,所以客户端会向 Windows 服务询问一些东西来工作,所以如果它工作,服务会发送一些东西,并且每 10 分钟服务器会询问客户端是否工作,客户端会回答一些问题。
    • 我创建了一个新项目,我只是复制并粘贴您的示例,但出现错误:System.ServiceModel.AddressAccessDeniedException: 'HTTP could not register URL http://+:3333/.您的进程无权访问此命名空间(有关详细信息,请参阅go.microsoft.com/fwlink/?LinkId=70353)。'
    • 因为机器端口占用是由操作系统驱动程序Http.SYS组件管理的。它需要操作系统将端口的权限分配给该用户。我们只需使用管理员帐户运行控制台应用程序即可解决此问题。此外,如果服务器端和客户端不是同一台计算机,我们应该在客户端提供身份验证凭据。
    • factory.Credentials.Windows.ClientCredential.UserName = "Serverwindowsuser"; factory.Credentials.Windows.ClientCredential.Password="123456":如果有什么我可以帮忙的,请随时告诉我。
    • 此外,我不明白如何使用 Windows 服务的双工我可以要求客户端执行操作
    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多