0

有时,在我的 xp 机器上,调用自动生成的客户端代理中的方法时会出现异常。我告诉调试器停止所有 clr 异常。现在有时当我调用以下命令时:

public MyStuff.Entities.Package GetPackageById(System.Guid sessionId, int packageId)
    {
        return base.Channel.GetPackageById(sessionId, packageId);
    }

我首先得到一个 InvalidOperationException: Collection was modified... 按 F10 会导致 FileLoadException 并带有以下消息:

无法加载文件或程序集“System.Runtime.Serialization.resources,Version=4.0.0.0,Culture=de-DE,PublicKeyToken=b77a5c561934e089”或其依赖项之一。操作在当前状态下是不合法的。(来自 HRESULT 的异常:0x80131509)

我确定该服务没有引发异常,因为它会显示为 FaultException。因为它是调用base.Channel.GetPackageById(sessionId, packageId)时抛出的InvalidOperationException我认为这不是我的错?

我正在慢慢地想尽办法消除或解决这个异常。

使用安装了 Windows 7 和 .NET 4.5 的开发人员机器时从未发生过这种情况。在 XP 上,这种情况大约会发生 4 次中的 1 次。

服务端的 GetPackageById 如下所示:

public Package GetPackageById(Guid sessionId, int packageId)
        {
            try
            {
                return DataProvider.Provider.GetPackagesByKey(packageId,null);
            }
            catch (Exception ex)
            {
                throw new FaultException<MySericeFault>(new MySericeFault(ex));
            }                        
        }

包类如下所示:

    [DataContract(IsReference = true)]
    [KnownType(typeof(MyApp.Entities.MachinePackage))]
    public partial class Package: INotifyPropertyChanged
    {
    private DateTime? _outDate;
    [DataMember]
    public DateTime? OutDate
    {
        get { return _outDate; }
        set
        {
            if (_outDate != value)
            {
                _outDate = value;
                OnPropertyChanged("OutDate");
            }
        }
    }

    private int _productId;
    [DataMember]
    public int ProductId
    {
        get { return _productId; }
        set
        {
            if (_productId != value)
            {
                _productId = value;
                OnPropertyChanged("ProductId");
            }
        }
    }
    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (_propertyChanged != null)
        {
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
        add { _propertyChanged += value; } 
        remove { _propertyChanged -= value; }
    }
    private event PropertyChangedEventHandler _propertyChanged;
    }
4

1 回答 1

2

这更像是一个实验,但太大而不能作为评论!

尝试创建一个执行此代码的新操作合约:

服务:

public Package GetPackageByIdTest(Guid sessionId, int packageId)
{
    return new Package { ProductId = packageId, OutDate = DateTime.Now };
}

Then create a console application that references your service and write something like this:

Client:

static void Main(string[] args)
{
    for (int tester = 0; tester < 2000; tester++)
    {
        using (var service = new ConsoleApplication1.ServiceReference1.Service1Client())
        {
            Package result = service.GetPackageByIdTest(Guid.NewGuid(), tester);
            Console.WriteLine(result.ProductId);
        }
    }

    Console.ReadKey();
    return;
}

Then, try running that on one of the known XP machines that fails and see if you get the same issue. If not it would suggest there is something going a miss in your DataProvider.Provider.GetPackagesByKey(...) method.

于 2013-02-19T16:17:59.793 回答