Base solution for your next web application
Open Closed

WebApi Owin and Abp Module initialize #822


User avatar
0
guglielmino created

Hi, I'm trying to use Abp with a WebApi project using Owin. I made a simple module and in Startup class I call

app.UseAbp();

but Initialize of my module is never called. Module class is pretty simple

[DependsOn(typeof(DataEfUsersModule),
        typeof(DataUsersModule),
        typeof(BusinessUsersModule))]
    public class EpAuthModule : AbpOwinModule
    {
        public override void PreInitialize()
        {
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }

And Configure method of Startup is also really simple:

public  void  Configuration(IAppBuilder app)
{           
    app.UseAbp();
    var config = new HttpConfiguration();
    config.MapHttpAttributeRoutes();
    app.UseWebApi(config);
}

Is there something wrong ?


11 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    UseAbp does not initialize ABP system. ABP currently needs a Global.asax file as like that: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-templates/blob/master/src/AbpCompanyName.AbpProjectName.WebSpaAngular/Global.asax.cs">https://github.com/aspnetboilerplate/as ... al.asax.cs</a> You can add one class like that.

  • User Avatar
    0
    guglielmino created

    Many thanks, now module Initialize is called as expected but I'm facing another problem. In my application I made a simple ApplicationService I'd like to inject in a ApiController (I don't want to use Dynamic Api Controllers). Trying to put some brakpoint in modules initialize methods i see all my services are correctly registered but I get an error like:

    An error occurred when trying to create a controller of type 'UserController'. Make sure that the controller has a parameterless public constructor.
    

    UserController is my ApiController, it receives the ApplicationService by constructor injection but there is something wrong because as You can see Abp can't inject the dependency. How can I check the problem? I tried to use ApiController and AbpApiController with the same result.

    thanks Fabrizio

  • User Avatar
    0
    hikalkan created
    Support Team

    Does your WebApi module depends on AbpWebApi module? Because AbpWebApi module register Api controllers. Can you test this: Create a new project from <a class="postlink" href="http://www.aspnetboilerplate.com/Templates">http://www.aspnetboilerplate.com/Templates</a> and try to add a api controller to WebApi project, run and test it.

  • User Avatar
    0
    guglielmino created

    Yes, my APi module is:

    [DependsOn(typeof(AbpWebApiModule),
            typeof(BusinessModule),
            typeof(DataModule), 
            typeof(DataEfModule))]
        public class ApiModule : AbpModule
        {
            public override void Initialize()
            {
                IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            }
        }
    
  • User Avatar
    0
    guglielmino created

    I finally unerstood the problem. In my project I'm using IdentityServer3 as external auth system, it uses Owin and require this in Startup::Configuration

    public void Configuration(IAppBuilder app)
            {
                app.UseAbp();
                          
                // accept access tokens from identityserver and require a scope of 'api1'
                app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
                {
                    Authority = "http://localhost:29169",
                    ValidationMode = ValidationMode.ValidationEndpoint,
                    RequiredScopes = new[] { "myapi" }
                });
     
                // configure web api
                var config = new HttpConfiguration();
                config.MapHttpAttributeRoutes();
    
                app.UseWebApi(config); 
            }
    

    Looking at the code in <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.Web.Api/WebApi/Configuration/AbpWebApiModuleConfiguration.cs">https://github.com/aspnetboilerplate/as ... uration.cs</a> I see You use GlobalConfiguration.Configuration and this is the problem. Trying to remove the configuration in startup and app.UseWebConfig make Abp works but not IdentityServer integration.

  • User Avatar
    0
    hikalkan created
    Support Team

    Do not create HttpConfiguration yourself (new HttpConfiguration), but always use GlobalConfiguration.Configuration:

    GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    

    If you don't want to use GlobalConfiguration, you can get the HttpConfiguration inside ABP like that:

    var httpConfiguration = Abp.Dependency.IocManager.Instance.Resolve<IAbpWebApiModuleConfiguration>().HttpConfiguration;
    
  • User Avatar
    0
    guglielmino created

    Many thanks but this doesn't resolve my problem. Using

    var config = Abp.Dependency.IocManager.Instance.Resolve<IAbpWebApiModuleConfiguration>().HttpConfiguration;
    

    in Startup give me this error:

    The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Then try to call EnsureInitialized(), or try to move your configuration to module's Initialization.

  • User Avatar
    0
    guglielmino created

    I tryed with

    public void Configuration(IAppBuilder app)
            {
                app.UseAbp();
    
                // accept access tokens from identityserver and require a scope of 'api1'
                app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
                {
                    Authority = "http://localhost:29169",
                    ValidationMode = ValidationMode.ValidationEndpoint,
                    RequiredScopes = new[] { "myapi" }
    
    
                });
    
                var config = Abp.Dependency.IocManager.Instance.Resolve<IAbpWebApiModuleConfiguration>().HttpConfiguration;
    
                config.EnsureInitialized();
    
                config.MapHttpAttributeRoutes();
                app.UseWebApi(config);
            }
    

    But I got the same error, I tryed to move

    config.EnsureInitialized();
    

    in module initialization too but the same error still here. I have no more ideas how to solve this problem.

  • User Avatar
    0
    hikalkan created
    Support Team

    AbpWebApiModule calls EnsureInitialized in PostInitialize (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.Web.Api/WebApi/AbpWebApiModule.cs#L68">https://github.com/aspnetboilerplate/as ... ule.cs#L68</a>). If your configuration in your module's PreInitialize, it should work. I did not understand why it does not.

  • User Avatar
    0
    guglielmino created

    I made some tries without success. config.EnsureInitialized() is called before using the configurationobject in Startup but I get always the same error. I checked when EnsureInitialized is called and tried to move it in different places (module Initialize, PreInitialize, PostInitialize and in Startup) but the result is always the same.