Webapi 2 controllers for 2 projects

I have webapi2 C # projects on which I have all the controllers in the controllers folder. I now have some functions that I want to add, but I want to put it in another visual studio project, which will have its own controllers folders (and models and views). Is it possible so that I can create some kind of module that webapi2 will load?

+5
source share
5 answers

Web Api relies on IHttpControllerSelector to select an api controller to handle the request, from which it has a default implementation, which is based on IAssembliesResolver to solve assemblies for finding api controllers.

With a minimal minimum change, you can replace this collector with a special implementation that will load other libraries for you.

A very naive example might look like this:

public class CustomAssemblyResolver : IAssembliesResolver { public List<string> PluginNames { get; set; } public CustomAssemblyResolver() { PluginNames = new List<string>(); //Add the custom libraries here PluginNames.Add("Your_Second_Library"); } public ICollection<Assembly> GetAssemblies() { var asms = AppDomain.CurrentDomain.GetAssemblies().ToList(); foreach (var name in PluginNames) { var asmPath = System.IO.Path.Combine(HostingEnvironment.MapPath("~/"), name + ".dll"); try { var asm= System.Reflection.Assembly.LoadFrom(asmPath); if(!asms.Contains(asm)) asms.Add(asm); } catch (Exception) { } } return asms; } } 

Then you can replace the default recognizer with this code

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

inside your Register method of the WebApiConfig class.

Then copy all your additional libraries with controller classes to the bin directory, and you're done.

If you need another setting to select a controller, you can go to the custom implementation of IHttpControllerSelector and replace the existing implementation in the same way.

+1
source

You can create functionality in a library project, and then link to this project on your webapi2 and another visual studio project. Basically you will have three decisions; two webapi solutions and one library solution. The library solution will contain the required logic needed for the two webapi solutions.

-1
source

You should not have the need for controllers in two projects.

If the controllers are designed for FULLY different business domains, make two api in IIS with two solutions.

If they are similar, create all your controllers in a web project. Then call these controllers for individual applications.

 public CustomerAccountsController : ApiController { private CustomerAccountService _customerAccountService; // lives in application layer project public CustomerAccountsController() { // di stuff } public HttpResponseMessage PutCancelAccount( int accountId ) { // exception handling + logging _customerAccountService.CancelAccount(accountId); // return status code if success, or if an exception } } public OrderController : ApiController { private OrderService _orderService; // lives in application layer project public OrderController() { //di stuff } public HttpResponseMessage PostCreateOrder(CreateOrderRequest createOrderRequest) { // exception handling + logging _orderService.TakeOrder(createOrderRequest); // return status code if success, or if an exception } } 

So, most of your logic should be hiding behind application-level services, and these services should have methods that map 1-1 for use cases. If your business domain for these two applications is completely different, just create two separate solutions and two separate IIS / api applications

-1
source

No, It is Immpossible. The maximum you can do is create a class library that will compile as a DLL, and then reference that DLL in your WebApi. Otherwise, you will be required to either put everything in one application (WebApi) or create two different WebApi applications.

-1
source

Depending on your needs ...

I can advise you, just put 2 controllers in one project and create an auxiliary / service folder / class in another project and call these services when you need to.

This is not really the answer to your question, but I believe that this will help. We usually create a solution using this folder structure, hope this helps:

 MyTeamSolution - MyTeam.Core = Class libraries > Models > Model1.cs > Model2.cs > Model3.cs > Interface > ISomeInterface.cs > Helpers > HelperClass.cs - MyTeam.Api = webapi project > Model1Controller.cs > Model2Controller.cs > Model3Controller.cs - MyTeam.Web = mvc project > Controllers > Models > Views > etc. - MyTeam.Sql = Class libraries > MyTeamDbContext.cs 
-1
source

Source: https://habr.com/ru/post/1263750/


All Articles