0

我需要将大约 10,000 行数据发送到 WCF 服务,然后将其一一插入到数据库中。这个过程需要1个多小时。但是,从开始运行 1 小时后,我会收到一个未找到服务的异常,我认为这是由于超时造成的。但是,只要 Visual Studio 开发服务器继续运行,服务就会继续将此数据插入数据库。

我尝试在客户端和服务端将所有超时值设置为 2 小时,但这没有帮助。

服务的 web.config:

<bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IDService" allowCookies="true" maxBufferPoolSize="2147483647"
                         maxReceivedMessageSize="2147483647" closeTimeout="02:00:00"
                         openTimeout="02:00:00" receiveTimeout="02:00:00" sendTimeout="02:00:00">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                                  maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                                  maxNameTableCharCount="2147483647"/>
          <security mode="None"/>

        </binding>
      </basicHttpBinding>
    </bindings>

在客户端(在初始化服务客户端时)

    this.binding.ReceiveTimeout = TimeSpan.FromHours(2);
    this.binding.OpenTimeout = TimeSpan.FromHours(2);
    this.binding.SendTimeout = TimeSpan.FromHours(2);
    this.binding.CloseTimeout = TimeSpan.FromHours(2);

    this.binding.MaxBufferSize = int.MaxValue;
    this.binding.MaxReceivedMessageSize = int.MaxValue;

我做了一些搜索,在很多地方都建议增加 OperationTimeout。所以我尝试了它但没有任何成功。

((IContextChannel)base.Channel).OperationTimeout = new TimeSpan(2, 0, 0); 

请给出一些想法,我应该尝试什么,以便服务完成整个操作并将响应发送回客户端。另外,如果您认为有更好的方法来执行此操作,请告诉我。

4

2 回答 2

1

There are some options that you can try:

  1. Start writting in a different thread.

This way you can respond faster, telling that the data has been received successfully. Then you can ask the service if the data has been written yet - you might even be able to respond with how much have been written until now. At last you can get the response.

  1. Only send a few records at a time.

This way you can respond faster and you can show the progress.

  1. Optimize writting.

Two hours are a huge amount of time for writting only 10000 records. Maybe you should take a look at SqlBulkCopy and combine it with a DataTable, this can improve speed considerable, but can only be used for insert.

You can combine 1 and 2 with number 3.

Good luck with your quest.

于 2013-10-18T13:54:34.333 回答
1

I think your web service endpoint got killed because it did not respond to the app server in reasonable time. Second thing I would notice, you do something on a web service side for more than 2 hours. Not good solution at all.

In case if you process the data before save:

I sugest that you store all the data somewhere in DB and having another one Watchdog or "Job" or "Queue manager" Proess to get this job done in the background.

If this is not a case then I have a question:

Why does it take 2 hours to get the data saved?

于 2013-10-18T13:55:08.040 回答