几次通话后调用服务超时

时间:2014-08-21 14:29:58

标签: c# web-services wcf timeout

我正在开发一个WCFService(C#),它是外部公司提供的外部Web服务的“包装器”。 myService和exterService之间的连接就像dev机器上的魅力以及我的客户端提供的测试服务器上的问题 - 生产服务器上的问题开始 - 当我上传我的服务并从网站调用它时,它可以用于几个调用,而不是外部服务开始抛出超时 - 在我使用指令的原始代码中,但在阅读了几个类似问题的线程后,我已将其更改为在服务客户端上使用.Close。不幸的是它没有帮助。你能帮我吗?

这是调用外部服务的代码。

public static ExternalResponse RunAskQuestionBasicQuery(ExternalRequest request, string innerTransId)
    {
        ExternalResponse resp = null;
        ExternalTestSoapClient client = new ExternalTestSoapClient("ExternalTestSoap12");

        Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient created.");
        try
        {
            Logger.GetInstance().Log(innerTransId, "AskQuestion() calling...");
            resp = client.AskQuestion(request.SearchNumber, User, Password);
            Logger.GetInstance().Log(innerTransId, "AskQuestion() called.");
            client.Close();
            Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient closed.");
        }
        catch (CommunicationException)
        {
            client.Abort();
            resp = null;
            SetError(bvResp, "External service is not accessible.");
            Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient aborted [CommunicationException].");
        }
        catch (TimeoutException)
        {
            client.Abort();
            resp = null;
            SetError(bvResp, "External service is not accessible.");
            Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient aborted [TimeoutException].");
        }
        catch (Exception ex)
        {
            Logger.GetInstance().Log(innerTransId, ex.Message);
            throw;
        }
 }

经过一些电话,我才开始超时。

1 个答案:

答案 0 :(得分:0)

我真的看不出你的代码有什么问题,但是一件小事虽然每次创建一个新客户都不重要,但我不确定我是否有静态方法调用WCF服务。

我使用这种模式与WCF运气最好 - 它非常接近你的只是将Close()移动到finally块

public ExternalResponse RunAskQuestionBasicQuery(ExternalRequest request, string innerTransId)
{
    ExternalResponse resp = null;
    ExternalTestSoapClient client = new ExternalTestSoapClient("ExternalTestSoap12");

    Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient created.");
    try
    {
        Logger.GetInstance().Log(innerTransId, "AskQuestion() calling...");
        resp = client.AskQuestion(request.SearchNumber, User, Password);
        Logger.GetInstance().Log(innerTransId, "AskQuestion() called.");           
        return resp;
    }
    catch (CommunicationException)
    {
        client.Abort();
        resp = null;
        SetError(bvResp, "External service is not accessible.");
        Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient aborted [CommunicationException].");
    }
    catch (TimeoutException)
    {
        client.Abort();
        resp = null;
        SetError(bvResp, "External service is not accessible.");
        Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient aborted [TimeoutException].");
    }
    catch (Exception ex)
    {
        Logger.GetInstance().Log(innerTransId, ex.Message);
        throw;
    }
    finally
    {
        if (client.State == CommunicationState.Opened)
        {
            client.Close();
            Logger.GetInstance().Log(innerTransId, "ExternalTestSoapClient closed.");
        }
    }
}