我编写的代码如下:
public class ConferenceService
{
private static readonly IEventFactory _eventFactory = DI.Current.Resolve<IEventFactory>();
private static readonly IEventHandlerFactory _eventHandlerFactory = DI.Current.Resolve<IEventHandlerFactory>();
private readonly string nameOrConnectionString;
private readonly RetryPolicy<SqlAzureTransientErrorDetectionStrategy> retryPolicy;
public ConferenceService(string nameOrConnectionString = "ConferenceManagement")
{
this.nameOrConnectionString = nameOrConnectionString;
this.retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(new Incremental(5, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1.5)) { FastFirstRetry = true });
this.retryPolicy.Retrying += (s, e) =>
Trace.TraceWarning("An error occurred in attempt number {1} to access the database in ConferenceService: {0}", e.LastException.Message, e.CurrentRetryCount);
}
public void CreateConference(ConferenceInfo conference)
{
using (var context = new ConferenceContext(this.nameOrConnectionString))
{
var existingSlug = this.retryPolicy.ExecuteAction(() =>
context.Conferences
.Where(c => c.Slug == conference.Slug)
.Select(c => c.Slug)
.Any());
if (existingSlug)
throw new DuplicateNameException("The chosen conference slug is already taken.");
// Conference publishing is explicit.
if (conference.IsPublished)
conference.IsPublished = false;
context.Conferences.Add(conference);
this.retryPolicy.ExecuteAction(() => context.SaveChanges());
this.PublishConferenceEvent<ConferenceCreated>(conference);
}
}
private void PublishConferenceEvent<T>(ConferenceInfo conference)
where T : ConferenceEvent, new()
{
Action<T> action = x => new T
{
SourceId = conference.Id,
Owner = new Owner
{
Name = conference.OwnerName,
Email = conference.OwnerEmail,
},
Name = conference.Name,
Description = conference.Description,
Location = conference.Location,
Slug = conference.Slug,
Tagline = conference.Tagline,
TwitterSearch = conference.TwitterSearch,
StartDate = conference.StartDate,
EndDate = conference.EndDate,
};
Apply(action);
}
private void Apply<TEvent>(Action<TEvent> action)
{
var @event = _eventFactory.Create(action);
Raise(@event);
}
private void Raise(object @event)
{
_eventHandlerFactory.Get(this, @event.GetType())(@event);
}
}但是,当我运行我的代码并尝试创建一个会议时,它会显示以下异常消息:
对象引用未设置为对象的实例. 描述:在执行当前web请求时发生了未处理的异常。请查看堆栈跟踪以获得有关错误的更多信息,以及它起源于代码的位置。 异常详细信息: System.NullReferenceException:对象引用未设置为对象的实例。 源错误: 第10行:公共类EventHandlerFactory : IEventHandlerFactory第11行:{第12行:私有静态只读ILogger Logger =IEventHandlerFactory第13行:私有静态只读Dictionary>> _cache =新Dictionary>>();第14行:私有静态只读对象_cacheLock =新对象(); 源文件: F:\MyProjects\NES\NES-master\src\NES\EventHandlerFactory.cs行: 12 堆栈跟踪: NullReferenceException:对象引用没有设置为对象的实例。F:\MyProjects\NES\NES-master\src\NES\EventHandlerFactory.cs:12中的NES.EventHandlerFactory..cctor() TypeInitializationException:“NES.EventHandlerFactory”的类型初始化程序引发了一个异常。 NES.EventHandlerFactory..ctor() +0 NES.<>c.<.cctor>b__4_4(),F:\MyProjects\NES\NES-master\src\NES\DI.cs:24 F:\MyProjects\NES\NES-master\src\NES\DependencyInjectionContainer.cs:21 Conference.ConferenceService..cctor()中的NES.DependencyInjectionContainer.Resolve()在F:\MyProjects\NES\NES-master\samples\Conference\ConferenceService.cs:23中 TypeInitializationException:“Conference.ConferenceService”的类型初始化程序引发了一个异常。 F:\MyProjects\NES\NES-master\samples\Conference\ConferenceService.cs:36 Conference.Web.Admin.Controllers.ConferenceController.get_Service() in F:\MyProjects\NES\NES-master\samples\Conference.Web.Admin\Controllers\ConferenceController.cs:32 Conference.Web.Admin.Controllers.ConferenceController.Create(ConferenceInfo F:\MyProjects\NES\NES-master\samples\Conference.Web.Admin\Controllers\ConferenceController.cs:119 lambda_method中的Conference.ConferenceService..ctor(String nameOrConnectionString)( Object[] ) +180 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object[]参数) +68 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary
2 parameters) +486System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2参数) +71 System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +85 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult,ActionInvocation innerInvokeState) +44 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +82System.Web.Mvc.Async.WrappedAsyncResultBase1.结尾() +143 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +109 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +58 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +121 System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +321 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +56System.Web.Mvc.Async.WrappedAsyncResultBase1.结尾() +143 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +109 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +58 System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +76 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +184号 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +53System.Web.Mvc.Async.WrappedAsyncResultBase1.140() +140 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +63 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +50 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState) +45 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +81System.Web.Mvc.Async.WrappedAsyncResultBase1.140() +140 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +63 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +47 ( System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +50 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult,控制器) +50 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +73System.Web.Mvc.Async.WrappedAsyncResultBase1.140() +140 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +63 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +47 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +50 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +50 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult,ProcessRequestState innerState) +45 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +81System.Web.Mvc.Async.WrappedAsyncResultBase1.140() +140 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +63 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,对象标签) +47 ( System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +50 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult结果) +49 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9644037 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,布尔& completedSynchronously) +155
发布于 2016-02-25 07:35:38
问题已经解决了。这个问题是由于配置引起的。最后,我通过在Application_Start() Global.asax方法中添加以下代码来解决这个问题。
LogManager.Use<NServiceBus.Log4Net.Log4NetFactory>();https://stackoverflow.com/questions/35586531
复制相似问题