tags:

views:

123

answers:

2

Hi,

I have built ASP.NET app. I need to utilize a third party COM object SDK for managing documents. The application's name is "Interwoven". According to the COM object documentation you should "Never create more than one IManDMS object per application".

So, I decided to create one instance of the IManDMS object and store it in an Application variable. Here is the function that I use to retrieve a IManDMS object:

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);
…

The above code works fine when only one person is requesting the .ASPX page at a time. When 2 users concurrently request the page I get the following error:

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

Apparently you cannot add more than one session to the IManDMS object, with the same name, at the same time. This means I can only have one session open, for the entire app, at any given time.

Is there anyone who has experience with a similar issue and can offer a suggestion on how to get around this problem, assuming I can't create more than one IManDMS object per app?

Thanks.

+2  A: 

You can use a lock to make sure only one session access the object at the same time. I am not sure what IManDSM does, but based on your code, it looks like the error is caused by

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

You are adding the same name to the Sessions collection. Can you try adding a different name like SesssionID to the Sessions collection?

David
Unfortunately, I cannot add a different name. The name indicates which server to connect to. There is only one server.Using the "lock" statement seems to be doing the trick. Thanks for the help.
Tod1d
+1  A: 

In ASP.Net, you can safely think of each session as it's own application. Here is logic I have used with the IManage SDK kit for years to manage sessions in Global.asax file:

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();
}

The benefit to this is that the session setup / tear-down is linked to the ASP.Net session, which adequately manages the session, and provides some great speed benefits to the user.

Whenever you need to access the Session object, just use this code on your ASP.Net pages or postbacks:

IManSession clientSession =
        (IManSession)Session["Client.Session"];
Ryan from Denver