2

What's the most simple way that a WCF Service, that's being hosted by a console application , can interact with the said console application, to do a Console.WriteLine() ie.

Some code:

The contract:

[ServiceContract(Name = "IProdsService")]
public interface IProdsService
{
    [OperationContract(Name = "Alert",IsOneWay=true)]
    void Alert(string msg);
}

The Service:

public class ProdsService : IProdsService
{
    //IProdsService.Alert implementation
    public void Alert(string msg)
    {
        //TODO: Send Alert to Console Application!
    }
}

The Console App:

class Program
{
    static void Main(string[] args)
    {
        ServiceHost prodService = new ServiceHost(typeof(ProdsService));
        ServiceDescription serviceDesciption = prodService.Description;
        prodService.Open();
        Console.ReadLine();
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
RASMiranda
  • 371
  • 6
  • 19

2 Answers2

1

Following is an example of running host and client where host can log message on the console. On your example, I am not sure why you have set IsOneWay=true. For this specific case, one way is not what you want. Also, I have used net.tcp binding in the following example; it should work with any other binding as well.

Basically in the example, user's entry is sent to the host service which echos the message on the console.

[ServiceContract]
public interface IProdsService
{
    [OperationContract]
    void Alert(string msg);
}

/// <summary>
/// Host Class
/// </summary>
public class ProdsService : IProdsService
{
    public ProdsService()
    {
        Console.WriteLine("Service instantiated.");
    }

    public void Alert(string msg)
    {
        Console.WriteLine(msg);
    }
}

/// <summary>
/// Client proxy wrapper
/// </summary>
public class ProdsServiceClient : ClientBase<IProdsService>, IProdsService
{
    public ProdsServiceClient()
    {
    }

    public ProdsServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
        base(binding, remoteAddress)
    {
    }

    public void Alert(string msg)
    {
        base.Channel.Alert(msg);
    }
}

class Program
{
    static ManualResetEvent _reset;

    static void Main(string[] args)
    {
        string host = "localhost";
        int port = 8888;

        //ManualResetEvent is used for syncing start/stop of service.
        _reset = new ManualResetEvent(false);

        var action = new Action<string, int>(Start);
        var result = action.BeginInvoke(host, port, null, null);

        //Wait for svc startup, this can be synced with resetEvents.
        Thread.Sleep(2000);

        //Create a client instance and send your messages to host
        using (var client = new ProdsServiceClient(new NetTcpBinding(), new EndpointAddress(string.Format("net.tcp://{0}:{1}", host, port))))
        {
         client.Alert("Test message");

         string msg = string.Empty;
         do
         {
            Console.Write("Type a message to send (X to exit): ");
            msg = Console.ReadLine();
            client.Alert(msg);
         }
         while (!msg.Trim().ToUpper().Equals("X"));
        }

        //Signal host to stop
        _reset.Set();
        action.EndInvoke(result);

        Console.Write("Press any to exit.");
        Console.ReadKey();
    }

    static void Start(string host, int port)
    {
        string uri = string.Format("net.tcp://{0}:{1}", host, port);
        //var server = new ProdsService();
        ServiceHost prodService = new ServiceHost(typeof(ProdsService));
        prodService.AddServiceEndpoint(typeof(IProdsService), new NetTcpBinding(), uri);
        Console.WriteLine("Service host opened");
        prodService.Open();

        //Wait until signaled to stop
        _reset.WaitOne();
        Console.WriteLine("Stopping host, please wait...");
        prodService.Close();
        Console.WriteLine("Service host closed");
    }
}
loopedcode
  • 4,863
  • 1
  • 21
  • 21
  • 1
    The OP says doing `Console.WriteLine` in the `Alert` operation does not work. Maybe it only works in your case because you have the client and the service in the same process. – John Saunders Jan 12 '13 at 17:08
  • ok i'm stupid, i was getting a error but did not see it beacause of the one way. @JohnSaunders like you said before, and i tried, simply doing the Console.WriteLine in the Alert operation does work...sorry for the wasting your time guys. – RASMiranda Jan 12 '13 at 18:27
1

duh, my bad, just calling console.writeline from the alert operation does it, i was being fooled by the oneway and was not getting an error...so that's no more oneways for me...

RASMiranda
  • 371
  • 6
  • 19