如何在ASP.NET应用程序中管理COM访问
我已经建立了ASP.NET应用程序。我需要使用第三方COM对象SDK来管理文档。应用程序的名称为“Interwoven”。根据COM对象文档,您应该“每个应用程序创建的IManDMS对象不得超过一个” 因此,我决定创建IManDMS对象的一个实例,并将其存储在应用程序变量中。以下是我用来检索IManDMS对象的函数:如何在ASP.NET应用程序中管理COM访问,asp.net,com,Asp.net,Com,我已经建立了ASP.NET应用程序。我需要使用第三方COM对象SDK来管理文档。应用程序的名称为“Interwoven”。根据COM对象文档,您应该“每个应用程序创建的IManDMS对象不得超过一个” 因此,我决定创建IManDMS对象的一个实例,并将其存储在应用程序变量中。以下是我用来检索IManDMS对象的函数: public static IManDMS GetDMS() { IManDMS dms = HttpContext.Current.Application["DMS"]
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);
…
当一次只有一个人请求.ASPX页面时,上述代码可以正常工作。当两个用户同时请求该页面时,我得到以下错误:
[会议][添加]项目“SV-TRC-IWDEV”
集合中已存在
显然,您不能同时向IManDMS对象添加多个具有相同名称的会话。这意味着在任何给定时间,我只能为整个应用打开一个会话
假设我不能为每个应用程序创建多个IManDMS对象,是否有人有类似问题的经验,并能就如何解决此问题提供建议
谢谢。您可以使用锁来确保同一时间只有一个会话访问该对象。我不确定IManDSM是做什么的,但根据您的代码,错误似乎是由
IManSession s = dms.Sessions.Add(serverName);
您正在向会话集合添加相同的名称。您可以尝试向Sessions集合添加一个不同的名称,如SessionId吗 在ASP.Net中,您可以安全地将每个会话视为自己的应用程序。以下是我多年来一直与iManager 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会话,该会话可以充分管理会话,并为用户提供了一些非常快的好处
每当需要访问会话对象时,只需在ASP.Net页面或回发上使用以下代码:
IManSession clientSession =
(IManSession)Session["Client.Session"];
不幸的是,我无法添加其他名称。该名称指示要连接到的服务器。只有一台服务器。使用“lock”语句似乎就是一个好办法。谢谢你的帮助。