I have a Service. We are sending records to this service. However, when we send too many records (3,000) the Service times out. My idea was to break up the records and open the Service, then close it every 1,000 records.
However, I am getting an error:
{"Cannot access a disposed object.\r\nObject name: 'System.ServiceModel.Channels.ServiceChannel'."}
Here is my code:
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();
}
}
I created a sample console application that does this, but when i actually incorporated it into the actual project with the actual service, I get an error. Can you please help me figure out what I am doing wrong. It has to be my client.open and client.close, is my guess. Any help is greatly appreciated!!
I would try something more like this...
Note that you should always .Dispose()
the client as well. Also, if an error occurs, then .Close()
no longer works on the client, instead you have to .Abort()
it.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With