在实例化WCF对象方面有一点帮助,事件不起作用

本文关键字:一点 帮助 不起作用 事件 WCF 实例化 对象 方面 | 更新日期: 2023-09-27 17:49:42

仍然在wcf应用程序的服务器端挣扎。:)

正如您可以从代码中看到的,每当客户端使StartConnection时,我都试图触发一个事件。但不知何故,我似乎不能实例服务器对象的权利,因为它给了我这个3错误。

请建议,正确的方法是什么?谢谢!:)

namespace server2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            myServer.eventHappened += new EventHandler(eventFunction);
            // 'server2.IfaceClient2Server' does not contain a definition for 'eventHappened' and no extension method 'eventHappened' accepting a first argument of type 'server2.IfaceClient2Server' could be found (are you missing a using directive or an assembly reference?)
        }
        IfaceClient2Server myServer = new ServerClass();
        // The type or namespace name 'eventCaller' could not be found (are you missing a using directive or an assembly reference?)
        void eventFunction(object sender, EventArgs e)
        {
            label1.Text = myServer.clientName;
            // 'server2.IfaceClient2Server' does not contain a definition for 'clientName' and no extension method 'clientName' accepting a first argument of type 'server2.IfaceClient2Server' could be found (are you missing a using directive or an assembly reference?)
        }
    }


    class ServerClass : IfaceClient2Server
    {
        public event EventHandler eventHappened;
        //IfaceServer2Client callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();
        public string clientName;
        public void StartConnection(string name)
        {
            clientName = name;
            eventHappened(this, new EventArgs());
            MessageBox.Show(clientName + " has connected!");
        }
        public void Message_Cleint2Server(string msg)
        {
        }
        public void Message2Client(string msg)
        {
        }
    }


    [ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]

    public interface IfaceClient2Server           ///// what comes from the client to the server.
    {
        [OperationContract(IsOneWay = true)]
        void StartConnection(string clientName);
        [OperationContract(IsOneWay = true)]
        void Message_Cleint2Server(string msg);
    }

    public interface IfaceServer2Client          ///// what goes from the sertver, to the client.
    {
        [OperationContract(IsOneWay = true)]
        void AcceptConnection();
        [OperationContract(IsOneWay = true)]
        void RejectConnection();
        [OperationContract(IsOneWay = true)]
        void Message_Server2Client(string msg);
    }
}

在实例化WCF对象方面有一点帮助,事件不起作用

您正在创建myserver作为IfaceClient2Server的实例,该实例没有事件或clientname字符串的定义。应该是

ServerClass myServer = new ServerClass();

您仍然可以访问接口方法作为继承自IfaceClient2Server的ServerClass。