2009-12-10 29 views
0

我想托管一个WCF服务,在Windows服务中使用NetTcpBinding。 (我打算将它用作Web和Windows32的各种客户端的API)显然,在将它放入Windows服务之前,我正在测试主机中执行此操作。WCF:自托管一个NetTcp服务,不解决

我有以下合约:

namespace yyy.xxx.Server.API.WCF 
{ 
    [ServiceContract] 
    public interface ISecureSessionBroker 
    { 
     [OperationContract] 
     string GetSessionToken(string username, string encryptedPassword, string clientApiKey, string clientAddress); 
    } 
} 

与下面的实现:

namespace yyy.xxx.Server.API.WCF 
{ 
    public class SecureSessionBroker : ISecureSessionBroker 
    { 
     #region ~ from ISecureSessionBroker ~ 

     public string GetSessionToken(string username, string encryptedPassword, string clientApiKey, string clientAddress) 
     { 
      return Guid.NewGuid().ToString(); 
     } 

     #endregion 
    } 
} 

我使用下面的代码(在一类/方法)托管WCF服务:

try 
{ 
    _secureSessionBrokerHost = new ServiceHost(typeof(SecureSessionBroker)); 
    NetTcpBinding netTcpBinding = new NetTcpBinding(); 
    _secureSessionBrokerHost.AddServiceEndpoint(typeof(ISecureSessionBroker), netTcpBinding, "net.tcp://localhost:8080/secureSessionBrokerTcp"); 
    int newLimit = _secureSessionBrokerHost.IncrementManualFlowControlLimit(100); 
    // Open the ServiceHost to start listening for messages. 
    _secureSessionBrokerHost.Open(); 

} 
catch (Exception ex) 
{ 
throw; 
} 

这里的关键是我不想依赖一个App.config文件。一切都必须以编程方式配置。当我运行这段代码时,服务似乎会“起来”并聆听。 (即我没有例外)

但是当我使用下面的客户端代码:

string secureSessionBrokerUrl = string.Format("{0}/secureSessionBrokerTcp","net.tcp://localhost/8080",url); 
EndpointAddress endpointAddress=new EndpointAddress(secureSessionBrokerUrl); 
System.ServiceModel.Channels.Binding binding = new NetTcpBinding(); 
yyy.xxx.Windows.AdminTool.API.WCF.SecureSessions.SecureSessionBrokerClient 
    client = new yyy.xxx.Windows.AdminTool.API.WCF.SecureSessions.SecureSessionBrokerClient(binding,endpointAddress); 
string sessionToken=client.GetSessionToken("", "", ""); // exception here 
MessageBox.Show(sessionToken); 

...我总是得到一个异常。目前,我得到:

发送到 的net.tcp此请求操作://本地主机:8080/secureSessionBrokerTcp 没有收到 配置的超时(〇时01分00秒)内作出答复。分配给此操作的 时间可能是 已超时的更长时间的一部分。这可能是因为 服务仍在处理 操作或因为服务是 无法发送答复消息。 请考虑增加 操作超时(通过强制 通道/代理转换为IContextChannel和 设置OperationTimeout属性) ,并确保该服务能够 连接到客户端。

所以我想它无法解析服务。

我哪里错了?如何测试TCP上的服务是否存在?我已经使用了SvcTraceViewer,我只是得到相同的消息,所以没有消息。

我希望向用户询问服务的URL,以便他们输入“net.tcp:// localhost:8080”或其他东西,然后将其作为BaseAddress用于各种调用SecureSessionBroker(和其他)WCF服务...而不诉诸于App.config。

不幸的是,我能找到的所有例子都使用App.config。

有趣的是,我可以使用VS主机托管服务,客户端连接正常。 (使用: d:\ dev2008 \ XXX \ yyy.xxx.Server> WcfSvcHost.exe /服务:斌/调试/ YYY xxx.Server.dll /config:App.config)

回答

1

好吧,它来了给我一点灵感。

我正在使用Windows窗体(警报铃声)来“托管”服务。点击表单后,我用一些代码在点击按钮时调用服务(包含)。当然,这项服务并不在自己的线程中,所以服务无法回应。

我已经把服务容器(包含主机)自己的线程内固定它:

Thread thread = new Thread(new ThreadStart(_serviceWrapper.Start)); 
thread.Start(); 

在start()方法设置ServiceHost的。

我错误地认为,虽然WCF服务主机将为传入请求创建线程,但它只会在它自己的非阻塞线程(即不是UI线程)中执行此操作。

希望它可以帮助别人。

+0

可以“接受”你自己的答案,以解决未答复的问题。 – nitzmahone

+0

谢谢,nitzamahone,我只需要等待48小时。 :) –

+0

5个小时以上... –