0

Suppose, I want to add a button in a WinForms WCF client such that, whenever a user pushes the button, a specific client on the other side would see a MessageBox saying Hello [user].

enter image description here

I have modified this program to have a DataGridView instead of the big TextBox. I also tried to raise the event up on double click of a DataGridView row.

I have done something like the following:

[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface ICallableForm
{
    [OperationContract]
    void ShowMessage();
}

[DataContract]
public class ChatUser
{
    //...

    [DataMember]
    public ICallableForm WinForm { get; set; }

    public override string ToString()
    {
        return this.Username;
    }
}

class MainForm : Form, ICallableForm
{
    // ...
    public void ShowMessage()
    {
        MessageBox.Show("Hello " + ___clientUser.Username);
    }
    // ...

   private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        int rowIndex = e.RowIndex;

        ChatMessage message = dataGridView1.Rows[rowIndex+1].Tag as ChatMessage;

        ChatUser user = message.User;

        ICallableForm form = user.WinForm;

        form.ShowMessage();

        string str = string.Empty;
    }

And, getting the following error:

enter image description here

enter image description here


user366312
  • 16,949
  • 65
  • 235
  • 452
  • search google for `wcf` chat application examples, The principles to the answer you're looking for will be there. – Jonathan Applebaum Apr 18 '19 at 17:41
  • @jonathana, I already linked a chat application here. By the way, the functionality I am talking about is absent in all of the applications available in the Internet. – user366312 Apr 18 '19 at 18:23
  • Is WCF is must? I have implemented such functionality in the past using `System.Net.Sockets` TcpClinet class. the other client was listening for a stream from the server using a while loop (common convention). maybe consider using this way to achieve that functionality. – Jonathan Applebaum Apr 18 '19 at 18:39
  • @jonathana, yes, WCF is a must. – user366312 Apr 18 '19 at 18:45
  • maybe that can help: https://stackoverflow.com/questions/5739501/how-can-a-wcf-service-raise-events-to-its-clients – Jonathan Applebaum Apr 18 '19 at 18:50

0 Answers0