Hi,

I have the need to access two domains in my web app. I use the session manager to initialize the first domain. I thought of creating a second session manager class but how would I make sure which domain is active.

What I would like to accomplish is be able to use a second session manager to handle transactions automatically so I can activate the domain sessions as needed.

Thank you for your cooperation,

Richard

asked Feb 17 '16 at 21:29

rasxte's gravatar image

rasxte
20161617


2 Answers:

Hello Richard

I don't thing that you can use pair of Session managers at the same time. I thing you'll have to implement your own manager. SessionManager is extendible class so some of it's members might be overriten but I'm not sure that you achieve behavior you need by just extending our one. Anyway SessionManager class is just an implementation of IHttpModule and you are might create yours.

I suppose the domains will have different models, am I right?

answered Feb 18 '16 at 13:34

Alexey%20Kulakov's gravatar image

Alexey Kulakov
77225

Hi Alexey,

Thank you for your response..

My application needs to access a second domain for temporary purposes since I am migrating in stages an old app to a new app. The new app. is the main domain and the second domain is the old system.

What I did was create a session manager based on your session manager using the IHttpModule. It works but if you have the time you can check it for any problem I may have during production.

Thanks again,

Richard

(Feb 18 '16 at 22:13) rasxte rasxte's gravatar image

I though that the session manager should handle transactions manually since the access to the second domain would be minimal so I created methods to start and end transactions...

(Feb 18 '16 at 22:18) rasxte rasxte's gravatar image

namespace COICMS {

/// <summary>
/// Session Manager for COIBV Domain
/// </summary>
/// 
public class SM_COIBV : IHttpModule
{
    private static Domain domain = null;
    private Session session = null;
    private TransactionScope ts;
    private static readonly object currentItemKey = new object();
    private readonly object provideSessionLock = new object();

    public static Domain Domain
    {
        get
        {
            return domain;
        }
    }

    public Session Session
    {
        get
        {
            return session;
        }
    }

    public static void Init()
    {

        if (Domain == null)
        {
            domain = Set_Domain();
        }

    }

    protected static Domain Set_Domain()
    {
        StringBuilder connectionString;

        connectionString = new StringBuilder();

        connectionString.Append("Data Source =" + AppConfiguration.DataSource);
        connectionString.Append("; Initial Catalog = COIBV_M");
        connectionString.Append("; User =" + AppConfiguration.Sqluser);
        connectionString.Append("; password = " + AppConfiguration.Sqlpassword);
        connectionString.Append("; MultipleActiveResultSets=True");

        Domain domainBV;

        var domainConfigCMS = DomainConfiguration.Load("COIBV");
        var sessionConfigCMS = domainConfigCMS.Sessions["SP"];

        ConnectionInfo connInfo;

        connInfo = new ConnectionInfo(AppConfiguration.SqlProvider, connectionString.ToString());

        domainConfigCMS.ConnectionInfo = connInfo;

        domainBV = Domain.Build(domainConfigCMS);

        return domainBV;

    }

    public static SM_COIBV Current
    {
        get
        {
            var httpContext = HttpContext.Current;
            return httpContext == null ? null : (SM_COIBV)httpContext.Items[currentItemKey];
        }
        protected set
        {
            HttpContext.Current.Items[currentItemKey] = value;
        }
    }

    public void OpenTransaction()
    {
        if (session == null)
        {
            session = domain.OpenSession();
        }

        ts = session.OpenTransaction();

    }

    public void CompleteTransaction()
    {

        if (ts != null)
        {
            ts.Complete();
            ts.Dispose();
        }

    }

    public void RollBack()
    {

        if (ts != null)
        {

            ts.Dispose();

        }

    }

    protected virtual void BeginRequest(object sender, EventArgs e)
    {
        Current = this;

    }

    protected void EndRequest(object sender, EventArgs e)
    {

        lock (provideSessionLock)
            try
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
            finally
            {

                session = null;
            }
    }

    public void Error(object sender, EventArgs e)
    {

        RollBack();

    }

    /// <inheritdoc/>
    void IHttpModule.Init(HttpApplication context)
    {
        context.BeginRequest += BeginRequest;
        context.EndRequest += EndRequest;
        context.Error += Error;

        if (Session.Resolver == null)
            Session.Resolver = () => Current.Session;
    }

    /// <inheritdoc/>
    void IHttpModule.Dispose()
    {
    }
}

}

answered Feb 18 '16 at 22:15

rasxte's gravatar image

rasxte
20161617

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×2
×1

Asked: Feb 17 '16 at 21:29

Seen: 5,049 times

Last updated: Feb 18 '16 at 22:18

Related questions

powered by OSQA