由于性能问题,我在线程中添加了发布代码。
我的代码是:
public void functionname()
{
----------------
------------
try
{
HttpontextforMailSending gethttpcontextforpublish2 = new HttpontextforMailSending()
{
HttpContextReference = HttpContext.Current,
courseDocument = shortCourseDocument,
createdUser = new User(0)
};
Thread t2 = new Thread(PublishDocument);
t2.Start(gethttpcontextforpublish2);
}
catch { }
-------------
-----------
}
private void PublishDocument(object input)
{
HttpontextforMailSending httpcontextformail = (HttpontextforMailSending)input;
Document course = httpcontextformail.courseDocument;
User createduser = httpcontextformail.createdUser;
if (course != null && createduser != null)
{
course.Publish(createduser);
umbraco.library.UpdateDocumentCache(course.Id);
}
}
public class HttpontextforMailSending
{
public HttpContext HttpContextReference { get; set; }
public Document courseDocument { get; set; }
public User createdUser { get; set; }
}
但我得到Object reference not set to an instance of an object" error on "course.Publish(createduser);
Umbraco版本是4.8.1。
可能是这个错误归因于course.HttpContext
。它的值为null
。
但是,当我将其设为course.HttpContext = httpcontextformail.HttpContextReference;
时,会显示警告
“umbraco.cms.businesslogic.web.document.HttpContext is obsolete. Do not use this. Get
的HttpContext via regular ASP.Net methods instead
。
当我调试此代码时,我会在HttpContext
上获得httpcontextformail.HttpContextReference
。
但在执行course.HttpContext = httpcontextformail.HttpContextReference;
后,course.HttpContext
仍然具有空值。
请帮帮我
由于
答案 0 :(得分:0)
Umbraco希望在Web请求中使用,因此如果在其他应用程序等Web请求之外调用API,API将无法工作。
如果你想在外部使用Umbraco API执行某些操作,那么你需要使用Umbraco Base(或类似的),以便API具有所需的HttpContext。
Umbraco Base是一个类似REST的系统,允许在Umbraco上下文中通过定义的URL执行查询或代码。