基于OSGI.NET的MVC插件式开发
最近在研究OSGI.NET插件式开发框架。官方网站提供了一个基于OSGI.NET的插件仓库。下载官方的SDK包安装后VS项目模板会多出一组iOpenWorks项目模板。在学习过程中,发现通过iOpenWorks模板创建的应用程序主程序都会附加上iOpenWorks插件仓库相关的插件。如何使用OSGI.NET框架库来创建纯净的插件框架,来实现自己的IDE呢?
基于WinForm、WPF的主程序很简单。这里主要介绍下如何来实现基于MVC的插件式框架。
主要思路:通过MVC中的Area方式来实现对插件页面的访问,一个子项目为一个Area。
需要解决的问题:
1.基于OSGI.NET插件的框架,实现的物理隔离,运行时不会将子项目的dll文件复制到主程序的bin目录下。
2.MVC中Area下的controller创建是根据命名空间结构来查找controller的,如何将Area下的controller对应到子项目的程序集中。
3.Controller如何正确的加载cshtml页面。
4.子项目中的View文件中如何访问资源文件。
接下来我们来逐个解决以上的问题。(关于OSGI.NET,可以在官网上看下视频教程,这里就不介绍了)
1.实现MVC主程序对插件MVC项目下的ControllerAction的访问
新建空的解决方案:Osgi.Net.Mvc
在解决方案下新建MVC4项目,命名为Osgi.Net.Mvc.Web,选择空模板,选择Razor视图引擎
在新建的MVC4项目下添加项目目录Plugins
在解决方案中添加新的MVC4项目,命名为About,选择空模板,选择Razor视图引擎,将About项目的路径,保存到Osgi.Net.Mvc.Web项目的Plugins目录下
删除Global.asax文件和App_Start文件夹,并添加Manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<Bundle xmlns="urn:uiosp-bundle-manifest-2.0" Name="About" SymbolicName="About" Version="1.0.0.0" InitializedState="Active">
<Runtime>
<Assembly Path="bin\About.dll" Share="false" MultipleVersions="false" />
</Runtime>
</Bundle>
建成后的空项目如下:

