C# WCF,正在执行事件?

C# WCF,正在执行事件?,c#,wcf,C#,Wcf,在继续学习WCF的同时,我正在努力使它与事件一起工作 在本例中,在表单中单击一个按钮时,我希望我的wcf服务执行一个事件,该事件将触发连接到此服务的其他表单中的某些内容 这是Form1的代码 public Form1() { InitializeComponent(); client.TimeShowEvent += new EventHandler(TimeShowEvent); ///// SAYS THAT IT DOES NOT CONTAIN A DEFINITION

在继续学习WCF的同时,我正在努力使它与事件一起工作

在本例中,在表单中单击一个按钮时,我希望我的wcf服务执行一个事件,该事件将触发连接到此服务的其他表单中的某些内容

这是Form1的代码

public Form1()
{
   InitializeComponent();
   client.TimeShowEvent += new EventHandler(TimeShowEvent);
   ///// SAYS THAT IT DOES NOT CONTAIN A DEFINITION FOR TimeShowEvent
}

MyWcfService1.IfaceServiceClient client = new MyWcfService1.IfaceServiceClient();

private void button2_Click(object sender, EventArgs e)
{
   try
   {
      MyWcfService1.IfaceServiceClient client = new MyWcfService1.IfaceServiceClient();
      client.passTime();
   }
   catch
   {
      MessageBox.Show("Service not availabe!");
   }
}

void TimeShowEvent(object sender, EventArgs e)
{
   textBox2.Text = client.timestring;
   //// SAYS THAT IT DOES NOT CONTAIN A DEFINITION FOR timestring
}
至于这项服务:

namespace wcfLib
{
   [ServiceContract]
   public interface IfaceService
   {
       [OperationContract]
       int wordLen(string word);

       [OperationContract]
       string passTime();

        ///// DO I NEED TO SOMEHOW DECLARE THE VARIABLES ( timestring ) AND EVENTS ( TimeShowEvent ) HERE?
   }
}
服务实施:

public class StockService : IfaceService
{
    public event EventHandler TimeShowEvent;

    public string timestring = "none";

    public string passTime()
    {
        TimeShowEvent(this, new EventArgs());
        timestring = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
        return "";
    }

    public int wordLen(string word)
    {
        return word.Length;
    }
}
服务主机应用程序:

public class Service
{
    static void Main()
    {
        ServiceHost serviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/wcfLib"));
        serviceHost.AddServiceEndpoint(typeof(IfaceService), new BasicHttpBinding(), "");
        serviceHost.Open();
        Console.WriteLine("Press return to terminate the service");
        Console.ReadLine();
        serviceHost.Close();
    }
}
我是否需要以某种方式在[ServiceContract]中声明事件和变量?是吗,怎么了


谢谢!:)

当您为您的服务创建一个服务引用时,您的客户可以访问服务方法,并且只能访问服务方法

服务实现类中的事件处理程序
TimeShowEvent
将仅在服务器端出现并可用在客户端不可用

如果您想调用某个服务方法,则需要定义另一个服务方法——这些服务方法在客户端代理类中是“镜像的”——并且只定义这些服务方法


客户端代理和服务器共享的唯一连接是服务契约中定义的服务方法,以及作为序列化(XML)消息传递给这些方法的数据。客户机和服务器之间没有“神奇的链接”——客户机无法“进入”服务器类并从中读取内容或调用该类上的事件。两者之间没有“远程对象”连接。

请尝试以下操作:

string timeString;

private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            //You already declared client, so you don't need to do it again.

            //Assign the value from your wcf call to a local variable
            timeString = client.passTime();
        }
        catch
        {
            MessageBox.Show("Service not availabe!");
        }
    }
并修改服务以返回字符串:

public string passTime()
    {
        TimeShowEvent(this, new EventArgs());
        return DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
    }
您的活动:

void TimeShowEvent(object sender, EventArgs e)
    {
        textBox2.Text = timeString;  //this is your local variable
    }
您的事件处理程序需要位于客户机中。wcf服务将不知道调用它或使用其结果的事件的任何信息。

哦,我明白了。谢谢。:)然而,服务是否有办法与客户进行回话?例如,一个聊天应用程序,一个客户端将字符串传递给服务,而服务将字符串传递给另一个客户端。@Rob:有一些“双工”绑定(例如,
wsDualHttpBinding
)-但我不知道它们的工作原理和可靠性(我自己从来没有使用过)