标签:
措施集和宿主不在一个措施集新建一个类库:
SelfHost: 要领一:1.添加对MyControllers类库的引用.
2.在控制台代码中插手一行代码:
固然,可以添加多个措施集.(记得引用)
var config = new HttpSelfHostConfiguration(":9527"); config.Routes.MapHttpRoute( "API Default", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }); //加载外部措施集 Assembly.Load("MyControllers"); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("请开始您的演出"); Console.ReadLine(); }
View Code3.测试功效:
要领二:1.新建自界说的 AssembliesResolver 类
View Code
2.改削控制台代码:
var config = new HttpSelfHostConfiguration(":9527"); config.Routes.MapHttpRoute( "API Default", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }); //加载外部措施集 //Assembly.Load("MyControllers"); //Assembly.Load("TestControllers"); config.Services.WordStr(typeof(IAssembliesResolver), new MyAssembiesResolver()); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("请开始您的演出"); Console.ReadLine(); }
View Code实际上就添了一行代码...
3.测试:
上面的要领将措施集写死在代码里,这必定不切合要求.
要领三:新建3个类,如下:
public class AppConfigSetting { public static string AssembliesLoadFrom => GetValue(); public static string GetValue([CallerMemberName]string key = null) { return System.Configuration.ConfigurationManager.AppSettings[key]; } }
public class AssembliesLoad : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public AssemblyElementCollection AssemblyNames { get { return (AssemblyElementCollection)this[""]; } } public static AssembliesLoad GetSection() { return ConfigurationManager.GetSection(AppConfigSetting.AssembliesLoadFrom) as AssembliesLoad; } } public class AssemblyElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new AssemblyElement(); } protected override object GetElementKey(ConfigurationElement element) { AssemblyElement serviceTypeElement = (AssemblyElement)element; return serviceTypeElement.AssemblyName; } } public class AssemblyElement : ConfigurationElement { [ConfigurationProperty("assemblyName", IsRequired = true)] public string AssemblyName { get { return (string)this["assemblyName"]; } set { this["assemblyName"] = value; } } }