wcf服务与回调失败,可能是并发问题

时间:2015-02-24 22:15:58

标签: c# wcf nettcpbinding duplex

我有双工wcf服务。客户端注册然后服务器调用回调。回调将参数作为字符串列表的列表。因为这可能非常大,所以列表被分成块并且回调被多次调用。

这是服务代码:

using Shared;
using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Timers;
namespace CallbackService.Server
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, MaxItemsInObjectGraph = int.MaxValue)]
    public class MyService : IMyService
    {
        public static IMyServiceCallback Callback;
        public static Timer Timer;
        public void OpenSession()
        {
            Console.WriteLine("> Session opened at {0}", DateTime.Now);
            Callback = OperationContext.Current.GetCallbackChannel<IMyServiceCallback>();
        Task.Factory.StartNew(() =>
        {
            GenerateListAndCallClient(null, null);
        });
    }
    public static string RandomStr()
    {
        string rStr = Path.GetRandomFileName();
        rStr = rStr.Replace(".", ""); // For Removing the .
        return rStr;
    }
    void GenerateListAndCallClient(object sender, ElapsedEventArgs e)
    {

        List<List<string>> theList = new List<List<string>>();
        for (int i = 0; i < 100; i++)
        {
            var innerList = new List<string>();
            theList.Add(innerList);
            for (int j = 0; j < 10000; j++)
            {
                innerList.Add(RandomStr());
            }
        }
        int start = 0;
        int count = 90;
        while(start + count < theList.Count)
        {
            lock (typeof(MyService))
            {
                try
                {
                    List<List<string>> currentList = theList.GetRange(start, count);
                    Callback.OnCallback(currentList);
                    start += count;
                }
                catch (ArgumentException) { 
                    //skip last chunk
                }
            }
        }   
    }
}

}

将计数设置为1,一切正常。 Callback在客户端上调用100次。将计数设置为90会产生异常:

An exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll but was not handled in user codeAdditional information: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:30'.

有什么想法吗?

0 个答案:

没有答案