Base solution for your next web application

Activities of "waku"

Hi,

I have an AuthorizationProvider, I injected the RoleManager into it, and in SetPermissions method, I used the RoleManager to grant some permissions to a role. The code is like this:

public class UserPermissionProvider : AuthorizationProvider
{
	private readonly RoleManager _roleManager;

	public UserPermissionProvider(RoleManager roleManager)
	{
		_roleManager = roleManager;
	}

	public override async void SetPermissions(IPermissionDefinitionContext context)
	{
		var somePermission = context.CreatePermission(...);
		
		var someRole = _roleManager.GetRoleByNameAsync("SomeRole").Result;
		_roleManager.GrantPermissionAsync(someRole, somePermission).Wait();
	}
}

Everything is fine, until unit tests. I noticed that the SetPermission method is executed before seeding data code in my TestBase:

// SetPermission is executed event before the constructor method of TestBase!
protected XXXTestBase()
{
	//Seed initial data
	UsingDbContext(context => new InitialDataBuilder(context).Build());

	LoginAsDefaultTenantAdmin();
}

As a result, there's no data, no "SomeRole" in the database, so the SetPermission method failed. How can I fix this problem?

Answer

Hi, thank you for your replies! They encourage me a lot!

If you have any suggestions, feel free to create issues on ABPHelper's github!

Question

Hi,

I wrote a VS extension to help with developing ABP application----ABPHelper. By now, ABPHelper has one feature:

Generate ApplicationService Methods Just input methods names, and click button, all the methods and DTO files will be generated.

You can install ABPHelper by using Extensions and Updates in Visual Studio. Or download it from Visual Studio Galleryand install it manullay.

Source code on github Any suggestion is welcome! :mrgreen:

<cite>hikalkan: </cite> Hi,

You can try this: Download the sample application which includes working unit tests (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/SimpleTaskSystem">https://github.com/aspnetboilerplate/as ... TaskSystem</a>) and compare with yours. Maybe your test project's app.config does not contain proper EF configuration.

I changed my app.config, it didn't work either. :cry: What makes me think it's related to dependency injection is that if I remove the RoleManager parameter from my AuthorizationProvider's constructor, then my unit tests are OK.

<cite>hikalkan: </cite> Hi,

That's related to EffortDB initialization, not arelated to injection. I got this exception before.

Go to ParentIf you have used Database.SetInitializer... method in somewhere in your code, it causes that problem. Remove it (at least for tests) and try again.

Sorry, but I don't use Database.SetInitializer method, I use the generated code by template to seed my data, they'are very "standard":

InitialDataBuilder:

public class InitialDataBuilder
    {
        private readonly BMSDbContext _context;

        public InitialDataBuilder(BMSDbContext context)
        {
            _context = context;
        }

        public void Build()
        {
            new DefaultTenantRoleAndUserBuilder(_context).Build();
        }
    }

BMSTestBase:

public abstract class BMSTestBase : AbpIntegratedTestBase
    {
        protected BMSTestBase()
        {
            //Fake DbConnection using Effort!
            LocalIocManager.IocContainer.Register(
                Component.For<DbConnection>()
                    .UsingFactoryMethod(Effort.DbConnectionFactory.CreateTransient)
                    .LifestyleSingleton()
                );

            //Seed initial data
            UsingDbContext(context => new InitialDataBuilder(context).Build());

            LoginAsDefaultTenantAdmin();
        }

Also, I do need the seed data to run my unit tests, so I can't remove them. Maybe I didn't understand your solution, but could you explain more? Thank you!

Hi, I need to use the RoleManager in my AuthorizationProvider like this:

public class UserPermissionProvider : AuthorizationProvider
    {
        private readonly RoleManager _roleManager;

        public UserPermissionProvider(RoleManager roleManager)
        {
            _roleManager = roleManager;
        }
    }

My app runs well as I expected, but when I run unit tests, all the tests failed. Here's the error message:

System.TypeInitializationException“Effort.DbConnectionFactory”的类型初始值设定项引发异常。 在 Effort.DbConnectionFactory.CreateTransient() 在 Castle.MicroKernel.ComponentActivator.FactoryMethodActivator1.Instantiate(CreationContext context) 在 Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) 在 Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden) 在 Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally) 在 Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) 在 Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, ref Burden burden) 在 Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired) 在 Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveFromKernelByType(CreationContext context, ComponentModel model, DependencyModel dependency) 在 Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) 在 Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context) 在 Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) 在 Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) 在 Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden) 在 Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally) 在 Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) 在 Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, ref Burden burden) 在 Castle.MicroKernel.Handlers.ExtendedHandler.InvokeResolvePipeline(Int32 extensionIndex, ResolveInvocation invocation) 在 Castle.MicroKernel.Handlers.ComponentLifecycleExtension.Intercept(ResolveInvocation invocation) 在 Castle.MicroKernel.Handlers.ExtendedHandler.InvokeResolvePipeline(Int32 extensionIndex, ResolveInvocation invocation) 在 Castle.MicroKernel.Handlers.ExtendedHandler.Resolve(CreationContext context, Boolean instanceRequired) 在 Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy) 在 Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy) 在 Castle.Windsor.WindsorContainer.Resolve() 在 BMS.Tests.BMSTestBase.UsingDbContext(Action1 action) 位置 BMSTestBase.cs: line 49 在 BMS.Tests.BMSTestBase..ctor() 位置 BMSTestBase.cs: line 32 在 BMS.Tests.Users.RoleAppServiceTests..ctor() 位置 RoleAppServiceTests.cs: line 11 Effort.Exceptions.EffortExceptionThe Effort library failed to register its provider automatically, so manual registration is required.

