2014-05-09 147 views
0

一旦應用程序啓動,我正在使用ApplicationEventHanlder創建內容。這裏是我的代碼Umbraco - 嘗試使用ContentService保存內容時出現錯誤

 
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

哪個版本的Umbraco是這個?從堆棧跟蹤看來,通知服務正在導致一些問題,但之前沒有看到過這種類型的問題。 – sitereactor

+0

感謝@sitereactor。是的,你是對的。我下載了源代碼並進行了調試,通知服務在嘗試保存或發佈內容時導致問題。它不會從WebSecurity獲取當前用戶。 我使用Umbraco 7.1.1 –

回答

0

看起來問題已經解決 - 至少部分:https://github.com/umbraco/Umbraco-CMS/commit/ac88da4188365b101debf0f3e9ad7ec7c300a898即將發佈的7.1.3補丁版本。

我說,部分原因是因爲我覺得這裏有兩個問題:

  1. 您發佈的例外是圍繞CurrentUser,使用API​​時不會 存在。這個問題還沒有解決。

  2. 用於發送通知的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);包含兩個額外paramenters:0爲用戶ID(零是通常總是存在管理員用戶),和假用於指定raiseEvents =假。

使用此方法應確保您的代碼不會調用NotificationService,然後引發異常。

希望這會有所幫助!

相關問題