2012-09-05 27 views
1

不設置到對象的實例。由於性能問題,我已經加入線程發佈代碼。對象引用的一把umbraco 4.8.1

我的代碼是:

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

當我調試代碼,我得到HttpContexthttpcontextformail.HttpContextReference

但執行course.HttpContext = httpcontextformail.HttpContextReference;後,course.HttpContext還有空值。

請幫我

感謝

+0

可以擴大這個代碼是如何被稱爲位?它是從Umbraco內部作爲一個事件監聽器調用的,還是你試圖從一個計劃任務中調用它? – Tim

回答

0

一把umbraco預計爲Web請求中使用,因此如果調用在另一個應用程序Web請求的外部API將無法正常工作等

如果要執行使用一把umbraco一些東西API外部,那麼您將需要使用Umbraco Base(或類似),以便API具有所需的HttpContext。

一把umbraco鹼基是RESTlike系統用於允許查詢通過定義的網址一把umbraco上下文中的執行或代碼。