csharp ASP.NET Web API:通过提供自己的AssembliesResolver来控制加载的程序集

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp ASP.NET Web API:通过提供自己的AssembliesResolver来控制加载的程序集相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Web.Http.SelfHost;

namespace SelfHost
{
    class Program
    {
        static HttpSelfHostServer CreateHost(string address)
        {
            // Create normal config
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(address);

            // Set our own assembly resolver where we add the assemblies we need
            AssembliesResolver assemblyResolver = new AssembliesResolver();
            config.Services.Replace(typeof(IAssembliesResolver), assemblyResolver);

            // Add a route
            config.Routes.MapHttpRoute(
              name: "default",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { controller = "Home", id = RouteParameter.Optional });

            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();

            Console.WriteLine("Listening on " + address);
            return server;
        }

        static void Main(string[] args)
        {
            // Create and open our host
            HttpSelfHostServer server = CreateHost("http://localhost:8080");

            Console.WriteLine("Hit ENTER to exit...");
            Console.ReadLine();
        }
    }

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

            // Add whatever additional assemblies you wish

            return assemblies;
        }
    }
}

以上是关于csharp ASP.NET Web API:通过提供自己的AssembliesResolver来控制加载的程序集的主要内容,如果未能解决你的问题,请参考以下文章

csharp Nancy和ASP.NET Web API比较

csharp 在ASP.NET Web Api有效负载中添加资源链接

csharp 将Castle Windsor依赖项注入ASP.NET Web API过滤器C#

如何使用 HTTP 连接通过托管在 IIS 上的 ASP.NET 成员身份保护 ASP.NET Web API [重复]

如何通过js跨域调用ASP.NET Web API (请问如何实现在javascript中通过http get的方式跨域调用ASP.NET Web API?)

通过 ASP.NET Web API 有效地使用 async/await