使用ServiceStack Client在与服务主机

时间:2015-05-22 18:11:24

标签: servicestack

我们已将代码部署到另一个环境,现在我们在日志中看到此异常,并希望尝试将问题缩小到环境或我的代码。

System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ServiceStack.ServiceClientBase.Send[TResponse](String httpMethod, String relativeOrAbsoluteUrl, Object request)
   at LO.Leads.Processor.ProcessorAppHost.<>c__DisplayClass23.<ConfigureRabbitMqServer>b__20(IMessage`1 m) in c:\repo\AppHost.cs:line 171

代码可以工作,并且在较低的环境中工作了几个月,但没有这个代码所做的负担。

经过一些阅读后,我发现this帖子有些类似,因为超时发生在x次使用之后,但我们正在以正确的方式使用客户端(处理响应和客户端)和该服务旨在为客户提供便利,例如IReturn

来自Windows服务的

代码:

mqServer.RegisterHandler<LeadInformationInfo>(m =>
{
    var messageBody = m.GetBody();
    var id = messageBody.CorrelationId;
    _log.InfoFormat("CorrelationId:{0} mqServer.RegisterHandler<LeadInformationInfo>", id);
    container.Resolve<IFrontEndRepository>().SaveMessage(m as Message);
    LeadInformationInfoResponse response;
    try
    {
        using (var client = new JsonServiceClient(container.Resolve<ISettingsFactory>().GetMasterSetting("ProcessorApi:baseUri")))
        {
            response = client.Post(messageBody);
        }
    }
    catch (WebServiceException webServiceException)
    {
        _log.ErrorFormat("CorrelationId:{0} webServiceException:{1}", id, webServiceException);
        _log.ErrorFormat("CorrelationId:{0} messageBody:{1}", id, messageBody.Dump());
        response = ((LeadInformationInfoResponse)webServiceException.ResponseDto);
        response.CorrelationId = id;
    }
    catch (Exception exception)
    {
        _log.ErrorFormat("CorrelationId:{0} exception:{1} body:{2}", id, exception, messageBody);
        return null;
    }
    var responseMessage = new Message<LeadInformationInfoResponse>(response)
    {
        ReplyId = m.Id
    };
    container.Resolve<IFrontEndRepository>().SaveMessage(responseMessage);
    _log.InfoFormat("CorrelationId:{0} mqServer.RegisterHandler<LeadInformationInfo> final", id);
    return response;
}, 8);
来自服务的

代码:

public object Post(LeadInformationInfo request)
{
    if (request == null) throw new ArgumentNullException("request");
    var profiler = Profiler.Current;
    using (profiler.Step("Direct API POST"))
    {
        if (_log.IsDebugEnabled)
            _log.DebugFormat("CorrelationId:{0} LeadInformationInfo request:{1}", request.CorrelationId, request.Dump());
        var id = request.CorrelationId;
        using (profiler.Step("Set defaults"))
        {
            request.SetDefaults();
            request.LeadApplicationInfo.FixBankData();    
        }
        PreprocessInfoResponse preprocessResponse;
        using (profiler.Step("Preprocess"))
        using (var service = ResolveService<PreprocessServices>())
        {
            var preprocessRequest = request.LeadApplicationInfo.ConvertTo<PreprocessInfo>();
            preprocessResponse = service.Get(preprocessRequest);
            if (_log.IsDebugEnabled)
                _log.DebugFormat("CorrelationId:{0} LeadInformationInfo preprocessResponse:{1}", id, preprocessResponse.Dump());
            request.LeadApplicationInfo.PopulateWithNonDefaultValues(preprocessResponse);
        }
        if (_log.IsDebugEnabled)
            _log.DebugFormat("CorrelationId:{0} LeadInformationInfo updated request:{1}", id, request.Dump());
        AddLeadResponse saveLeadResponse;
        using (profiler.Step("Save lead"))
        {
            saveLeadResponse = SaveLead(request);
        }

        var leadInformationResponse = new LeadInformationInfoResponse
        {
            CorrelationId = id,
            LeadId = saveLeadResponse.LeadId,
            IsValidLead = preprocessResponse.IsValidLead,
            ValidStatus = preprocessResponse.LeadStatus,
        };

        if (!preprocessResponse.IsValidLead)
        {
            var unprocessedLead = new UnprocessedLead
            {
                TenantId = request.TenantId,
                CorrelationId = id,
                LeadId = saveLeadResponse.LeadId,
                ValidStatus = preprocessResponse.LeadStatus,
                AgentQueue = preprocessResponse.AgentQueue,
            };
            _log.WarnFormat("CorrelationId:{0} unprocessedLead:{0}", id, unprocessedLead.Dump());
            using (var client = MessageFactory.CreateMessageQueueClient())
            {
                client.Publish(unprocessedLead);
            }
            if (_log.IsDebugEnabled)
                _log.DebugFormat("CorrelationId:{0} LeadInformationInfo unprocessed lead leadInformationResponse:{1}", id, leadInformationResponse.Dump());
            return leadInformationResponse;
        }

        var outboundInitiateCallInfo = new OutboundInitiateCallInfo
        {
            TenantId = request.TenantId,
            CorrelationId = id,
            LeadId = saveLeadResponse.LeadId,
            LeadPhoneNumber = request.LeadApplicationInfo.Phones.First(),
            AgentQueue = preprocessResponse.AgentQueue,
        };
        _log.InfoFormat("CorrelationId:{0} outboundInitiateCallInfo:{0}", id, outboundInitiateCallInfo.Dump());
        using (var client = MessageFactory.CreateMessageQueueClient())
        {
            client.Publish(outboundInitiateCallInfo);
        }
        if (_log.IsDebugEnabled)
            _log.DebugFormat("CorrelationId:{0} LeadInformationInfo outbound phone lead leadInformationResponse:{1}", id, leadInformationResponse.Dump());
        return leadInformationResponse;      
    }
}

请求和响应DTO:

public class LeadInformationInfo : IForTenant, IAudit, IReturn<LeadInformationInfoResponse>
{
    public Guid CorrelationId { get; set; }
    public LeadApplicationInfo LeadApplicationInfo { get; set; }
    public RequestInfo RequestInfo { get; set; }
    public long CreatedDate { get; set; }
    public long ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }
    public string TenantId { get; set; }
}

public class LeadInformationInfoResponse
{
    public Guid CorrelationId { get; set; }
    public bool IsValidLead { get; set; }
    public string LeadId { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
    public string ValidStatus { get; set; }
}

所以我的问题是如何诊断此错误以找到根本原因,如果代码是正确的?,或者是否有更好的方法使用ServiceStack与Web服务进行通信,从而摆脱Web请求对象完全?

谢谢你,Stephen

0 个答案:

没有答案