在About下添加控制器Hello创建Index方法。
public class HelloController : Controller
{
public ActionResult Index()
{
return Content("About Plugin.");
}
}
接下来我们来编写代码,在Application启动前启动插件运行时BundleRuntime。
在Global.asax的MvcApplication方法中加入静态方法,并实现BundleRuntime的启动。
public static void BundleStart()
{
var runtime = new BundleRuntime();
runtime.Start();
}
在命名空间上注册PreApplicationStartMethod,让方法在Application前启动。
[assembly: PreApplicationStartMethod(typeof(MvcApplication), "BundleStart")]
namespace Osgi.Net.Mvc.Web
{
//......
}
此时,插件加载已完成。启动应用程序,访问/hello/index页面返回404.
如何能让主程序能访问插件中的controller呢?
我们知道,MVC的动态编译是通过System.Web.Compilation.BulidManager来管理程序集的。
我们尝试将插件的程序集加入到BulidManager管理的程序集中。
在BundleStart()方法中,BundleRuntime启动后加入代码
foreach (var bundle in runtime.Framework.Bundles)
{
var bundleData = runtime.GetFirstOrDefaultService<IBundleInstallerService>()
.GetBundleDataByName(bundle.SymbolicName);
if (bundleData == null) continue; var serviceContainer = runtime.Framework.ServiceContainer;
var service = serviceContainer.GetFirstOrDefaultService<IRuntimeService>();
var assemlbies = service.LoadBundleAssembly(bundle.SymbolicName);
assemlbies.ForEach(BuildManager.AddReferencedAssembly);
}
重新启动应用程序,访问/hello/index页面,这时已经能够看到从About中的HelloController返回的数据了。
2.实现通过Area的方式来访问插件
要实现通过Area的方式来访问插件,就需要修改通过Area来查找Controller的方式
首先给About插件添加Area注册文件AboutAreaRegistration.cs
public class AboutAreaRegistration : AreaRegistration
{
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"AboutPlugin",
"About/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "About.Controllers" }
);
} public override string AreaName
{
get { return "About"; }
}
}
然后创建一个BundleAreaControllerFactroy类来重载DefaultControllerFactory
public class BundleAreaControllerFactroy : DefaultControllerFactory
{
}
重载GetControllerType方法
1.拦截上下文中的Area信息
2.根据Area信息加载对应的Bundle信息
3.在Bundle中查找对应的contrller
4.如果未找到对应controller,将上下文转交给基类来处理
protected override Type GetControllerType(RequestContext requestContext, string controllerName)
{
string symbolicName = null;
object area;
if (requestContext.RouteData.DataTokens.TryGetValue("area", out area))
{
symbolicName = area as string;
}
else
{
var routeWithArea = requestContext.RouteData.Route as IRouteWithArea;
if (routeWithArea != null)
{
symbolicName = routeWithArea.Area;
}
var castRoute = requestContext.RouteData.Route as Route;
if (castRoute != null && castRoute.DataTokens != null)
{
symbolicName = castRoute.DataTokens["area"] as string;
}
} if (symbolicName != null)
{
var controllerTypeName = controllerName + "Controller";
var runtimeService = BundleRuntime.Instance.GetFirstOrDefaultService<IRuntimeService>();
var assemblies = runtimeService.LoadBundleAssembly(symbolicName); foreach (var assembly in assemblies)
{
foreach (var type in assembly.GetTypes())
{
if (type.Name.ToLower().Contains(controllerTypeName.ToLower())
&& typeof(IController).IsAssignableFrom(type))
{
return type;
}
}
}
} return base.GetControllerType(requestContext, controllerName);
}
在Global.asax的Application_Start中注册ControllerFactory
ControllerBuilder.Current.SetControllerFactory(new BundleAreaControllerFactroy());
到这里已经完成了通过Area访问插件的功能。启动应用程序,通过/about/hello/index访问页面,这时就能够看到刚才的页面了。
3.实现能够正确加载插件路径下cshtml文件的视图引擎
在RazorViewEngine对象中提供了一组视图路径模板:AreaViewLocationFormats、AreaMasterLocationFormats、AreaPartialViewLocationFormats、ViewLocationFormats、MasterLocationFormats、PartialViewLocationFormats。我们可以通过修改路径模板来实现对插件路径下的cshtml文件的正确加载。
首先创建BundleRazorViewEngine对象来重载RazorViewEngine
public class BundleRazorViewEngine : RazorViewEngine
{
}
在构造函数中我们首先将基类中的路径模板存储下来
public class BundleRazorViewEngine : RazorViewEngine
{
private readonly string[] _baseAreaViewLocationFormats;
private readonly string[] _baseAreaMasterLocationFormats;
private readonly string[] _baseAreaPartialViewLocationFormats;
private readonly string[] _baseViewLocationFormats;
private readonly string[] _baseMasterLocationFormats;
private readonly string[] _basePartialViewLocationFormats; public BundleRazorViewEngine()
{
_baseAreaViewLocationFormats = AreaViewLocationFormats;
_baseAreaMasterLocationFormats = AreaMasterLocationFormats;
_baseAreaPartialViewLocationFormats = AreaPartialViewLocationFormats;
_baseViewLocationFormats = ViewLocationFormats;
_baseMasterLocationFormats = MasterLocationFormats;
_basePartialViewLocationFormats = PartialViewLocationFormats;
}
}
然后重载FindView方法,在FindView中
1.从上下文中查找Area信息
2.根据Area信息加载插件
3.根据插件路径信息修改路径模板
4.执行基类的FindWiew来返回ViewEngineResult
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName,
string masterName, bool useCache)
{
string symbolicName = null;
object area;
if (controllerContext.RouteData.DataTokens.TryGetValue("area", out area))
{
symbolicName = area as string;
}
else
{
var routeWithArea = controllerContext.RouteData.Route as IRouteWithArea;
if (routeWithArea != null)
{
symbolicName = routeWithArea.Area;
}
var castRoute = controllerContext.RouteData.Route as Route;
if (castRoute != null && castRoute.DataTokens != null)
{
symbolicName = castRoute.DataTokens["area"] as string;
}
} if (string.IsNullOrEmpty(symbolicName)) return new ViewEngineResult(new string[0]); var bundle = BundleRuntime.Instance.Framework.GetBundleBySymbolicName(symbolicName);
if (bundle == null) return new ViewEngineResult(new string[0]); SetLocationFormats(@"~\" + bundle.Location.Replace(HostingEnvironment.ApplicationPhysicalPath, String.Empty));
return base.FindView(controllerContext, viewName, masterName, useCache);
} private void SetLocationFormats(string bundleLocation)
{
AreaViewLocationFormats = _baseAreaViewLocationFormats
.Select(item => item.Replace("~", bundleLocation)).ToArray();
AreaMasterLocationFormats = _baseAreaMasterLocationFormats
.Select(item => item.Replace("~", bundleLocation)).ToArray();
AreaPartialViewLocationFormats = _baseAreaPartialViewLocationFormats
.Select(item => item.Replace("~", bundleLocation)).ToArray();
ViewLocationFormats = _baseViewLocationFormats
.Select(item => item.Replace("~", bundleLocation)).ToArray();
MasterLocationFormats = _baseMasterLocationFormats
.Select(item => item.Replace("~", bundleLocation)).ToArray();
PartialViewLocationFormats = _basePartialViewLocationFormats
.Select(item => item.Replace("~", bundleLocation)).ToArray();
}
重载FindPartialView方法,方式与重载FindView相同,这里就不贴代码了。
在Global.asax的Application_Start中注册BundleRazorViewEngine
ViewEngines.Engines.Add(new BundleRazorViewEngine());
然后在About的HelloController中添加一个方法,返回ViewResult,在cshtml页面中添加一些文字来测试一下吧。
关于资源文件的访问,下次在说。
基于OSGI.NET的MVC插件式开发的更多相关文章
- MVC 插件式开发
MVC 插件式开发 在开发一个OA系统是,我们可能遇到 A模块. B模块 .C模块,这也模块组成一个完整的系统,买给客服.现在又有一个客服要我们做一个OA系统,唉我们发现,跟上一个OA系统差不多,但没 ...
- 零基础ASP.NET Core MVC插件式开发
零基础ASP.NET Core MVC插件式开发 一个项目随着业务模块的不断增加,系统会越来越庞大.如果参与开发的人员越多,管理起来也难度也很大.面对这样的情况,首先想到的是模块化插件式开发,根据业务 ...
- MVC插件式开发平台
---恢复内容开始--- 经过DyOS.BraveOS1.0再到BraveOS2.0,系统现在已经开发了下载. 我们的目标是,网页版操作系统,可以在线安装更新软件,并提供二次开发平台,提供基础的逻辑和 ...
- 从零开始实现ASP.NET Core MVC的插件式开发(五) - 插件的删除和升级
标题:从零开始实现ASP.NET Core MVC的插件式开发(五) - 使用AssemblyLoadContext实现插件的升级和删除 作者:Lamond Lu 地址:https://www.cnb ...
- 从零开始实现ASP.NET Core MVC的插件式开发(九) - 升级.NET 5及启用预编译视图
标题:从零开始实现ASP.NET Core MVC的插件式开发(九) - 如何启用预编译视图 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/1399 ...
- .NET MVC 简单的插件式开发
插件式开发的优势 1.提高软件的复用度 2.提高软件开发的并行性 3.缩短软件的研发周期.节约研发成本,带给程序开发人员更多的灵活性,产品在软件发布以后还可以添加新的插件和完善已有的功能. 4.方便软 ...
- 基于AppDomain的"插件式"开发
很多时候,我们都想使用(开发)USB式(热插拔)的应用,例如,开发一个WinForm应用,并且这个WinForm应用能允许开发人员定制扩展插件,又例如,我们可能维护着一个WinService管理系统, ...
- 从零开始实现ASP.NET Core MVC的插件式开发(一) - 使用ApplicationPart动态加载控制器和视图
标题:从零开始实现ASP.NET Core MVC的插件式开发(一) - 使用Application Part动态加载控制器和视图 作者:Lamond Lu 地址:http://www.cnblogs ...
- 从零开始实现ASP.NET Core MVC的插件式开发(二) - 如何创建项目模板
标题:从零开始实现ASP.NET Core MVC的插件式开发(二) - 如何创建项目模板 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/11155 ...
随机推荐
- elasticsearch-head连接不上es
修改elasticsearch.yml,增加如下字段 http.cors.enabled: true http.cors.allow-origin: "*" cros为: Cros ...
- PHP7 学习笔记(五)安装event扩展(libevent)
一.描述:有效安排I/O,时间和信号的扩展 使用可用于特定平台的最佳I/O通知机制的事件,是PHP基础设施的libevent端口. 二.下载地址:http://pecl.php.net/package ...
- JAVA实现具有迭代器的线性表(单链表)
一,迭代器的基本知识: 1,为什么要用迭代器?(迭代:即对每一个元素进行一次“问候”) 比如说,我们定义了一个ADT(抽象数据类型),作为ADT的一种实现,如单链表.而单链表的基本操作中,大部分需要用 ...
- python自带的调试器
python是自带调试器的. 比如你写了一个python程序,名叫test.py. 你想调试一下这个程序,你可以执行 python -m pdb test.py,就会进入test.py的调试. 想查看 ...
- Java基础编程题——打印九九乘法表
package com.yangzl.basic; /** * 九九乘法表 * @author Administrator * */ public class Nine_Nine_Multiplica ...
- Flask最强攻略 - 跟DragonFire学Flask - 第十篇 before_request after_request
Flask我们已经学习很多基础知识了,现在有一个问题 我们现在有一个 Flask 程序其中有3个路由和视图函数,如下: from flask import Flask app = Flask(__na ...
- RoIPooling、RoIAlign笔记
一).RoIPooling 这个可以在Faster RCNN中使用以便使生成的候选框region proposal映射产生固定大小的feature map 先贴出一张图,接着通过这图解释RoiPool ...
- Web下文件上传下载的路径问题
工程结构
- 【Python】zip文件密码破解
掌握基础语法后,尝试使用python的zipfile模块练手. zipfile是Python里用来做zip格式编码的压缩和解压缩的. 这里将大体的思路分解成四段代码,逐一完善功能: 第一段代码:解压z ...
- 【vim】删除标记内部的文字 di[标记]
当我开始使用 Vim 时,一件我总是想很方便做的事情是如何轻松的删除方括号或圆括号里的内容.转到开始的标记,然后使用下面的语法: di[标记] 比如,把光标放在开始的圆括号上,使用下面的命令来删除圆括 ...