开发者

a little help instancing a wcf object, events don't work

开发者 https://www.devze.com 2023-02-23 03:59 出处:网络
Still struggling with the server side of my wcf application. :) As you can see from the code, I am trying to trigger an event whenever a client makes StartConnection. But somehow seems I can\'t insta

Still struggling with the server side of my wcf application. :)

As you can see from the code, I am trying to trigger an event whenever a client makes StartConnection. But somehow seems I can't instance the server object right, as it gives me this 3 error.

Please suggest, what would be the right way to do that? Thanks! :)

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          //开发者_JAVA百科/// 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);
    }

}


you are creating myserver as an instance of IfaceClient2Server which does not have a definition for the event or clientname string. Should be

ServerClass myServer = new ServerClass();

You will still have access the the interface methods as ServerClass inherits from IfaceClient2Server.

0

精彩评论

暂无评论...
验证码 换一张
取 消