开发者

How to correctly open WCF service

I'm using WCF client like this...

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

but from time to time I get 500 HTTP error which is the only answer I get for next 15 minutes, then everything is back to normal for 15 minutes and so on. I know there is some load balan开发者_如何转开发cing stuff going on service side but guys there can't find any problems with it.

That's why I started wondering am I using WCF service correctly. I already made a mistake once when I was using "using" to close service connection and I'm afraid I doing something wrong again.

So can anybody say whether my way of calling WCF service is correct or not in all (event the most rare) circumstances?


I would suggest:

var client = new TestClient();
try
{    
    response = client.Operation(request);
    client.Close();
}
catch
{
    client.Abort();
}

The way you're doing, you're not aborting if something goes wrong, since your catch is inside the finally block. If you want to use the code you have today, I think you need to change it to:

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();
    }
}


Use this to enable trace logging on the service

  <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 may also help, also just trying to grab the WSDL through a web browser during those 15min.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