NullReferenceException:对象引用未设置为对象的实例 - NES.EventHandlerFactory

时间:2016-02-23 19:33:40

标签: cqrs

我编写的代码如下:

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);
        }
    }

但是当我运行我的代码并尝试创建会议时,它会显示以下异常消息:

  

对象引用未设置为对象的实例。

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.NullReferenceException:不是对象引用   设置为对象的实例。

     

来源错误:

     

第10行:公共类EventHandlerFactory:IEventHandlerFactory   第11行:{第12行:私有静态只读ILogger记录器   = LoggerFactory.Create(typeof(EventHandlerFactory));第13行:私有静态只读字典&gt;&gt; _cache = new Dictionary&gt;&gt;();第14行:私人   static readonly object _cacheLock = new object();

     

源文件:   F:\ MyProjects \ NES \ NES-master \ src \ NES \ EventHandlerFactory.cs行:   12

     

堆栈追踪:

     

[NullReferenceException:对象引用未设置为的实例   对象。] NES.EventHandlerFactory..cctor()in   F:\ MyProjects下\ NES \ NES主\ SRC \ NES \ EventHandlerFactory.cs:12

     

[TypeInitializationException:类型初始值设定项   &#39; NES.EventHandlerFactory&#39;抛出异常。]
  NES.EventHandlerFactory..ctor()+ 0 NES。&lt;&gt; c。&lt; .cctor&gt; b__4_4()in   F:\ MyProjects下\ NES \ NES主\ SRC \ NES \ DI.cs:24
  NES.DependencyInjectionContainer.Resolve()in   F:\ MyProjects下\ NES \ NES主\ SRC \ NES \ DependencyInjectionContainer.cs:21   Conference.ConferenceService..cctor()in   F:\ MyProjects下\ NES \ NES主\样品\会议\ ConferenceService.cs:23

     

[TypeInitializationException:类型初始值设定项   &#39; Conference.ConferenceService&#39;抛出异常。]
  Conference.ConferenceService..ctor(String nameOrConnectionString)in   F:\ MyProjects下\ NES \ NES主\样品\会议\ ConferenceService.cs:36   Conference.Web.Admin.Controllers.ConferenceController.get_Service()in   F:\ MyProjects下\ NES \ NES主\样品\ Conference.Web.Admin \控制器\ ConferenceController.cs:32   Conference.Web.Admin.Controllers.ConferenceController.Create(ConferenceInfo   会议)   F:\ MyProjects下\ NES \ NES主\样品\ Conference.Web.Admin \控制器\ ConferenceController.cs:119   lambda_method(Closure,ControllerBase,Object [])+180
  System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase   controller,Object []参数)+68
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters) +486
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2   参数)+71
  System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod()   +85 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult   asyncResult,ActionInvocation innerInvokeState)+44
  System.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) +82
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+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。&lt;&gt; c__DisplayClass46.b__3f()   +321 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass33.b__32(IAsyncResult asyncResult)+53
  System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +56
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+143
  System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,   对象标签)+109
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult的   asyncResult)+58
  System.Web.Mvc.Async&LT;&GT; c__DisplayClass2b.b__1c()   +76 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21.b__1e(IAsyncResult)   asyncResult)+184
  System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+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.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +81
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+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.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +73
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+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.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +81
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+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,Boolean&amp; completedSynchronously)+155

1 个答案:

答案 0 :(得分:0)

问题已得到解决。由于配置问题引发了问题。最后,我通过在Global.asax的Application_Start()方法中添加以下代码行来修复此问题。

LogManager.Use<NServiceBus.Log4Net.Log4NetFactory>();