Ticket S32802
Visible to All Users

Initialization of one XPO data layer per one global HttpApplication

Answers approved by DevExpress Support

created 9 years ago (modified 8 years ago)

We have implemented the functionality described in this ticket. It will be included in our next update(s).

Please check back and leave a comment to this response to let us know whether or not this solution addresses your concerns.

Additional information:

The following code is now added by the Solution Wizard to the Global.asax.cs file for XPO-based applications (the behavior of existing applications is not changed):

C#
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {     CreateXPObjectSpaceProvider(args.ConnectionString, args);     args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null)); } private void CreateXPObjectSpaceProvider(string connectionString, CreateCustomObjectSpaceProviderEventArgs e) {     System.Web.HttpApplicationState application = (System.Web.HttpContext.Current != null) ? System.Web.HttpContext.Current.Application : null;     IXpoDataStoreProvider dataStoreProvider = null;     if(application != null && application["DataStoreProvider"] != null) {         dataStoreProvider = application["DataStoreProvider"] as IXpoDataStoreProvider;         e.ObjectSpaceProvider = new XPObjectSpaceProvider(dataStoreProvider, true);     }     else {         if(!String.IsNullOrEmpty(connectionString)) {             connectionString = DevExpress.Xpo.XpoDefault.GetConnectionPoolString(connectionString);             dataStoreProvider = new ConnectionStringDataStoreProvider(connectionString, true);         }         else if(e.Connection != null) {             dataStoreProvider = new ConnectionDataStoreProvider(e.Connection);         }         if (application != null) {             application["DataStoreProvider"] = dataStoreProvider;         }         e.ObjectSpaceProvider = new XPObjectSpaceProvider(dataStoreProvider, true);     } }

A single IXpoDataStoreProvider and IDataStore are created per each database, and they are shared by all users. Note that the true value is passed to the useCachedDataStore parameter of the ConnectionStringDataStoreProvider constructor. This parameter specifies if the cached IDataStore instance is used or a new IDataStore instance is created per each CreateWorkingStore method call.

When Object Space Providers are created in the overridden CreateDefaultObjectSpaceProvider method, providers (and their connections) are disposed of when the application is disposed of (e.g. on re-logon). When providers are created using the CreateCustomObjectSpaceProvider event, providers and their connections are disposed of together with their application too. Set the event's CreateCustomObjectSpaceProviderEventArgs.IsObjectSpaceProviderOwner  parameter to false to cancel dispose of providers together with the application in this scenario:

C#
private void Instance_CreateCustomObjectSpaceProvider(object sender, CreateCustomObjectSpaceProviderEventArgs e) { IXpoDataStoreProvider dataStoreProvider = null; if(Application["DataStoreProvider"] != null) { dataStoreProvider = Application["DataStoreProvider"] as IXpoDataStoreProvider; e.ObjectSpaceProvider = new SecuredObjectSpaceProvider((ISelectDataSecurityProvider)WebApplication.Instance.Security, dataStoreProvider, true); } else { if(!String.IsNullOrEmpty(e.ConnectionString)) { string connectionString = DevExpress.Xpo.XpoDefault.GetConnectionPoolString(e.ConnectionString); dataStoreProvider = new ConnectionStringDataStoreProvider(connectionString, true); } else if (e.Connection != null){ dataStoreProvider = new ConnectionDataStoreProvider(e.Connection); } Application["DataStoreProvider"] = dataStoreProvider; e.ObjectSpaceProvider = new SecuredObjectSpaceProvider((ISelectDataSecurityProvider)WebApplication.Instance.Security, dataStoreProvider, true); } e.IsObjectSpaceProviderOwner = false; }
    Show previous comments (5)
    AG AG
    Alex Gn (DevExpress) 7 years ago

      Hello,
      The CreateDefaultObjectSpaceProvider method should be modified as follows:

      C#
      protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) { SecuredObjectSpaceProvider provider = new SecuredObjectSpaceProvider((ISelectDataSecurityProvider)Security, GetDataStoreProvider(args.ConnectionString, args.Connection)); provider.AllowICommandChannelDoWithSecurityContext = true; args.ObjectSpaceProviders.Add(provider); } private IXpoDataStoreProvider GetDataStoreProvider(string connectionString, System.Data.IDbConnection connection) { System.Web.HttpApplicationState application = (System.Web.HttpContext.Current != null) ? System.Web.HttpContext.Current.Application : null; IXpoDataStoreProvider dataStoreProvider = null; if (application != null && application["DataStoreProvider"] != null) { dataStoreProvider = application["DataStoreProvider"] as IXpoDataStoreProvider; } else { dataStoreProvider = XPObjectSpaceProvider.GetDataStoreProvider(connectionString, connection, true); if (application != null) { application["DataStoreProvider"] = dataStoreProvider; } } return dataStoreProvider; }

      Let me know if you need any further assistance.

        Many thanks Alex for your quick response and solution.

        AG AG
        Alex Gn (DevExpress) 7 years ago

          You are always welcome!

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.