2016-01-08 70 views
0

我在Windows 2003 Server 32位操作系統上部署了一個Web應用程序(在VS 2008中開發)。 我的應用程序使用Microsoft Office Word(我正在使用它來生成PDF)。 應用程序運行良好,正在生成.Doc & .Pdf。 但是,一旦我重新啓動服務器,我得到上述錯誤(8007007E)只有第一次,我刷新頁面後,應用程序開始再次正常工作。 我查了各種原因的錯誤,但沒有一個匹配我的。通常在發生此錯誤時它會導致COM使用的完全崩潰。下面的代碼示例爲 。奇怪的錯誤。 8007007E:檢索組件{}的COM類工廠 - 8007007E

string htmlContent = div_Data.InnerHtml; 
string filename = strLoanNo + strTime + ".doc"; 
if (!Directory.Exists(Server.MapPath("~/Doc/"))) 
{ 
Directory.CreateDirectory(Server.MapPath("~/Doc/")); 
} 
System.IO.StringWriter tw = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 
div_Data.RenderControl(hw); 
FileStream fs = new FileStream(Server.MapPath("~/Doc/") + filename,FileMode.OpenOrCreate, FileAccess.Write); 
StreamWriter sw = new StreamWriter(fs); 
sw.Write(tw.ToString()); 
sw.Flush(); 
sw.Close(); 
fs.Close(); 

object fileName = Server.MapPath("~/Doc/") + filename; 
object missing = System.Reflection.Missing.Value; 
object readOnly = false; 
object isVisible = true; 
object SaveChanges = true; 
Microsoft.Office.Interop.Word.ApplicationClass appWord = new Microsoft.Office.Interop.Word.ApplicationClass(); 
Microsoft.Office.Interop.Word.Document oWordDoc = appWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing, ref missing); 
oWordDoc.ExportAsFixedFormat(Server.MapPath("~/Doc/") + strLoanNo + "_" + strTime + ".pdf", WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 1, 1, WdExportItem.wdExportDocumentWithMarkup, true, false, WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, true, true, ref missing); 
((_Document)oWordDoc).Close(ref SaveChanges, ref missing, ref missing); 
appWord.Quit(ref SaveChanges, ref missing, ref missing); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(appWord); 

Response.Clear(); 
Response.Charset = ""; 
Response.ClearHeaders(); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment;filename=" + strLoanNo + "_" + strTime + ".pdf"); 
     Response.WriteFile(Server.MapPath("~/Doc/") + strLoanNo + "_" + strTime + ".pdf"); 
Response.Flush(); 
Response.Close(); 

if (File.Exists(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".doc")) 
{ 
    File.Delete(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".doc"); 
} 
if (File.Exists(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".pdf")) 
{ 
    File.Delete(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".pdf"); 
} 

回答

1

不知道代碼的問題,在這一點上,因爲這個問題可能是一個DLL的地方,但只有系統管理員將能夠告訴你哪一個是掙扎。如果可以,請使用Process monitor來隔離哪些組件失敗https://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

感謝, 頂點

+0

,如果它是一個DLL問題將不是一次又一次地給出錯誤? –

+0

我可能是錯的,但我非常確定COM DLLS是按需加載的,只要相應的COM對象被創建(顯然這並不意味着你有一個COM DLL的問題,它可能是任何DLL)。所以聽起來像是初始加載和DLL加載之間存在延遲,只是一個理論,我仍然認爲你應該隔離。希望能幫助到你。 – apexlol

+0

我認爲DLL的Load on Demand理論看起來相當準確,因爲無論我在重新啓動後等待多久,第一個實例始終會引發錯誤。將Doc轉換爲PDF我確實安裝了一些附加的微軟插件......可能是因爲它們沒有隨COM服務一起啓動 –

相關問題