有点帮助实例化wcf对象,事件不起作用

时间:2011-04-10 17:31:39

标签: c# wcf events

仍然在我的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);
    }

}

1 个答案:

答案 0 :(得分:1)

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

ServerClass myServer = new ServerClass();

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