0

我使用从客户端接收字符串的简单方法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;

并且从我的服务中,我还订阅了这个事件,我想从这个事件中将这个对象发送到我的主窗体,以便更新我的 UI:

job.Event += job_Event;

    public void job_Event(Job obj)
    {
        // Send to to my main form and update UI
    }

我的问题是因为我的 ServiceBehavior 是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();
4

1 回答 1

1

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

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

        }
于 2013-10-03T17:26:09.493 回答