Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确打开WCF服务_Wcf - Fatal编程技术网

如何正确打开WCF服务

如何正确打开WCF服务,wcf,Wcf,我正在使用WCF客户端,就像这样 var client = new TestClient(); try { response = service.Operation(request); } finally { try { if (client.State != CommunicationState.Faulted) client.Close(); } catch (Exception) {

我正在使用WCF客户端,就像这样

var client = new TestClient();
try
{    
    response = service.Operation(request);
}
finally
{    
    try
    {
        if (client.State != CommunicationState.Faulted)
            client.Close();
    }
    catch (Exception)
    {
        client.Abort();
    }
}
但有时我会收到500个HTTP错误,这是我在接下来的15分钟内得到的唯一答案,然后在15分钟内一切都恢复正常,以此类推。我知道服务端有一些负载平衡的东西,但那个里的人并没有发现任何问题

这就是为什么我开始怀疑我是否正确使用了WCF服务。当我使用“using”关闭服务连接时,我已经犯过一次错误,我担心我又做错了什么


那么,有人能说出我调用WCF服务的方式在所有(最罕见的情况下)情况下是否正确吗?

使用此选项可以在服务上启用跟踪日志记录

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


Fiddler也可能会有所帮助,也只是在这15分钟内尝试通过web浏览器获取WSDL。

使用此功能可以在服务上启用跟踪日志记录

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

Fiddler可能也会有所帮助,也只是在这15分钟内尝试通过web浏览器获取WSDL。

我建议:

var client = new TestClient();
try
{    
    response = client.Operation(request);
    client.Close();
}
catch
{
    client.Abort();
}
按照你的方式,如果出现问题,你不会放弃,因为你的捕获在finally块内。如果您想使用今天的代码,我认为您需要将其更改为:

var client = new TestClient();
try
{    
    response = client.Operation(request);
}
finally
{    
    try
    {
        if (client.State != CommunicationState.Faulted)
            client.Close();
        else
            client.Abort(); // Abort if the State is Faulted.
    }
    catch (Exception)
    {
        client.Abort();
    }
}
我建议:

var client = new TestClient();
try
{    
    response = client.Operation(request);
    client.Close();
}
catch
{
    client.Abort();
}
按照你的方式,如果出现问题,你不会放弃,因为你的捕获在finally块内。如果您想使用今天的代码,我认为您需要将其更改为:

var client = new TestClient();
try
{    
    response = client.Operation(request);
}
finally
{    
    try
    {
        if (client.State != CommunicationState.Faulted)
            client.Close();
        else
            client.Abort(); // Abort if the State is Faulted.
    }
    catch (Exception)
    {
        client.Abort();
    }
}

您是否检查了catch块中的异常?异常是“接收到的html/text而不是xml/text”。但这是因为IIS返回了“服务错误500”页面。是否检查了catch块中的异常?异常是“接收到的html/text而不是xml/text”。但这是因为IIS返回了“服务错误500”页面。