C# 传递到我的表单WCF客户端消息

C# 传递到我的表单WCF客户端消息,c#,winforms,wcf,C#,Winforms,Wcf,我有Winforms应用程序承载WCF服务。 这是我的按钮连接事件: private void btnConnect_Click(object sender, EventArgs e) { try { // Returns a list of ipaddress configuration IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());

我有
Winforms
应用程序承载
WCF
服务。 这是我的
按钮连接事件

private void btnConnect_Click(object sender, EventArgs e)
{
        try
        {
            // Returns a list of ipaddress configuration
            IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());
            // Get machine ipaddress

            IPAddress _ipAddress = IPAddress.Parse(tbServerIp.Text);

            // Create the url that is needed to specify where the service should be started
            urlService = "net.tcp://" + _ipAddress.ToString() + ":8000/MyService";

            // Instruct the ServiceHost that the type that is used is a ServiceLibrary.service1
            host = new ServiceHost(typeof(ServiceLibrary.service1));
            host.Opening += new EventHandler(host_Opening);
            host.Opened += new EventHandler(host_Opened);
            host.Closing += new EventHandler(host_Closing);
            host.Closed += new EventHandler(host_Closed);

            // The binding is where we can choose what transport layer we want to use. HTTP, TCP ect.
            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; // <- Very crucial

            // Add a endpoint
            host.AddServiceEndpoint(typeof(ServiceLibrary.IService1), tcpBinding, urlService);

            // A channel to describe the service. Used with the proxy scvutil.exe tool
            ServiceMetadataBehavior metadataBehavior;
            metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (metadataBehavior == null)
            {
                // This is how I 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();
                //pbIndicator.Image = Resources.indicator_green;
                btnConnect.BackColor = Color.Red;
                btnConnect.Text = "Stop";
            }

            host.Open();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.StackTrace);
    }
}
我想做的是将客户机消息从
HelloWorld函数
传递到我的主窗体,因此我尝试在
类服务1
中创建事件:

public delegate void StatusEventHandler(string srt);
public event StatusEventHandler StartEvent;
但即使从我的主窗体(在我的按钮单击事件中)注册后,它仍然为空
实现这一点的最简单方法是什么?

创建服务主机时,您给出了一个类型,这意味着每次调用服务时,都会创建该类型的新实例

host = new ServiceHost(typeof(ServiceLibrary.service1));
您需要将服务的实例传递给servicehost构造函数,以便每次调用都使用该实例

var yourServiceInstance = new ServiceLibrary.service1();

// attach event handlers here

host = new ServiceHost(yourServiceInstance);
执行此操作时,需要将服务类配置为单实例模式:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class service1
{
   // methods here
}

我需要用其他东西替换您的ServiceInstance?因为这样我就无法理解:(为了使用服务实例中的一个服务宿主构造函数,必须将服务的StaseCytoTrimeMod设置为InstanceContextMode.Single。这可以通过Service Error属性来配置。否则,请考虑使用类型参数的Service宿主构造函数。)如果您还没有服务类的实例,您将需要创建一个新实例。可能是一个类字段。好的,该消息包括您的问题的解决方案。“服务的InstanceContextMode必须设置为InstanceContextMode.Single”。这样做,它就会起作用。我可以举个例子(我是一名新开发人员…)?InstanceContextMode.Single对可以连接到我的服务的客户端数量有影响吗?
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class service1
{
   // methods here
}