0

I'm trying to name a server-client messaging application using wcf.

Here's the server part so far.

namespace server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)     /// once the form loads, create and open a new ServiceEndpoint.
        {
              ServiceHost duplex = new ServiceHost(typeof(ServerWCallbackImpl));
              duplex.AddServiceEndpoint(typeof(IServerWithCallback), new NetTcpBinding(), "net.tcp://localhost:9080/DataService");
              duplex.Open();
              this.Text = "SERVER *on-line*";
        }

        private void buttonSendMsg_Click(object sender, EventArgs e)
        {
            Message2Client(textBox2.Text); /// The name 'Message_Server2Client' does not exist in the current context :(
        }

        class ServerWCallbackImpl : IServerWithCallback /// NEED TO SOMEHOW MERGE THIS ONE WITH THE FORM1 CLASS
        {

            IDataOutputCallback callback = OperationContext.Current.GetCallbackChannel<IDataOutputCallback>();

            public void StartConnection(string name)
            {
                /// client has connected
            }

            public void Message_Cleint2Server(string msg)
            {
                TextBox1.text += msg; /// 'TextBox1' does not exist in the current context :(
            }

            public void Message2Client(string msg)
            {
                callback.Message_Server2Client(msg);
            }
        }

        [ServiceContract(Namespace = "rf.services",
            CallbackContract = typeof(IDataOutputCallback),
            SessionMode = SessionMode.Required)]
        public interface IServerWithCallback           ///// 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 IDataOutputCallback          ///// what goes from the sertver, to the client.
        {
            [OperationContract(IsOneWay = true)]
            void AcceptConnection();

            [OperationContract(IsOneWay = true)]
            void Message_Server2Client(string msg);
        }
    }
}

I just can't figure out, how do I merge "class Form1:Form" and "class ServerWCallbackImpl : IServerWithCallback", so that I would be able to induce the Message2Client function from a buttonclick, as well as add TextBox1.text += msg when the *Message_Cleint2Server* happens.

Thanks!

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
Roger
  • 6,443
  • 20
  • 61
  • 88

2 Answers2

1

What is the Need for merging why can't You Use Inheritance?

Solving Multiple Inheritance in C#.net

Click this for more details (stackoverflow)

i think that will give you the answer

Community
  • 1
  • 1
Nighil
  • 4,099
  • 7
  • 30
  • 56
  • You can't inherit from two classes at once. `Form1` already inherits `Form`. – Dan Puzey Apr 09 '11 at 08:41
  • 2
    Extension methods are the accepted answer on the question you've linked. This is not a good solution for combining a business class (the server logic) with UI (the form)! – Dan Puzey Apr 09 '11 at 09:01
1

You don't need to merge your class with the form: you need to create an instance of it. Something like this:

IServerWCallback server = new ServerWCallbackImpl();
server.Message2Client("hello world");

However (from the structure of your code so far), you'll probably need to have created an instance of the class earlier. This allows you to connect that instance and keep it around for later operation.

You may also want to read the MSDN pages on classes and objects (instances of classes) to make sure you've understood the concepts fully before you continue - this stuff is pretty fundamental to .NET programming.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • didn't really understand, how do I make this instance of IServerWCallback? :( – Roger Apr 09 '11 at 10:54
  • @Roger: The line `IServerWCallback server = new ServerWCallbackImpl()` creates a new instance of `ServerWCallbackImpl`. However, it'll only last as long as your one method, so probably you want to create it outside of the method (so that you don't drop your connection immediately). I'm afraid if you're not comfortable with classes and interfaces, you're going to struggle with C# development as a whole: this is basic concepts. I'd suggest you pick up some of the MSDN introductory articles that'll help explain this all much better, and maybe come back to your messaging application later. – Dan Puzey Apr 09 '11 at 16:16
  • Thanks for your suggestion! I've read about objects and classes. – Roger Apr 09 '11 at 16:41
  • IServerWCallback server = new ServerWCallbackImpl(); server.Message2Client("hello world"); gives an error with this line "IDataOutputCallback callback = OperationContext.Current.GetCallbackChannel();", suggesting to use "new" to create an instance. – Roger Apr 09 '11 at 16:42
  • yet if i add this "new" it shows "Error 1 'System.ServiceModel.OperationContext.Current' is a 'property' but is used like a 'type'"... – Roger Apr 09 '11 at 16:44
  • So I guess the best way would be create an instance of that ServerWCallbackImpl() on an event, when someone tries to connect to the server and store this instances in a list, so like every connection would have its own object... am I right? – Roger Apr 09 '11 at 16:46