我正在研究一位同事在通过Visual Studio 2010运行应用程序时遇到的异常:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=mscorlib
StackTrace:
at System.Collections.Generic.GenericEqualityComparer`1.Equals(T x, T y)
at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue& value)
at xxxxxxx.xxxxxxx.xxxxxxx.RepositoryBase`2.GetFromCache(TIdentity id)
Run Code Online (Sandbox Code Playgroud)
使用.NET Reflector,我查看了代码GenericEqualityComparer<T>.Equals(T x, T y),我看不出任何可能的原因NullReferenceException.
//GenericEqualityComparer<T>.Equals(T x, T y) from mscorlib 4.0.30319.269
public override bool Equals(T x, T y)
{
if (x != null)
{
return ((y != null) && x.Equals(y));
}
if (y != null)
{
return false;
} …Run Code Online (Sandbox Code Playgroud) 当我需要能够取消大型/长期运行工作负载的任务时,我经常使用与此类似的模板执行任务:
public void DoWork(CancellationToken cancelToken)
{
try
{
//do work
cancelToken.ThrowIfCancellationRequested();
//more work
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
Log.Exception(ex);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
不应将OperationCanceledException记录为错误,但如果任务要转换为已取消状态,则不得吞下OperationCanceledException.除了此方法的范围之外,不需要处理任何其他异常.
这总觉得有点笨重,默认情况下visual studio会在OperationCanceledException的中断(虽然因为我使用了这种模式,我已经'关闭了User-unhandled'现在关闭了OperationCanceledException).
理想情况下,我认为我希望能够做到这样的事情:
public void DoWork(CancellationToken cancelToken)
{
try
{
//do work
cancelToken.ThrowIfCancellationRequested();
//more work
}
catch (Exception ex) exclude (OperationCanceledException)
{
Log.Exception(ex);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
即将某种排除列表应用于捕获但没有当前不可能的语言支持(@ eric-lippert:c#vNext feature :)).
另一种方式是通过延续:
public void StartWork()
{
Task.Factory.StartNew(() => DoWork(cancellationSource.Token), cancellationSource.Token)
.ContinueWith(t => Log.Exception(t.Exception.InnerException), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
}
public void DoWork(CancellationToken cancelToken)
{
//do …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的NHibernate映射编写一个测试,它将自动获取并测试任何新添加的映射.
目前,我有一个测试打开一个已知测试数据库的会话,然后尝试加载每个类型的第一个实体并断言它不是null.
这一切都很好,但这意味着每次添加新的实体映射时,我都需要记住更新测试.
所以,我想要做的是检查映射并尝试加载每个映射实体中的一个,但是我的测试看不到构建sessionfactory的NHibernate Configuration对象,所以我想知道是否有办法从会话中访问映射实体的列表,还是需要公开原始配置?