Base solution for your next web application
Open Closed

Dynamic Web API initialization #572


User avatar
0
doubledp created

Hi,

In the documentation regarding the Dynamic Web API layer, there is the mention of the following:

ITaskAppService is the application service that we want to wrap with an api controller. It is not restricted to application services but this is the traditional and recommended way. "tasksystem/task" is name of the api controller with an arbitrary namespace. You should define at least one-level namespace but you can define more deep namespaces like "myCompany/myApplication/myNamespace1/myNamespace2/myServiceName". '/api/services/' is a prefix for all dynamic web api controllers. So, address of the api controller will be like '/api/services/tasksystem/task' and GetTasks method's address will be '/api/services/tasksystem/task/getTasks'. Method names are converted to camelCase since it's conventional in javascript world.

Does that mean we should explicitly define every application service like the following, if we want it generate the APIs according to the namespace of where the interface is defined?

DynamicApiControllerBuilder.For<ITaskAppService>("/myNamespace1/myNamespace2/tasksystem/task").Build();

Or is there perhaps a more dynamic way of generating it as above? The reason I am asking is because I see that currently in the template's WebAPI assembly, the initialization is defined as follows:

DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(TemplateApplicationModule).Assembly, "app")
                .Build();

3 Answer(s)
  • User Avatar
    0
    doubledp created

    Hi Halil, any advice on this?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    There is an extension point for it. You can use WithServiceName method to determine service name based on the service type:

    DynamicApiControllerBuilder
        .ForAll<IApplicationService>(typeof(TemplateApplicationModule).Assembly, "app")
        .WithServiceName(type => ...)
        .Build();
    

    So, you can get namespace of type, perform any logic and generate service name including namespaces. For example, if type's FullName is "MyCompany.MyProject.MyNs1.TestAppService" then you can return "myNs1/test". Then your service in client side will be "abp.services.app.myNs1.test". You can investigate more by trying.

    Note: Default service name generator is here: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.Web.Api/WebApi/Controllers/Dynamic/Builders/BatchApiControllerBuilder.cs#L104">https://github.com/aspnetboilerplate/as ... er.cs#L104</a>

  • User Avatar
    0
    doubledp created

    Thank you Halil :)

    I've been playing around with different naming conventions and I understand now how it works. The thing that threw me off was thinking that I have to inherit from IApplicationService, not knowing that I can create my own interface and inherit from that.

    After going back to the documentation I realize now that I did not read properly to understand it fully.

    Sorry for the unnecessary question :oops: