我想托管一个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)
可以“接受”你自己的答案,以解决未答复的问题。 – nitzmahone
谢谢,nitzamahone,我只需要等待48小时。 :) –
5个小时以上... –