无法从 ContentService 使用 SaveAndPublish

Car*_*sen 5 umbraco umbraco8

我正在尝试保存我的IContent呼叫child,但在这一行(contentService.SaveAndPublish(child);)上我收到以下错误:Object reference not set to an instance of an object.

\n\n
if (child.HasProperty("navn"))\n{\n    child.SetValue("navn", worker.Name.ToString(), "da-dk");\n}\ncontentService.SaveAndPublish(child);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这就是我定义我的contentService
IContentService contentService = Umbraco.Core.Composing.Current.Services.ContentService;

\n\n

我发现孩子们是这样的:

\n\n
long totalChildren;\n\nIEnumerable<IContent> children = contentService.GetPagedChildren(filialsParent.Id, 0, 100, out totalChildren);\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xc2\xb4\n有人能指出这里出了什么问题吗?

\n

Car*_*sen 5

我发现如果我这样做的话它就会起作用。

var umbf = Umbraco.Web.Composing.Current.Factory.GetInstance<IUmbracoContextFactory>();
using (var contextf = umbf.EnsureUmbracoContext())
{
    var umbcontext = contextf.UmbracoContext;
    IContentService cs = Umbraco.Core.Composing.Current.Services.ContentService;
    cs.SaveAndPublish(child);

}
Run Code Online (Sandbox Code Playgroud)