【问题标题】:WCF: Self-host a NetTcp service, doesn't resolveWCF:自托管 NetTcp 服务,无法解析
【发布时间】: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)

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    好吧,我灵光一现。

    我使用 Windows 窗体(警钟)来“托管”服务。单击表单外,我使用了一些代码在单击按钮时调用服务(包括)。当然,服务不在自己的线程中,所以服务无法响应。

    我已经通过将服务容器(包含主机)放在它自己的线程中来修复它:

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

    Start() 方法设置 ServiceHost。

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

    希望对其他人有所帮助。

    【讨论】:

    • “接受”你自己的答案是可以解决问题的。
    • 谢谢,nitzamahone,我只需要等待 48 小时。 :)
    猜你喜欢
    • 2012-05-14
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多