aspnetboilerplate / aspnetboilerplate

ASP.NET Boilerplate - Web Application Framework
https://aspnetboilerplate.com
MIT License
11.8k stars 3.79k forks source link

Configuring DynamicApiControllerBuilder #1259

Closed not-authorized closed 8 years ago

not-authorized commented 8 years ago

Hi All,

I'm trying to setup an application from ASP.NET Boilerplate Templates. I've chosen ASP.NET Core 1.0 which is not actually pure ASP.NET Core because it uses .Net Framework 4.6.1. An app which is just installed works fine, however, I want to replace ASP.NET MVC with ASP.NET WebAPI. Any of my attempts led me to error 404. Please advise, what am I doing wrong?

My changes were:

public interface IAccountAppService : IApplicationService
{
    Task<string> GetAllAccounts();
}

public class AccountAppService : CrmAppServiceBase, IAccountAppService
{
    public async Task<string> GetAllAccounts()
    {
        return await Task.Run(() => "Hello world!");
    }
}
public override void PreInitialize()
{
    Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(CrmConsts.ConnectionStringName);

    Configuration.Navigation.Providers.Add<CrmNavigationProvider>();

    Configuration.Modules.AbpAspNetCore()
        .CreateControllersForAppServices(
            typeof(CrmApplicationModule).Assembly
        );

    DynamicApiControllerBuilder
        .ForAll<IApplicationService>(Assembly.GetAssembly(typeof(CrmApplicationModule)), "crm")
        .WithConventionalVerbs()
        .Build();
}
osoykan commented 8 years ago

Could you have forgotten typeof(AbpWebApiModule) in depends on statement?

Also as far as i know,

Configuration.Modules.AbpAspNetCore()
        .CreateControllersForAppServices(
            typeof(CrmApplicationModule).Assembly
        );

code snippet allows you to do expose ApplicationService methods as api services but based on controller level, which means you don't need explicitly define DynamicApiControllerBuilder on PreInitialize method since the code snippet at the above that is exactly doing same thing.

hikalkan commented 8 years ago

Hi,

First of all, there is not concept "pure ASP.NET Core" as I know. ASP.NET Core can work on full .NET framework or .NET Core. ABP uses full .NET Framework (v4.6.1) since some of our depended libraries have no .NET Core support yet. We will work to support .NET Core in the future.

Do not mix ASP.NET Core MVC with ASP.NET MVC 5.x and Web API in same application. ASP.NET Core MVC replaces them. You don't need to Web API in ASP.NET Core.

not-authorized commented 8 years ago

Could you have forgotten typeof(AbpWebApiModule) in depends on statement?

No, I've added that dependency.

Well, guys, you're correct, I don't need to mix them. May I add separate WebApi project instead of the Web project? I've added it and again I had no luck.

The newly created WebApi project has the following configuration.

[DependsOn(typeof(AbpWebApiModule), typeof(CrmApplicationModule))]
public class CrmWebApiModule : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

        DynamicApiControllerBuilder
            .ForAll<IApplicationService>(Assembly.GetAssembly(typeof(CrmApplicationModule)), "crm")
            .WithConventionalVerbs()
            .Build();
    }
}
public class Startup
{
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options =>
        {
            options.AddAbp(services);
        });

        return services.AddAbp<CrmWebApiModule>(options =>
        {
            options.IocManager.IocContainer.AddFacility<LoggingFacility>(
                f => f.UseLog4Net().WithConfig("log4net.config")
            );
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseAbp();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "api/{controller}/{action}/{id?}");
        });
    }
}
"dependencies": {
  "Abp.AspNetCore": "0.10.3",
  "Abp.Web.Api": "0.10.3",
  "Castle.LoggingFacility.MsLogging": "1.0.0",
  "Castle.Windsor-log4net": "3.3.0",
  "Crm.Application": "1.0.0.0-*",
  "Microsoft.AspNetCore.Diagnostics": "1.0.0",
  "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
  "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
  "Microsoft.Extensions.Logging.Console": "1.0.0"
},

"tools": {
  "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},

"frameworks": {
  "net461": { }
}
hikalkan commented 8 years ago

Even if you create a new Web API project, eventually they will run in the same pipeline and it will lead to problems. I suggest you to create a second application. Both apps can share same core, ef, app... layers but they should run in different processes and hosted in different web sites under iis.

not-authorized commented 8 years ago

I want to have only WebAPI on the top of application layer. I do everything like described in documentation but constantly get 404.

hikalkan commented 8 years ago

You can do it like that:

Start by the startup template and delete views/controllers/scripts... in the Web project. If you want, you can move code from Web API project to the Web project and remove the WebApi project.

not-authorized commented 8 years ago

Thank you for the suggestion, but in that case, if I understand correctly it won't be ApiControllers, and consequently the controllers will have overhead and more complex life cycle. Please correct me if I am wrong.

hikalkan commented 8 years ago

Actually, they will be api controllers. I could not understand the thing about complex life cycle.

not-authorized commented 8 years ago

May I have the same with ASP.NET Core (.Net Framework) project?

Regarding the lifecycle, I was wrong. I checked it in documentation and MVC and API controllers have the same lifecycle, but API controllers have more lightweight architecture.

Thank you!

hikalkan commented 8 years ago

As I said before, do not add web api and aspnet core into same pipeline. Create different projects for them and share your other libraries (core, application...etc.)

not-authorized commented 8 years ago

I had just to use

Configuration.Modules.AbpAspNetCore()
   .CreateControllersForAppServices(typeof(MyApplicationModule).Assembly, moduleName: 'app',
      useConventionalHttpVerbs: true);

instead of

DynamicApiControllerBuilder
   .ForAll<IApplicationService>(typeof(TagCrmApplicationModule).Assembly, "crm")
   .Build();

since I'm using ASP.NET Core project.

Thanks!

hikalkan commented 8 years ago

Yes, AspNet Core replaces all of them.