打开/关闭Web服务
本文关键字:服务 Web 关闭 打开 | 更新日期: 2023-09-27 18:14:56
我有一个服务。我们正在向这个服务发送记录。但是,当我们发送太多记录(3000条)时,服务会超时。我的想法是将记录分解并打开服务,然后每1000条记录关闭一次。
然而,我得到一个错误:
{"Cannot access a disposed object.'r'nObject name: 'System.ServiceModel.Channels.ServiceChannel'."}
下面是我的代码:
ServiceClient client = new ServiceClient();
foreach (Record rc in creditTransactionList)
{
//if we are not on the last one...
if (currentTransCount < totalTransCount)
{
//Current batch count is less than 1,000
if (currentBatchCount <= amountPerBatch)
{
currentBatchCount++;
if (rc != null)
client.RecordInsert(rc);
}
//Current batch count is 1,000
if (currentBatchCount == amountPerBatch)
{
currentBatchCount = 0;
client.Close();
client.Open();
}
//Increment Total Counter by 1
currentTransCount++;
}
else
{
currentBatchCount++;
if (rc != null)
client.RecordInsert(rc);
client.Close();
}
}
amountPerBatch = 1000;
totalTransCount = ACHTransactionList.Count();
currentBatchCount = 0;
currentTransCount = 1;
foreach (Record rc in ACHTransactionList)
{
//if we are not on the last one...
if (currentTransCount < totalTransCount)
{
//Current batch count is less than 1,000
if (currentBatchCount <= amountPerBatch)
{
currentBatchCount++;
if (rc != null)
client.RecordInsert(rc);
}
//Current batch count is 1,000
if (currentBatchCount == amountPerBatch)
{
currentBatchCount = 0;
client.Close();
client.Open();
}
//Increment Total Counter by 1
currentTransCount++;
}
else
{
currentBatchCount++;
if (rc != null)
client.RecordInsert(rc);
client.Close();
}
}
我创建了一个示例控制台应用程序来执行此操作,但是当我实际将它与实际服务合并到实际项目中时,我得到了一个错误。你能帮我弄清楚我做错了什么吗?必须是我的客户。打开和客户端。我猜差不多。任何帮助都非常感谢!!
我会尝试这样做…注意,您也应该始终对客户机进行.Dispose()
。此外,如果发生错误,则.Close()
不再在客户端上工作,而必须.Abort()
它。
ServiceClient client = new ServiceClient();
try
{
foreach(...)
{
...
//Current batch count is 1,000
if (currentBatchCount == amountPerBatch)
{
currentBatchCount = 0;
client.Close();
client = new ServiceClient();
}
...
}
}
finally
{
if(client.State == CommunicationState.Faulted)
client.Abort();
else
client.Close();
}
client.Close
将处置该对象。client.Open
之后总是抛出错误。您需要使用new ServiceClient();
我所做的是检查客户端的状态并在必要时重置它:
if (wsClient.State.Equals(CommunicationState.Faulted) || wsClient.State.Equals(CommunicationState.Closed) || wsClient.State.Equals(CommunicationState.Closing))
{
wsClient = new ServiceClient();
}