【问题标题】:Self hosting wcf with nettcp exception带有 nettcp 异常的自托管 wcf
【发布时间】:2012-01-18 00:50:11
【问题描述】:

每当尝试连接到我的自托管 wcf 时,我都会遇到超时异常。

这是我的配置:

<configuration>
  <system.serviceModel>        
    <behaviors>
        <serviceBehaviors>
            <behavior name="MetaBehaviour">
                <serviceMetadata />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="MetaBehaviour" name="WcfStreaming.LargeDataService">
            <clear />
            <endpoint address="net.tcp://localhost/LargeData" binding="netTcpBinding"
                name="Tcp" contract="WcfStreaming.ILargeDataService" listenUriMode="Explicit" />
            <endpoint address="net.tcp://localhost/LargeData/mex" binding="mexTcpBinding"
                name="TcpMex" contract="IMetadataExchange" listenUriMode="Explicit" />
            <endpoint address="http://localhost/wcfstreaming" binding="basicHttpBinding"
                bindingConfiguration="" name="BasicHttp" contract="WcfStreaming.ILargeDataService" />
        </service>
     </services>
  </system.serviceModel>
</configuration>

这里是托管代码:

 host = new ServiceHost(typeof(WcfStreaming.LargeDataService),
           new Uri[] { HttpUri, TcpUri });

 host.Open();

 var factory = new ChannelFactory<WcfStreaming.ILargeDataService>(new NetTcpBinding(), new EndpointAddress(TcpUri)); //  Hnew NetTcpBinding(), new EndpointAddress(TcpUri));

 srvChannel = factory.CreateChannel();

 using (OpenFileDialog dlg = new OpenFileDialog())
 {
    if (dlg.ShowDialog() == DialogResult.OK)
    {
       Stream str = srvChannel.GetFile(dlg.FileName); //Exception here

       StreamReader sr = new StreamReader(str);
       string bf = sr.ReadToEnd();
       File.WriteAllText(@"C:\test", bf);
    }
 }

我很讨厌:

发送到 net.tcp://localhost/LargeData 的请求操作在配置的超时 (00:01:00) 内没有收到回复。分配给此操作的时间可能是较长超时的一部分。这可能是因为服务仍在处理操作,或者因为服务无法发送回复消息。请考虑增加操作超时(通过将通道/代理转换为 IContextChannel 并设置 OperationTimeout 属性)并确保服务能够连接到客户端。

【问题讨论】:

  • 尝试在服务器上启用跟踪,您可能会在跟踪处获得比在客户端更好的异常。
  • 好吧,您似乎在发送大量数据,而且您的命名让人相信您可能也想使用流式传输......也许超时太短了 - 你有吗尝试将超时增加到例如3 分钟(而不是 1 分钟)??
  • 我注意到在 VS 下运行很好:内置 wcf 客户端在 .../mex 上运行。那么我应该手动将 mex 端点添加到服务主机吗?
  • carlosfigueira,追踪?怎么做?

标签: .net wcf


【解决方案1】:

您可以通过将以下内容放入您的 app.config 文件(调用 wcf 服务的项目,而不是 wcf 服务 app.config 文件)中来添加 wcf 跟踪。跟踪被转储到 c:\wcf.svclog

<system.diagnostics>
      <sources>
        <source name="System.ServiceModel"
                switchValue="Information, Warning, ActivityTracing, Error, Critical"
                propagateActivity="true">
          <listeners>
            <add name="traceListener"
                type="System.Diagnostics.XmlWriterTraceListener"
                initializeData= "c:\wcf.svclog" />
          </listeners>
        </source>
      </sources>
    </system.diagnostics>

此外,如果它在 VS 下运行,但不在 VS 之外,这意味着您正在创建的服务主机与 app.config 文件中定义的服务端点不匹配(同样是调用 wcf 的项目的配置文件,而不是wcf 的 app.config 文件)。或者 servicehost 实例化代码永远不会运行。

它在 VS 下运行,因为 VS 默认会创建一个 WCF 调试主机,该主机会加载您的 wcf 服务并公开该服务和 MEX url。有时,如果您的应用程序崩溃,此调试辅助工具会继续运行。进程是wcfsvchost.exe。

“Mex”允许您获取服务上的元数据,因此最终程序不需要它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2016-10-11
    相关资源
    最近更新 更多