a) Call the Effort.Provider.EffortProviderConfiguration.RegisterProvider() method at entry point of the application

or

b) Add the following configuration to the App.config file: <system.data> <DbProviderFactories> <add name="Effort.Provider" > invariant="Effort.Provider" description="Effort.Provider" type="Effort.Provider.EffortProviderFactory, Effort" /> </DbProviderFactories> </system.data>

<entityFramework> <providers> <provider invariantName="Effort.Provider" > type="Effort.Provider.EffortProviderServices, Effort" /> </providers> </entityFramework> 在 Effort.Provider.EffortProviderConfiguration.RegisterDbConfigurationEventHandler() 在 Effort.Provider.EffortProviderConfiguration.RegisterProvider() 在 Effort.DbConnectionFactory..cctor() System.InvalidOperationExceptionThe Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See <a class="postlink" href="http://go.microsoft.com/fwlink/?LinkId=260883">http://go.microsoft.com/fwlink/?LinkId=260883</a> for more information. 在 System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.AddLoadedHandler(EventHandler`1 handler) 在 Effort.Provider.EffortProviderConfiguration.RegisterDbConfigurationEventHandler()

(sorry for the Chinese...)

It seems that unit tests can not inject the RoleManager to my AuthorizationProvider, how to solve it?

Some question about sample-blog-module

Hi, this is my first post, I’m Chinese, so forgive my English. Firstly, thank hikalkan for providing ABP framework, it’s awesome!

Recently I found sample-blog-module, I’m always interesting in module system of ABP, so I started to study it. However I encounter some questions:

  1. In the source of Abp.Samples.Blog, there are Application, Core, EntityFramework and Web projects. Are they must be created manually? Can I use <a class="postlink" href="http://www.aspnetboilerplate.com/Templates">http://www.aspnetboilerplate.com/Templates</a> to generate them? Otherwise I have to manually install all the dependency packages, it’s somehow a tedious work
  2. In the MyAbpZeroProject.Core project, there are already User, Role, etc. classes defined. However in the Auth folder of Abp.Samples.Blog.Core project, BlogUser, BlogRole exist. These classes are almost the same, what is the function of these classes? They seem to be duplicate.
  3. About “Run migrations”. If I change entities of Abp.Sample.Blog, should I run migrate.exe command again? Is there some way to simple this work?
  4. I noticed these is a blog folder in App\Main\views of MyAbpZeroProject.Web project. If I delete the folder, then the blog module of the app can’t work. In my opinion, the blog views are supposed to be existed only in Abp.Sample.Blog project, so how can I completely make it modular?

Thank you.

Showing 1 to 7 of 7 entries