Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF WinForm
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 :
       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:
       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):
       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) :
     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 1-Oct-12 12:07pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
arashmobileboy at 2-Oct-12 5:17am
   
thanks but it did not help
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 7 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100