2013-01-12 185 views
2

什麼是由控制檯應用程序託管的WCF服務可以與所述控制檯應用程序進行交互以執行Console.WriteLine()即最簡單的方式。控制檯應用程序主機wcf服務交互

一些代碼:

合同:

[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(); 
    } 
} 
+2

只是'Console.WriteLine'怎麼樣? –

+0

確定nvmd,正在進行的事情讓我錯了,你是正確的只是Console.WriteLine做到了......對不起,時間 – RMiranda

回答

1

以下是運行主機的一個例子,客戶所在地t可以在控制檯上記錄消息。以你爲例,我不確定你爲什麼設置IsOneWay=true。對於這個特定的情況,一種方法不是你想要的。另外,我在下面的例子中使用了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"); 
    } 
} 
+1

OP說在'Alert'操作中做'Console.WriteLine'不起作用。也許它只適用於你的情況,因爲你的客戶端和服務在同一個進程中。 –

+0

好吧,我很愚蠢,我得到一個錯誤,但沒有看到它因爲單向。 @JohnSaunders像你之前說過的,我試過了,只需在Alert操作中做Console.WriteLine就行了......對不起浪費時間的人。 – RMiranda

1

咄,是我不好,只是打電話從警戒動作console.writeline這樣做,我被單向上當,並沒有得到一個錯誤......所以這是沒有更多的oneways我...

+2

他是作者。 –

相關問題