2

コンソールアプリケーションによってホストされているWCFサービスが、コンソールアプリケーションと対話して Console.WriteLine() を実行できる最も簡単な方法は何ですか?

いくつかのコード:

その契約:

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

サービス:

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

コンソール アプリ:

class Program
{
    static void Main(string[] args)
    {
        ServiceHost prodService = new ServiceHost(typeof(ProdsService));
        ServiceDescription serviceDesciption = prodService.Description;
        prodService.Open();
        Console.ReadLine();
    }
}
4

2 に答える 2

1

まあ、私の悪い、アラート操作からconsole.writelineを呼び出すだけでそれができます.onewayにだまされてエラーが発生していませんでした...だから、それは私にとってもうonewaysではありません...

于 2013-01-12T18:32:00.603 に答える
1

以下は、ホストがコンソールにメッセージを記録できるホストとクライアントを実行する例です。あなたの例では、なぜあなたが設定したのかわかりませんIsOneWay=true。この特定のケースでは、1 つの方法が必要なわけではありません。また、次の例では net.tcp バインディングを使用しています。他のバインディングでも機能するはずです。

基本的に、この例では、ユーザーのエントリがホスト サービスに送信され、コンソールにメッセージがエコーされます。

[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");
    }
}
于 2013-01-12T05:43:17.967 に答える