How to setup roles in initialize module?

Vote:
 

I would like to setup commerce roles in the initialize module (Commerce 7.5 MVC).

But I get the error: System.NullReferenceException: Object reference not set to an instance of an object.

What should be added to the

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    [ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
    public class RoleInitializationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var securityManagerInstance = SecurityContext.Current.SecurityManagerInstance;
            securityManagerInstance.CreateRole("test", Enumerable.Empty<SecurityPermission>());
        }

        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context)
        {

        }
    }

 

#81251
Feb 12, 2014 15:56
Vote:
 

Hi,

The error you got was because SecurityContext.Current can be initialized properly, which itself caused by SqlContext is not properly initialized.

You can workaround this case by adding this:

            DataContext.Current = new DataContext(ConfigurationManager.ConnectionStrings["EcfSqlConnection"].ConnectionString);
            SqlContext.Current = DataContext.Current.SqlContext;
            var securityManagerInstance = SecurityContext.Current.SecurityManagerInstance;
            securityManagerInstance.CreateRole("test", Enumerable.Empty<SecurityPermission>());

Regards.

/Q

#81264
Feb 13, 2014 4:38
Vote:
 

Thank you )

#81268
Feb 13, 2014 8:04
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.