2009-06-16 69 views
1

我已經構建了ASP.NET應用程序。我需要利用第三方COM對象SDK來管理文檔。該應用程序的名稱是「交織」。根據COM對象文檔,您應該「不要爲每個應用程序創建多個IManDMS對象」。如何在ASP.NET應用程序中管理COM訪問

因此,我決定創建一個IManDMS對象實例並將其存儲在一個Application變量中。下面是我用來檢索IManDMS對象的功能:當只有一個人被要求在.aspx頁面同時

public static IManDMS GetDMS() 
{ 
    IManDMS dms = HttpContext.Current.Application["DMS"] as IManage.IManDMS; 
    if (dms == null) 
    { 
     dms = new IManage.ManDMSClass(); 
     HttpContext.Current.Application["DMS"] = dms; 
    } 
    return dms; 
} 

… 
// Here is a code snippet showing its use 
IManage.IManDMS dms = GetDMS(); 
string serverName = "myServer"; 
IManSession s = dms.Sessions.Add(serverName); 
s.TrustedLogin(); 

// Do something with the session object, like retrieve documents. 

s.Logout(); 
dms.Sessions.RemoveByObject(s); 
… 

上面的代碼工作正常。當2個用戶併發請求的頁面,我得到了以下錯誤:

[Sessions ][Add ]Item "SV-TRC-IWDEV" already exists in the collection

顯然,你不能超過一個會議上向IManDMS對象添加,具有相同的名稱,在同一時間。這意味着我可以在任何給定的時間爲整個應用程序打開一個會話。

有沒有人有類似問題的經驗,並可以提供一個關於如何解決這個問題的建議,假設我不能爲每個應用程序創建多個IManDMS對象?

謝謝。

回答

2

您可以使用鎖來確保只有一個會話同時訪問該對象。我不知道IManDSM做什麼,而是根據你的代碼,它看起來像錯誤是由

IManSession s = dms.Sessions.Add(serverName); 

引起你加入同名的會話集合。你可以嘗試在Sessions集合中添加SesssionID之類的不同名稱嗎?

+0

不幸的是,我不能添加不同的名稱。該名稱指示要連接到哪個服務器。只有一臺服務器。 使用「鎖定」語句似乎是在做伎倆。謝謝您的幫助。 – 2009-06-16 20:12:45

1

在ASP.Net中,您可以安全地將每個會話視爲自己的應用程序。以下是我與iManage的SDK工具包使用多年管理Global.asax文件會話邏輯:

void Session_Start(object sender, EventArgs e) 
{ 
    // Code that runs when a new session is started 
    ManDMS clientDMS = 
     new ManDMSClass(); 
    clientDMS.ApplicationName = "My App Name"; 
    IManSession clientSession = 
     clientDMS.Sessions.Add(
     ConfigurationManager.AppSettings["DMS Server"]); 
    clientSession.MaxRowsForSearch = 750; 
    clientSession.AllVersions = false; 

    // Save the configured session for use by the user 
    Session.Add("Client.Session", clientSession); 

} 

void Session_End(object sender, EventArgs e) 
{ 
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode 
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised. 
    IManSession clientSession = 
     (IManSession)Session["Client.Session"]; 
    try 
    { 
     if (clientSession.Connected) 
      clientSession.Logout(); 
    } 
    catch (System.Runtime.InteropServices.COMException cex) 
    { 
     // Ignore COMException if user cannot logout 
    } 
    clientSession.DMS.Close(); 
} 

這樣做的好處是,會話建立/拆除連接到ASP.Net會話,它充分管理會話,併爲用戶提供一些很好的速度優勢。

每當你需要訪問Session對象,只需使用此代碼對你的ASP.Net頁面或回傳:

IManSession clientSession = 
     (IManSession)Session["Client.Session"]; 
相關問題