asp.net Web API: How host defines controller in a standalone assembly

Source: Internet
Author: User
Tags hosting

How is the controller of the ASP.net Web API created? We know the default ASP.net Web API is a Defaultassembliesresolver object that is used to resolve assemblies in self host mode assembliesresolver. It only provides assemblies that are already loaded by the current application domain. If we define the Httpcontroller in the assembly in which the non-hosted program is located (in fact, we will basically choose to define the Httpcontroller type on a separate project in the self host hosted mode), even if we subordinate them to the directory where the host program runs , the host program does not actively load these assemblies when they are started. Because these assemblies are not loaded in the current application domain, Httpcontroller type resolution will fail, and Httpcontroller activation will not be possible. [This article has been synchronized to the how ASP.net Web APIs works?]

We can confirm this problem by a simple example. In one solution we defined 4 projects as shown in the right figure, where Foo, bar, and Baz are class library projects, and the corresponding Httpcontroller types are defined in these 3 projects. Hosting is a host console program that has references to the 3 projects mentioned above. We have defined three Httpcontroller types Foocontroller, Barcontroller, and Bazcontroller that inherit from Apicontroller in the project Foo, bar, and Baz respectively. As shown in the following code snippet, we define the unique action method get in these 3 httpcontroller types and let it return the assemblyqualifiedname of the current Httpcontroller type.

1:public class Foocontroller:apicontroller

2: {

3:public string Get ()

4: {

5:return this. GetType (). AssemblyQualifiedName;

6:}

7:}

8:

9:public class Barcontroller:apicontroller

10: {

11:public string Get ()

12: {

13:return this. GetType (). AssemblyQualifiedName;

14:}

15:}

16:

17:public class Barcontroller:apicontroller

18: {

19:public string Get ()

20: {

21:return this. GetType (). AssemblyQualifiedName;

22:}

23:}

We used the following code in the hosting program as the host to implement the homestay for the Web API in the Self host mode. We created a httpselfhostserver for the base site "http://127.0.0.1:3721" and we registered a route with the URL template "Api/{controller}/{id}" before it was opened.

1:class Program

2: {

3:static void Main (string[] args)

4: {

5:uri baseaddress = new Uri ("http://127.0.0.1:3721");

6:using (Httpselfhostserver httpserver = new Httpselfhostserver (new Httpselfhostconfiguration (baseAddress))

7: {

8:httpserver.configuration.routes.maphttproute (

9:name: "Defaultapi",

10:routetemplate: "Api/{controller}/{id}",

11:defaults:new {id = routeparameter.optional});

12:

13:httpserver.openasync (). Wait ();

14:console.read ();

15:}

16:}

17:}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.