Pregunta

I'm using ApplicationEventHanlder to create a content once application started. Here is my code

public class CreateContent : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        var root = applicationContext.Services.ContentService.GetRootContent();

        var content = applicationContext.Services.ContentService.CreateContent("Profile", -1, "umbTextPage");

        applicationContext.Services.ContentService.Save(content);

        base.ApplicationStarted(umbracoApplication, applicationContext);
    }
}
[NullReferenceException: Object reference not set to an instance of an object.]
   Umbraco.Core.Security.AuthenticationExtensions.GetCurrentIdentity(HttpContextBase http, Boolean authenticateRequestIfNotFound) +127
   Umbraco.Web.Security.WebSecurity.GetUserId() +55
   Umbraco.Web.Security.WebSecurity.get_CurrentUser() +63
   Umbraco.Web.NotificationServiceExtensions.SendNotification(INotificationService service, IUmbracoEntity entity, IAction action, UmbracoContext umbracoContext, ApplicationContext applicationContext) +221
   Umbraco.Web.NotificationServiceExtensions.SendNotification(INotificationService service, IUmbracoEntity entity, IAction action, UmbracoContext umbracoContext) +118
   Umbraco.Web.NotificationServiceExtensions.SendNotification(INotificationService service, IUmbracoEntity entity, IAction action, ApplicationContext applicationContext) +111
   Umbraco.Web.Strategies.c__DisplayClass7.b__2(IContentService sender, SaveEventArgs`1 args) +219
   Umbraco.Core.Events.TypedEventHandler`2.Invoke(TSender sender, TEventArgs e) +0
   Umbraco.Core.Events.EventExtensions.RaiseEvent(TypedEventHandler`2 eventHandler, TArgs args, TSender sender) +48
   Umbraco.Core.Services.ContentService.Save(IContent content, Boolean changeState, Int32 userId, Boolean raiseEvents) +632
   Umbraco.Core.Services.ContentService.Save(IContent content, Int32 userId, Boolean raiseEvents) +47
   MyUmbraco.CreateContent.ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) in c:\Users\duongdd\Documents\Visual Studio 2013\Projects\MyUmbraco\MyUmbraco\CreateContent.cs:19
   Umbraco.Core.ApplicationEventHandler.OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) +62
   Umbraco.Core.CoreBootManager.b__5(IApplicationEventHandler x) +79
   Umbraco.Core.EnumerableExtensions.ForEach(IEnumerable`1 items, Action`1 action) +204
   Umbraco.Core.CoreBootManager.Complete(Action`1 afterComplete) +185
   Umbraco.Web.WebBootManager.Complete(Action`1 afterComplete) +74
   Umbraco.Core.UmbracoApplicationBase.StartApplication(Object sender, EventArgs e) +242
   Umbraco.Core.UmbracoApplicationBase.Application_Start(Object sender, EventArgs e) +40

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +572
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +178
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +218
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +369
   System.Web.HttpApplicationFactory.GetPipelineApplicationInstance(IntPtr appContext, HttpContext context) +102
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +275

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +761
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +150
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +245

Is anyone similar with this error and provide me a solution?

Thanks!

¿Fue útil?

Solución

It looks like the issue has already been fixed - at least partially: https://github.com/umbraco/Umbraco-CMS/commit/ac88da4188365b101debf0f3e9ad7ec7c300a898 for the upcoming 7.1.3 patch release.

I say partially because I think there are two issues here:

  1. The exception you posted is around the CurrentUser, which won't exist when using the API. This issue hasn't been fixed yet.

  2. The NotificationService which is used to send the notification requires an HttpContext because of some legacy code that is involved. This is fixed in the above commit, so the notification won't be sent if an HttpContext is not available.

But luckily there is a way around this in the code sample you provided. The Save-method in the ContentService has an overload to specify that the method should not raise events. When you don't raise events for a Save method the NotificationService won't be called and the exception won't occur. But of course it also means that no notification will be sent for the Content item you are creating from code using the API.

Example:

public class CreateContent : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        var root = applicationContext.Services.ContentService.GetRootContent();

        var content = applicationContext.Services.ContentService.CreateContent("Profile", -1, "umbTextPage");

        applicationContext.Services.ContentService.Save(content, 0, false);

        base.ApplicationStarted(umbracoApplication, applicationContext);
    }
}

Notice that applicationContext.Services.ContentService.Save(content, 0, false); contains two extra paramenters: 0 for the user id (zero is normally the admin user which is always present), and false for specifying that raiseEvents = false.

Using this approach should ensure that your code doesn't invoke the NotificationService, which then throws an exception.

Hope this helps!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top