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);
}
}
}