0

hiii I am new to WCF and I have written a code in Console application. I have created a service like this

[ServiceContract]
public interface IHelloService
{
    [OperationContract]
    void SayHello(string msg);
}

and define the function

public class HelloService: IHelloService 
{
    public void SayHello(string msg)
    {
       Console.WriteLine("I rec message : " + msg); 

    }
}

and I am starting service from main program file

static void Main(string[] args)
{
        Console.WriteLine("******* Service Console *******");
        using(ServiceHost host = new ServiceHost(typeof(HelloWcfServiceLibrary.HelloService)))
        {

            host.AddServiceEndpoint(typeof(IHelloService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloWcfService");
            host.Open();
            Console.Read();
        }
 }

and at client side the code is

 static void Main(string[] args)
 {
        IHelloService proxy = ChannelFactory<IHelloService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloWcfService"));
        string msg;
        while (true)
        {
            msg = Console.ReadLine();
            msg = proxy.SayHello(msg);
            Console.WriteLine("Server returned " + msg);
        }
  }

it is working properly but I want to do the same thing in Windows Form Application and show the received data i richtextbox but I dont know how to do that. Please Someone help me

1 Answers1

0

It's just the same as what you did in console application. You can start a ServiceHost in Load method, but one difference is that RichTextbox can only be access in GUI thread, so you may have to save the GUI SynchronizationContext somewhere and when you want to output something to that rich textbox, you need to call Post method or send on the SynchronizationContext, like:


public class HelloService: IHelloService {    
private SynchronizationContext context;
private RichTextbox textbox;
public void SayHello(string msg)   
{       
context.Post((obj) => textbox.Add("I rec message : " + msg));
}
}

Note: this just shows a sample, it may not work.

Jack
  • 717
  • 5
  • 4