Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I Wrote a program that has two Console application(assusme A & B) and a WCF Service Library.
I connected A and B together so message can be transfer from A to B or B to A(WCF Duplex).
To Do this i added the reference of WCF Service Library to both A & B project.
Now I Want To Change these Console Applications To WinForm application but i cannot do that
because in console application we have console.writeline() that it Can be use in WCF Service Library But In WinForm,MessageBox Cannot be use in WCF Service.

Also I Cannot Add Reference Of A & B to Wcf Service Because it makes Circular Dependency Error

This Is The Project With Console Applications :

In Project A Program.cs file :
C#
 internal static void StartService()
{
   myServiceHost = new ServiceHost(typeof(WCF.MessageService));

   myServiceHost.Open();
}

internal static void StopService()
{
   if (myServiceHost.State != CommunicationState.Closed)
      myServiceHost.Close();
}

static void Main()
{
   StartService();
   Console.WriteLine("Service running; press return to exit");
   Console.ReadLine();
   StopService();
   Console.WriteLine("stopped");
}

In Project B Program.cs file:
C#
    class ClientCallback : IMyMessageCallback
 {
   public void OnCallback(string message)
   {
      Console.WriteLine("message from the server: {0}", message);
   }
 }

 class Program
 {
   static void Main()
   {
      Console.WriteLine("Client - wait for service");
      Console.ReadLine();

      WSDualHttpBinding binding = new WSDualHttpBinding();
      EndpointAddress address =
            newEndpointAddress("http://localhost:8731/Design_Time_Addresses/MessageService/Service1/");

      ClientCallback clientCallback = new ClientCallback();
       InstanceContext context = new InstanceContext(clientCallback);

      DuplexChannelFactory<IMyMessage> factory =
         new DuplexChannelFactory<IMyMessage>(context, binding, address);

      IMyMessage messageChannel = factory.CreateChannel();

      messageChannel.MessageToServer("From the client");

      Console.WriteLine("Client - press return to exit");
      Console.ReadLine();

   }
}


In IMYMessage.cs File (WCF Service Library):
C#
   public interface IMyMessageCallback
{
  [OperationContract(IsOneWay = true)]
  void OnCallback(string message);
}

[ServiceContract(CallbackContract = typeof(IMyMessageCallback))]
public interface IMyMessage
{
  [OperationContract]
  void MessageToServer(string message);
}

In MessageService.cs File(WCF Service Library) :
C#
 public class MessageService : IMyMessage
{
  public void MessageToServer(string message)
  {
     Console.WriteLine("message from the client: {0}", message);
     IMyMessageCallback callback =
           OperationContext.Current.
                 GetCallbackChannel<IMyMessageCallback>();

     callback.OnCallback("message from the server");

     new Thread(ThreadCallback).Start(callback);
   }

   private void ThreadCallback(object callback)
   {
     IMyMessageCallback messageCallback = callback as IMyMessageCallback;
     for (int i = 0; i < 10; i++)
     {
        messageCallback.OnCallback("message " + i.ToString());
        Thread.Sleep(1000);
     }
  }
}
Posted

Why not replace your console based logging with Log4Net[^]

You also want to have a look at WCF / WPF Chat Application[^] by Sacha Barber.

Best regards
Espen Harlinn
 
Share this answer
 
v2
 
Share this answer
 
Comments
arashmobileboy 2-Oct-12 5:17am    
thanks but it did not help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900