基于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 ...
随机推荐
- centos7 pgpool+postgresql
安装postgresql CentOS7安装并配置PostgreSQL 安装pgpool rpm -ivh http://www.pgpool.net/yum/rpms/3.7/redhat/rhel ...
- Quartz.net创建windows服务
序言 安装服务 sc create XXService binpath= "XXService.exe" start= auto sc description XXService ...
- 尚硅谷spring_boot课堂笔记
尚硅谷spring_boot课堂笔记
- u-boot移植(四)---修改前工作:代码流程分析3---代码重定位
一.重定位 1.以前版本的重定位 2.新版本 我们的程序不只涉及一个变量和函数,我们若想访问程序里面的地址,则必须使用SDRAM处的新地址,即我们的程序里面的变量和函数必须修改地址.我们要修改地址,则 ...
- luogu P4448 [AHOI2018初中组]球球的排列
这道题我一上来只会80 还是要感谢题解区大佬题解的帮助 先考虑若\(xy,xz\)为完全平方数,则\(yz\)也为完全平方数,因为\(xy*xz=x^2yz\)为完全平方数,除掉\(x^2\)就行了 ...
- HTTP 协议报文解析
说明转载自https://blog.csdn.net/chf1142152101/article/details/74162755 本篇主要是为了记录HTTP中报文的格式,以便针对报文进行解析.首先会 ...
- 前端 - Ajax (1)
Ajax 主要作用 用于隐式提交,有别于input 提交时不会跳转/刷新页面. 前端: html 代码:(id) <p> <input id="user" typ ...
- Java 注解 (Annotation)你可以这样学
注解语法 因为平常开发少见,相信有不少的人员会认为注解的地位不高.其实同 classs 和 interface 一样,注解也属于一种类型.它是在 Java SE 5.0 版本中开始引入的概念. 注解的 ...
- C语言中,float在内存中的储存方式
浮点型变量在计算机内存中占用4字节(Byte),即32-bit. 遵循IEEE-754格式标准. 一个浮点数由2部分组成:底数m 和 指数e. ±mantissa × 2exponent (注意,公式 ...
- if-else 重构
最近发现自己写的代码if else太多了,有时候自己回头看都要重新捋逻辑,很不好.决定深入理解下if else重构. 中心思想: ①不同分支应当是同等层次,内容相当. ②合并条件表达式,减少if语句数 ...