【发布时间】:2009-12-10 16:58:12
【问题描述】:
我正在尝试在 Windows 服务中使用 NetTcpBinding 托管 WCF 服务。 (我将把它用作 Web 和 Windows32 的各种客户端的 API)显然,我在将其放入 Windows 服务之前在测试主机中执行此操作。
我有以下合同:
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://localhost:8080/secureSessionBrokerTcp 内没有收到回复 配置超时 (00:01:00)。这 分配给此操作的时间可能 已经是较长时间的一部分 超时。这可能是因为 服务仍在处理中 操作或因为服务是 无法发送回复消息。 请考虑增加 操作超时(通过铸造 通道/代理到 IContextChannel 和 设置 OperationTimeout 属性) 并确保服务能够 连接到客户端。
所以我猜它无法解析服务。
我哪里错了?如何通过 TCP 测试服务是否存在?我使用了 SvcTraceViewer,我只是收到相同的消息,所以那里没有消息。
我更愿意向用户询问服务的 URL,以便他们输入“net.tcp://localhost:8080”或其他内容,然后将其用作对 SecureSessionBroker 的各种调用的 BaseAddress (和其他)WCF 服务......而不诉诸 App.config。
不幸的是,我能找到的所有示例都使用 App.config。
有趣的是,我可以使用 VS Host 托管服务,并且客户端连接良好。 (使用: D:\dev2008\xxx\yyy.xxx.Server>WcfSvcHost.exe /service:bin/debug/yyy。 xxx.Server.dll /config:App.config)
【问题讨论】: