如何从每个会话的 WCF 服务事件返回到我的主窗体

本文关键字:返回 事件 我的 窗体 服务 WCF 会话 | 更新日期: 2023-09-27 18:33:41

Winform使用从客户端接收字符串的简单方法托管WCF服务的应用程序,此方法打开打开进程并执行操作的其他类的新输入:

namespace ServiceLibrary
    [ServiceContract()]
    public interface IService1
    {
        [OperationContract]
        string startProcess(string str);
    }

[ServiceBehavior(
    ConcurrencyMode = ConcurrencyMode.Multiple,
    InstanceContextMode = InstanceContextMode.PerSession)]
public class service1 : IService1
{
        public string startProcess(string str)
        {
            Jo job = new job();
            job.Event += job_Event;
            job.Start(str);
        }
}

在类Job我有一个事件,该事件引发了我所有类属性(名称,大小等)的事件

public delegate void StartEventHandler(Job obj);
public event StartEventHandler Event;
从我的服务中,

IM 也订阅了此事件,并且从此事件中,我想将此对象发送到我的主窗体,以便更新我的 UI:

job.Event += job_Event;
    public void job_Event(Job obj)
    {
        // Send to to my main form and update UI
    }

我的问题是因为我的服务行为ConcurrencyMode.Multiple而不是Single我有几个服务会话,我不知道如何在我的表单中引发事件。

这就是我从主窗体创建服务的方式:

urlService = "net.tcp://" + ipAddress.ToString() + ":8000/MyService";
ServiceHost new ServiceHost(typeof(ServiceLibrary.service1));
                    NetTcpBinding tcpBinding = new NetTcpBinding();
                    tcpBinding.TransactionFlow = false;
                    tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
                    tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
                    tcpBinding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof(ServiceLibrary.IService1), tcpBinding, urlService);
                    ServiceMetadataBehavior metadataBehavior;
                    metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                    if (metadataBehavior == null)
                    {
                        // Create the proxy object that is generated via the svcutil.exe tool
                        metadataBehavior = new ServiceMetadataBehavior();
                        metadataBehavior.HttpGetUrl = new Uri("http://" + _ipAddress.ToString() + ":8001/MyService");
                        metadataBehavior.HttpGetEnabled = true;
                        metadataBehavior.ToString();
                        host.Description.Behaviors.Add(metadataBehavior);
                        urlMeta = metadataBehavior.HttpGetUrl.ToString();
                    }
                    host.Open();

如何从每个会话的 WCF 服务事件返回到我的主窗体

您将需要一个具有静态函数的类来访问您的表单。这样的事情呢?

public class MyFormFunctions
{
    public static job_Event(Job obj)
    {
         /// do something to form
    }
}
        public string startProcess(string str)
        {
            Job job = new job();            
            MyFormFunctions.job_Event(job);
        }