我正在使用ApplicationEventHanlder在应用程序启动后创建内容。这是我的代码
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
是否有人与此错误相似并为我提供解决方案?
谢谢!
答案 0 :(得分:0)
看起来这个问题已经得到修复 - 至少部分是:https://github.com/umbraco/Umbraco-CMS/commit/ac88da4188365b101debf0f3e9ad7ec7c300a898用于即将发布的7.1.3补丁版本。
我说部分是因为我认为这里有两个问题:
您发布的例外情况是在CurrentUser周围,但不会发生 使用API时存在。这个问题尚未解决。
用于发送通知的NotificationService需要一个HttpContext,因为它涉及一些遗留代码。这在上面的提交中已得到修复,因此如果HttpContext不可用,则不会发送通知。
但幸运的是,在您提供的代码示例中有一种解决方法。 ContentService中的Save-method有一个重载,用于指定该方法不应引发事件。当您不为Save方法引发事件时,NotificationService将无法被调用,并且异常不会发生。但当然,这也意味着不会使用API从代码中为您创建的内容项目发送通知。
示例:
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);
}
}
请注意applicationContext.Services.ContentService.Save(content, 0, false);
包含两个额外的参数:0表示用户ID(零通常是始终存在的admin用户),false表示raiseEvents = false。
使用此方法应确保您的代码不会调用NotificationService,然后会抛出异常。
希望这有帮助!