Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打开/关闭Web服务_C#_Asp.net_Wcf_Web Services - Fatal编程技术网

C# 打开/关闭Web服务

C# 打开/关闭Web服务,c#,asp.net,wcf,web-services,C#,Asp.net,Wcf,Web Services,我有服务。我们正在将记录发送到此服务。但是,当我们发送太多记录(3000条)时,服务超时。我的想法是打破记录,打开服务,然后每1000条记录关闭一次 但是,我得到一个错误: {"Cannot access a disposed object.\r\nObject name: 'System.ServiceModel.Channels.ServiceChannel'."} 这是我的密码: ServiceClient client = new ServiceClient(); foreach (R

我有服务。我们正在将记录发送到此服务。但是,当我们发送太多记录(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();
    }
}
ServiceClient=newserviceclient();
foreach(在creditTransactionList中记录rc)
{
//如果我们不是最后一个。。。
if(currentTransCount如果(currentBatchCount我要做的是检查客户端的状态,并在必要时重置它:

if (wsClient.State.Equals(CommunicationState.Faulted) || wsClient.State.Equals(CommunicationState.Closed) || wsClient.State.Equals(CommunicationState.Closing))
                {
                    wsClient = new ServiceClient();
                }

client.Close
将处理该对象。
client.Open
将始终在此之后抛出错误。
您需要使用
newserviceclient();

初始化客户端,我会尝试类似的方法。。。 请注意,您也应该始终
.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();
}

我怀疑您的服务客户端是一次性的,请尝试为每个批创建一个新的。我尝试通过执行
ServiceClient client=newserviceclient()初始化客户端
但是,我再次遇到一个错误:
名为“client”的局部变量不能在此范围内声明,因为它将赋予“client”不同的含义,它已在“parent or current”范围内用于表示其他内容
仅供参考-您不能调用client.Dispose(),这是无效的语法,因为无法调用该方法。@dyslexicanaboko-这是不正确的。WCF客户端的类型为
System.ServiceModel.ClientBase
,它实现了
IDisposable
,因此具有
.Dispose()
method。这是该方法的MSDN页面:无意冒犯,但是去编写代码并尝试编译它,你会得到一个语法错误。这是我这么说的唯一原因。@dyslexicanaboko-啊,似乎当前
ClientBase
显式地实现了
Dispose
,所以你是正确的,它不能像在我的代码中那样被调用。我的代码示例基于我当时在生产中使用的一些东西,所以我很确定它过去是可以调用的。也许自从这个答案创建以来,Microsoft在过去3年中改变了
ClientBase
?如果我没记错的话,这是为.NET 3.5编写的。哦,是的,他们改变了很多。我很高兴尝试去理解在infrastructure将.Net framework从4升级到4.5之后,我的生产代码在没有发布任何版本的情况下神奇地崩溃了。MS将一堆名称空间从一个程序集转移到了另一个程序集,这使得您必须更新您的引用,无论您是否计划对其进行更新。这只是一些内容发生了变化的一个示例,但确实发生了很大的变化:D