Web API 之SelfHost与OwinSelfHots加载外部程序
public interface IAssembliesResolver { ICollection<Assembly> GetAssemblies(); }
public class DefaultAssembliesResolver : IAssembliesResolver { public virtual ICollection<Assembly> GetAssemblies() { return AppDomain.CurrentDomain.GetAssemblies().ToList<Assembly>(); } }
static void Main(string[] args) { // 如果 API 处于外部程序集,可通过以下代码加载 CM.API为其他程序集 //Assembly.Load("CM.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); //加载外部程序集 string baseAddress = "http://localhost:9000/"; // 启动 OWIN host WebApp.Start<Startup>(url: baseAddress); Console.WriteLine("程序已启动,按任意键退出"); Console.ReadLine(); }
宿主项目还是需要引用CM.API程序集
public class UserResolver: DefaultAssembliesResolver { public override ICollection<Assembly> GetAssemblies() { ICollection<Assembly> baseAssemblies = base.GetAssemblies(); List<Assembly> assemblies = new List<Assembly>(baseAssemblies); var assembly = AppDomain.CurrentDomain.BaseDirectory; string path = assembly + "\\" + "CM.API.dll"; var controllersAssembly = Assembly.LoadFrom(path); baseAssemblies.Add(controllersAssembly); return assemblies; } }
static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://localhost:8083"); config.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); //注册UserResolver,加载程序集 config.Services.Replace(typeof(IAssembliesResolver), new UserResolver()); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } }
public class UserResolver: DefaultAssembliesResolver { public override ICollection<Assembly> GetAssemblies() { ICollection<Assembly> baseAssemblies = base.GetAssemblies(); List<Assembly> assemblies = new List<Assembly>(baseAssemblies); var assembly = AppDomain.CurrentDomain.BaseDirectory; string path = assembly + "\\" + "CM.API.dll"; var controllersAssembly = Assembly.LoadFrom(path); baseAssemblies.Add(controllersAssembly); //加载多个程序集 不建议写死 string path2 = assembly + "\\" + "CM.API2.dll"; var controllersAssembly2 = Assembly.LoadFrom(path2); baseAssemblies.Add(controllersAssembly2); return assemblies; } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Reflection; namespace CM.SelfHost { public class AssembliesLoad : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public AssemblyElementCollection AssemblyNames { get { return (AssemblyElementCollection)this[""]; } } public static AssembliesLoad GetSection() { return ConfigurationManager.GetSection("AssembliesLoad") 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; } } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <configSections> <section name="AssembliesLoad" type="CM.SelfHost.AssembliesLoad,CM.SelfHost"/> </configSections> <AssembliesLoad> <add assemblyName="CM.API.dll" /> </AssembliesLoad> </configuration>
public class WebApiResolver: DefaultAssembliesResolver { public override ICollection<Assembly> GetAssemblies() { AssembliesLoad settings = AssembliesLoad.GetSection(); if (null != settings) { foreach (AssemblyElement element in settings.AssemblyNames) { AssemblyName assemblyName = AssemblyName.GetAssemblyName(element.AssemblyName); if (!AppDomain.CurrentDomain.GetAssemblies().Any(assembly => AssemblyName.ReferenceMatchesDefinition(assembly.GetName(), assemblyName))) { AppDomain.CurrentDomain.Load(assemblyName); } } } return base.GetAssemblies(); } }
static void Main(string[] args) { //Assembly.Load("CM.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); //加载外部程序集 var config = new HttpSelfHostConfiguration("http://localhost:8083"); config.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); config.Services.Replace(typeof(IAssembliesResolver), new UserResolver()); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="AssembliesLoad" type="CM.SelfHost.AssembliesLoad,CM.SelfHost"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <AssembliesLoad> <add assemblyName="CM.API.dll" /> </AssembliesLoad> </configuration>
至此,web api的承载方式就已经阐述完毕,说一下个人倾向,通过两篇文章对承载的了解,我喜欢使用OwinselfHost承载,并采用自定义配置文件的方式加载外部api程序集,Owin有跨平台功能,但没有去进行验证,大家有兴趣可以去验证下
Web API 之SelfHost与OwinSelfHots加载外部程序的更多相关文章
- Request Entity Too Large for Self Hosted ASP.Net Web API在Selfhost的api后台怎么解决Request Entity Too Large问题
Request Entity Too Large for Self Hosted ASP.Net Web API在Selfhost的api后台怎么解决Request Entity Too Large问 ...
- web.xml中配置启动时加载的servlet,load-on-starup
web.xml中配置启动时加载的servlet,load-on-starup 使用servlet来初始化配置文件数据: 在servlet的配置当中,<load-on-startup>1&l ...
- java web.xml listener servlet 和filter加载顺序
在该项目中总会遇到一些关于加载的优先问题.最近遇到了同样的类别似的,所以,如果你发现信息汇总下,以下是转载其他一些人,毕竟,人们写的不错.它不重复创建的轮.只是略作修改自己的观点. 首先能够肯定的是, ...
- AMD和RequireJS初识----优化Web应用前端(按需动态加载JS)
RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一.最新版本的RequireJS压缩后只有14K,堪称非常轻量.它还同时可以和其他的框架协同工作,使用Re ...
- Android浏览本地 API文档 + 解决页面加载慢的问题
火狐浏览器安装离线浏览插件: 用浏览器打开index.html文件,你会发现加载的很慢,原因你懂的,为此,我们可以通过离线的方式 查看本地API文档,用火狐浏览器 + Work Offline插 ...
- 移动端web页面列表类上拉加载,查看详情,iframe嵌套第三方页面遇到的问题以及解决办法
1.移动端上拉加载 网上有很多成熟的插件,比如iscroll.在这里介绍一下用jquery和js写的上拉加载方法.使用原生的去写上拉加载更多需要三个高度去做对比,以新闻类列表举例,首先需要整个dom的 ...
- Java之——Web项目中DLL文件动态加载方法
本文转自:https://blog.csdn.net/l1028386804/article/details/53903557 在Java Web项目中,我们经常会用到通过JNI调用dll动态库文件来 ...
- arcgis api for javascript本地部署加载地图
最近开始学习arcgis api for javascript,发现一头雾水,决定记录下自己的学习过程. 一.下载arcgis api for js 4.2的library和jdk,具体安装包可以去官 ...
- web.xml中监听器如何顺序加载
最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口, 并且在此自定义监听器中需要用到Spring的依赖注入功 ...
随机推荐
- 3893: [Usaco2014 Dec]Cow Jog
3893: [Usaco2014 Dec]Cow Jog Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 174 Solved: 87[Submit] ...
- python 接口自动化测试--代码实现(八)
用例读入数据库: #! /usr/bin/python # coding:utf-8 import sys,os from Engine import DataEngine reload(sys) s ...
- 一步到位分布式开发Zookeeper实现集群管理
说到分布式开发Zookeeper是必须了解和掌握的,分布式消息服务kafka .hbase 到hadoop等分布式大数据处理都会用到Zookeeper,所以在此将Zookeeper作为基础来讲解. Z ...
- Spring报错——Scope 'session' is not active for the current thread
在对程序进行了一些修改后,运行发现spring报了这个错误,这是由于我设置了一个@Scope("session")导致的,现记录下解决方法. 解决方法: 将Scope设置为scop ...
- 计算机程序的思维逻辑 (75) - 并发容器 - 基于SkipList的Map和Set
上节我们介绍了ConcurrentHashMap,ConcurrentHashMap不能排序,容器类中可以排序的Map和Set是TreeMap和TreeSet,但它们不是线程安全的.Java并发包中与 ...
- HBuilder的webview操作
HBuilder的webview操作 webviewAPI文档:http://www.html5plus.org/doc/zh_cn/webview.html 创建新的webview窗口: Webvi ...
- C# .NET 逻辑层的框架设计
前述:在我的了解中,一个大项目的逻辑层是不可捉摸的,对于不同项目或场景都是不同的逻辑.先说明,我的想法是对逻辑层类结构,以及如何操作逻辑的方法进行抽象的封装.并且考虑将不同类,或者不同程序集中的逻辑方 ...
- Struts2之OGNL表达式
OGNL(Object-Graph Navigation Language的简称),对象图导航语言,它是一门表达式语言,除了用来设置和获取Java对象的属性之外,另外提供诸如集合的投影和过滤以及lam ...
- linux vi hjkl由来
很远原因来自历史 I was reading about vim the other day and found out why it used hjkl keys as arrow keys. Wh ...
- FDG内存分配器笔记
FDG: 大规模并行系统中的动态内存分配器由于需要全局同步(记账) ,导致性能急剧下降. 代码解析 1.superblock 类中包含两个变量,两个函数.默认superblock大小为2048 ite ...