代码之家  ›  专栏  ›  技术社区  ›  Program.X

wcf:自承载nettcp服务,不解析

  •  0
  • Program.X  · 技术社区  · 16 年前

    我正在尝试在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)

    1 回复  |  直到 16 年前
        1
  •  1
  •   Program.X    16 年前

    好吧,我突然想到了灵感。

    我用一个Windows窗体(警铃)来“主持”服务。单击表单外的按钮,我使用了一点代码来调用服务(包括在内)。当然,服务不在自己的线程中,因此服务无法响应。

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

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

    start()方法设置服务主机。

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

    希望它能帮助别人。

    推荐文章