Click here to Skip to main content
15,880,796 members

Use Form Controls Into WCF Service

arashmobileboy asked:

Open original thread
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);
     }
  }
}
Tags: C#, WCF, Windows Forms

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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