gpt4 book ai didi

c# - WeakReference 返回错误的对象

转载 作者:行者123 更新时间:2023-12-03 16:17:41 32 4
gpt4 key购买 nike

我最近注意到我们的一个应用程序中出现了一种奇怪的行为。

Exception=System.InvalidCastException: Unable to cast object of type 'System.Data.SqlClient.SqlTransaction' to type 'System.Byte[]'.
at ServiceStack.Text.Pools.BufferPool.GetCachedBuffer(Int32 minSize) in C:\BuildAgent\work\912418dcce86a188\src\ServiceStack.Text\Pools\BufferPool.cs:line 55
at ServiceStack.Redis.RedisNativeClient..ctor(RedisEndpoint config) in C:\BuildAgent\work\b2a0bfe2b1c9a118\src\ServiceStack.Redis\RedisNativeClient_Utils.cs:line 447
at ServiceStack.Redis.RedisClient..ctor(RedisEndpoint config) in C:\BuildAgent\work\b2a0bfe2b1c9a118\src\ServiceStack.Redis\RedisClient.cs:line 66
at ServiceStack.Redis.RedisConfig.<>c.<.cctor>b__35_0(RedisEndpoint c) in C:\BuildAgent\work\b2a0bfe2b1c9a118\src\ServiceStack.Redis\RedisConfig.cs:line 22
at ServiceStack.Redis.RedisResolver.CreateRedisClient(RedisEndpoint config, Boolean master) in C:\BuildAgent\work\b2a0bfe2b1c9a118\src\ServiceStack.Redis\RedisResolver.cs:line 76
at ServiceStack.Redis.RedisManagerPool.GetClient() in C:\BuildAgent\work\b2a0bfe2b1c9a118\src\ServiceStack.Redis\RedisManagerPool.cs:line 214
...
或者
Exception=System.InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'System.Transactions.SafeIUnknown'.
at System.Transactions.Transaction.JitSafeGetContextTransaction(ContextData contextData)
at System.Transactions.Transaction.FastGetTransaction(TransactionScope currentScope, ContextData contextData, Transaction& contextTransaction)
at System.Transactions.Transaction.get_Current()
at System.Data.ProviderBase.DbConnectionPool.GetFromTransactedPool(Transaction& transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at ServiceStack.OrmLite.OrmLiteConnection.Open() in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteConnection.cs:line 86
...
我们应用程序的各个部分都会抛出与这两个类似的异常。所有这些异常的唯一共同点是 WeakReference 对象,但我看不出导致此异常的明显原因。例如,抛出此异常的 ServiceStack.Text 代码为:
    private class CachedBuffer
{
private readonly WeakReference _reference;

public int Size { get; }

public bool IsAlive => _reference.IsAlive;
public byte[] Buffer => (byte[])_reference.Target;

public CachedBuffer(byte[] buffer)
{
Size = buffer.Length;
_reference = new WeakReference(buffer);
}
}
从Buffer属性getter抛出异常,显然_reference.Target指向SqlTransaction对象而不是byte[],但是_reference在构造函数中只初始化一次,之后不能改变,那么怎么抛出这个异常呢?
此外,这些代码最近都没有改变,所以它突然开始抛出错误是没有意义的。我也看不出我们可能通过代码中的某些更改导致此错误的任何方式,或者我错了吗?
这可能是 .net clr 中的错误,如果是,我该如何诊断?我们的应用程序使用 .NET 4.8 框架,我们已经在测试环境中的 Windows server 2012 和 Windows server 2019 机器上看到了这些错误,但我无法在我的开发机器上本地重现它们。

最佳答案

我已经找到了问题所在,确实是我们自己的代码导致了这个错误,并且与反射有关。长话短说,我们的一位开发人员介绍了一段代码,该代码在 ExpandoObject.Keys 属性上调用了深度克隆。该属性不是简单的字符串集合,还包含对整个 ExpandoObject 的引用,并且在 ExpandoObject 的深处还有一些 WeakReference 字段。我仍然不明白到底发生了什么,但我猜在 ExpandoObject 中克隆那些 WeakReferences 以某种方式导致了我们遇到的错误。
谢谢你的帮助。我多次检查该代码,但完全错过了深度克​​隆调用,因为该扩展方法有一个非常通用的名称。

关于c# - WeakReference 返回错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67213321/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com