【问题标题】:Sending large data to wcf service returns me "504 (Gateway Time-out) error"将大数据发送到 wcf 服务返回“504(网关超时)错误”
【发布时间】:2018-12-27 07:39:35
【问题描述】:

我试图详细描述我的问题。我们的目标是将对象发送到 wcf 服务,该服务被处理并用另一个对象响应。如果我们发送 1000、2000 个对象,我们就有一个成功的结果。但是当我们增加对象的大小比如 10.000、20.000 时,服务器等待 1-1.30 分钟然后响应 504 网关错误。 首先,在客户端,我们上传一个文件,然后将其读取到服务器端。我们初始化读取数据的对象并将这个对象发送给服务。我的控制器喜欢:

public ActionResult SaleNotification(NOTIFICATION_OBJECT notificationItem, HttpPostedFileBase files)
{

    if (files != null)
    {
        List<NOTIFICATION_ITEM_OBJECT> fileItems = new List<NOTIFICATION_ITEM_OBJECT>();
        using (var package = new StreamReader(files.InputStream))
        {
            package.ReadLine();
            while (!package.EndOfStream)
            {

                var values = package.ReadLine().Split(';');
                if (values[0] != "" && values[1] != "" && values[2] != "" && values[3] != "")
                {
                    fileItems.Add(new NOTIFICATION_ITEM_OBJECT
                    {
                        GT = values[0],
                        SN = values[1].ToUpper(),
                        BN = values[2].ToUpper(),
                        XD = values[3]
                    });
                }
            }
        }
        notificationItem.NOTIFICATION_ITEMS.AddRange(fileItems);
    }
    var res = _serviceRepository.SendNotification(Security.Decrypt(SERVICE_PASS_ENCRYPT, true), notificationItem);
    return PartialView("PartialResult", res);

}

向服务部分发送通知,我试图一次发送整个对象,也许那部分我的方法是错误的。这是我的代码:

    public NOTIFICATION_RESULT_OBJECT SendNotification( string password, NOTIFICATION_OBJECT notification)
    {
        var res = new NOTIFICATION_RESULT_OBJECT();
        try
        {
            var client = new serviceClient();
            if (client.ClientCredentials != null)
            {
                client.ClientCredentials.UserName.Password = password;        
            }

            using (new OperationContextScope(client.InnerChannel))
            {
                #region header
                var requestMessage = new HttpRequestMessageProperty();   
                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                #endregion

               //Some codes service request initialize

                ServicePointManager.ServerCertificateValidationCallback = delegate (object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                var response = client.notify(serviceRequest);

                res.NOTIFICATION_ID = response.NOTIFICATIONID;
                foreach (var item in response)
                    res.PRODUCT_RESULT_LIST.Add(new PRODUCT_RESULT()
                    {
                        GT = item.GT,
                        RC = item.RC,
                        SN = item.SN,
                        BN = item.BN,
                        XD = item.XD.ToString("dd/MM/yyyy", new CultureInfo("tr-TR")),
                    });
                return res;
            }

        }
        catch (Exception ex)
        {

            Log4NetFunction.InsertLog(ex);
            return res;
        }
    }
}

我的绑定有关于 maxReceivedMessageSize、sendTimeout、receivedTimeout 等的配置。这是我的一个绑定示例:

<binding name="serviceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"  openTimeout="00:10:00" 
             closeTimeout="00:10:00"  sendTimeout="00:15:00" receiveTimeout="00:10:00">
      <security mode="Transport">
        <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="" />
        <message clientCredentialType="Certificate" algorithmSuite="Default" />
      </security>
    </binding>

在我的调试中,我找不到任何错误,它在本地成功发送 20000 个对象,或者服务器上没有关于此错误的日志。所以我找不到任何解决方案。我怎么解决这个问题?我的方法错了吗?

【问题讨论】:

    标签: asp.net-mvc wcf


    【解决方案1】:

    也许您的服务拒绝序列化具有这么多数据成员的数据。

    请尝试设置绑定的 readerQuotas 属性。

    <binding  allowCookies="false"  maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647"   openTimeout="00:10:00" 
             closeTimeout="00:10:00"  sendTimeout="00:15:00" receiveTimeout="00:10:00"  >
          <readerQuotas maxArrayLength="2147483647" maxNameTableCharCount="2147483647"
              maxStringContentLength="2147483647" maxDepth="2147483647"
              maxBytesPerRead="2147483647" />
          <security mode="None" />
        </binding>
    

    【讨论】:

    • 感谢您的回答。发生此错误是因为等待太久才能得到答案。我决定使用不同的方法来解决它。但添加 readerQuotas 也有助于发送消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2016-11-20
    • 2015-10-20
    • 2011-04-08
    • 2017-06-16
    相关资源
    最近更新 更多