我正在尝试在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/securessionbrokertcp
在中没有收到答复
配置超时(00:01:00)。这个
分配给此操作的时间可以
已经是一个较长的一部分
超时。这可能是因为
服务仍在处理
操作或因为服务
无法发送答复消息。
请考虑增加
操作超时(通过强制转换
到IContextChannel和的通道/代理
设置OperationTimeout属性)
并确保服务能够
连接到客户端。
所以我想它不能解决服务问题。
我哪里出错了?如何通过TCP测试服务的存在性?我使用了svctraceviewer,我只收到了相同的消息,所以没有消息。
我希望向用户请求该服务的URL,因此他们将输入“net.tcp://localhost:8080”或其他内容,然后将其用作对SecureSessionBroker(和其他)WCF服务的各种调用的基址…不使用app.config。
不幸的是,我能找到的所有示例都使用app.config。
有趣的是,我可以使用vs主机托管服务,并且客户机连接良好。(使用:
d:\dev2008\xxx\yyy.xxx.server>wcfsvchost.exe/service:bin/debug/yyy。
xxx.server.dll/config:app.config)