呼叫几次后呼叫服务超时



我正在开发一个WCFService (c#),这是外部公司提供的外部web服务的"包装器"。myService和exterService之间的连接在开发机器和我的客户端提供的测试服务器上像一个魅力一样工作-问题开始在生产服务器上-当我上传我的服务并从网站调用它时,它只工作了很少的调用,而外部服务开始抛出超时-在我的原始代码中,我使用using指令,但在阅读了几个类似的问题线程后,我已经将其更改为使用。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;
        }
 }

和一些呼叫后,我只是开始超时。

我真的看不出你的代码有什么问题,一件小事,虽然它不应该重要,因为你每次做一个新的客户端,但我不确定我有一个静态方法调用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.");
        }
    }
}

相关内容

  • 没有找到相关文章