Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web.Api plugin architecture

Can you suggest me some articles or code samples about plugin architecture in web api?

Currently I'm thinking about this scenario: to have 1, centralized api gateway, where every client sends request, and have different applications controllers in Plugins folder. If someone wants to add new service, writes it's own controllers and puts dll files in Plugin folder.

like image 447
Levan Avatar asked Feb 10 '14 09:02

Levan


People also ask

What is the structure of web API?

The Web API is a framework for building web services, these web services use the HTTP protocol. The Web API returns the data on request from the client, and it can be in the format XML or JSON. The MVC architecture is the Model-View-Controller Pattern.

Can we use web API with ASP.NET web form?

Although ASP.NET Web API is packaged with ASP.NET MVC, it is easy to add Web API to a traditional ASP.NET Web Forms application. To use Web API in a Web Forms application, there are two main steps: Add a Web API controller that derives from the ApiController class. Add a route table to the Application_Start method.

What is ASP.NET web API?

ASP.NET Web API is a framework that helps you to build services by making it easy to reach a wide range of clients including browsers, mobiles, tablets, etc. With the help of ASP.NET, you can use the same framework and same patterns for creating web pages and services both.


1 Answers

For locating controller classes at run time, you can write an assembly resolver, like this.

public class MyAssembliesResolver : DefaultAssembliesResolver
{
    public override ICollection<Assembly> GetAssemblies()
    {
        List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());

        // Add all plugin assemblies containing the controller classes
        assemblies.Add(Assembly.LoadFrom(@"C:\Plugins\MyAssembly.dll"));

        return assemblies;
    }
}

Then, add this line to the Register method in WebApiConfig.

config.Services.Replace(typeof(IAssembliesResolver), new MyAssembliesResolver());

With this, the request will still need to be sent to the individual controller even though the controller classes can come from assemblies in the plugin folder. For example, if MyAssembly.dll in the plugins folder contains CarsController, the URI to hit this controller will be /api/cars.

like image 74
Badri Avatar answered Sep 22 '22 01:09

Badri