【问题标题】:How to manage COM access in ASP.NET application如何在 ASP.NET 应用程序中管理 COM 访问
【发布时间】:2010-07-21 03:20:59
【问题描述】:

我已经构建了 ASP.NET 应用程序。我需要使用第三方 COM 对象 SDK 来管理文档。该应用程序的名称是“交织”。根据 COM 对象文档,您应该“不要为每个应用程序创建多个 IManDMS 对象”。

因此,我决定创建 IManDMS 对象的一个​​实例并将其存储在 Application 变量中。这是我用来检索 IManDMS 对象的函数:

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 页面时,上面的代码可以正常工作。当 2 个用户同时请求该页面时,我收到以下错误:

[会话][添加]项目“SV-TRC-IWDEV” 集合中已存在

显然,您不能同时向 IManDMS 对象添加多个具有相同名称的会话。这意味着在任何给定时间,我只能为整个应用打开一个会话。

假设我不能为每个应用创建多个 IManDMS 对象,是否有任何人遇到过类似问题并可以就如何解决此问题提出建议?

谢谢。

【问题讨论】:

    标签: asp.net com


    【解决方案1】:

    您可以使用锁来确保只有一个会话同时访问该对象。我不确定 IManDSM 做了什么,但根据您的代码,看起来错误是由

    引起的
    IManSession s = dms.Sessions.Add(serverName);
    

    您正在向 Sessions 集合添加相同的名称。您可以尝试在 Sessions 集合中添加一个不同的名称,例如 SessionID 吗?

    【讨论】:

    • 很遗憾,我无法添加其他名称。该名称指示要连接到哪个服务器。只有一台服务器。使用“锁定”语句似乎可以解决问题。感谢您的帮助。
    【解决方案2】:

    在 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"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 2016-04-03
      相关资源
      最近更新 更多