2013-01-12 115 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

他是作者。 –

相关问题