简体   繁体   English

如何在Visual Studio中组织模块化(C#/ MEF)应用程序以允许调试?

[英]How to organize a modular (C#/MEF) application in Visual Studio to allow debugging?

I am in the process of developing a modular application using C# and MEF. 我正在使用C#和MEF开发模块化应用程序。 The application is supposed to consist of 该应用程序应包含

  • a main application , which is the actual executable, providing core functionality, exposing a number of interfaces (extension points) and using MEF to pull in plug-in assemblies that fit into these 一个主应用程序 ,它是实际的可执行文件,提供核心功能,公开许多接口(扩展点)并使用MEF插入适合这些接口的插件程序集
  • a set of plug-ins that provide classes that fit into the interfaces and can be used by the main application 一组插件 ,这些插件提供适合接口的类,并且可由主应用程序使用

The main application may either run all by itself or with one or more plug-ins imported. 主应用程序可以全部运行,也可以导入一个或多个插件。 This should be a rather standard architecture for a modular MEF-based application. 对于基于模块化MEF的应用程序,这应该是一个相当标准的体系结构。 Initial tests have shown that this appears to generally work. 初步测试表明,这似乎普遍有效。 If I deploy the main application as well as one or multiple plug-in assemblies in to a folder, everything works fine. 如果我将主应用程序以及一个或多个插件程序集部署到一个文件夹中,则一切正常。

However, I am struggling with how to organize the Visual Studio solution for this. 但是,我正在为如何组织Visual Studio解决方案而苦苦挣扎。 My initial approach is that the main application, as well as each plug-in are separate projects within a solution. 我最初的方法是主应用程序以及每个插件都是解决方案中的独立项目。 The main application is an exe project, whereas the plug-ins are dll projects. 主要应用程序是一个exe项目,而插件是dll项目。 Plug-in projects depend on the main project, since they are implementing interfaces and using classes defined in the main application (I could have created a common lib project that does this, but it does not seem to add any benefit). 插件项目依赖于主项目,因为它们正在实现接口并使用在主应用程序中定义的类(我本可以创建一个通用的lib项目来执行此操作,但似乎没有任何好处)。

This way, I can start and debug the main application (with no plug-ins) fine. 这样,我可以很好地启动和调试主应用程序(没有插件)。

But how can the solution be organized so I can debug the main application with one, multiple or all plug-ins? 但是如何组织解决方案,以便我可以使用一个,多个或所有插件调试主应用程序? The current approach builds each plug-in into its own folder (which is generally fine) and copies the main application into each of these (which is not quite desirable). 当前的方法是将每个插件构建到自己的文件夹中(通常很好),然后将主应用程序复制到其中的每个应用程序中(不太理想)。 I could potentially configure an individual plug-in project to start the main application in its output folder, but I have no idea how to do this for more than one plug-in or how to do this if the main application should not be copied into each plug-in output folder. 我可能会配置一个单独的插件项目,以在其输出文件夹中启动主应用程序,但是我不知道如何对多个插件执行此操作,或者如果不应将主应用程序复制到该插件中,该怎么做每个插件输出文件夹。

Any hints or best practices would be highly appreciated. 任何提示或最佳做法将不胜感激。 I am using Visual Studio 2015 - if that makes any difference. 我正在使用Visual Studio 2015-如果有什么不同。

Plug-in projects depend on the main project, since they are implementing interfaces and using classes defined in the main application (I could have created a common lib project that does this, but it does not seem to add any benefit). 插件项目依赖于主项目,因为它们正在实现接口并使用在主应用程序中定义的类(我本可以创建一个通用的lib项目来执行此操作,但似乎没有任何好处)。

There are benefits to it, here's a couple of them: 它有很多好处,有几个:

  • it just makes sense that if you have multiple projects which are all using the same classes/functions, that these reside in a seperate common project. 如果您有多个项目都使用相同的类/函数,则将它们驻留在单独的公共项目中是很有意义的。 And possibly even another seperate project just for the interfaces. 甚至可能还有另一个单独的项目仅用于接口。 It makes it easier to grasp things, seeing a solution where you have a main application, some dlls with common functionality, some plugins 它使您可以更轻松地掌握内容,查看具有主应用程序的解决方案,一些具有常用功能的dll,一些插件
  • suppose you once have to add another main app then that can just make use of the common dll as well, instead of having to reference the first main app 假设您曾经必须添加另一个主应用程序,那么该应用程序也可以利用通用dll,而不必引用第一个主应用程序
  • you won't end up with your main app being copied x times to other project's output directories because they depend on it 您最终不会将主应用程序复制到其他项目的输出目录x次,因为它们依赖于该目录
  • it seems a bit strange (to me at last) that other projects have an exe project as dependency, rather than a dll project. (最后对我来说)其他项目将exe项目作为依赖项而不是dll项目似乎有点奇怪。 Just like it is really weird to me that a plugin depends on the main application it is in turn loaded in. Might be just me though - but I think it's also one of the reasons you have to ask the rest of your question in the first place. 就像一个插件真的依赖于它所加载的主应用程序对我来说真的很奇怪。虽然可能只是我一个人-但我认为这也是您必须在第一个问题中提出其余问题的原因之一地点。

But how can the solution be organized so I can debug the main application with one, multiple or all plug-ins? 但是如何组织解决方案,以便我可以使用一个,多个或所有插件调试主应用程序?

Typcially you tell your main application at startup which plugins to load. 通常,您在启动时告诉主应用程序要加载哪些插件。 There are different ways to do this: read a file containing names of plugins, scan a known directory for plugins, a combination. 有多种方法可以执行此操作:读取包含插件名称的文件,扫描已知目录中的插件,以及两者的结合。 All supported or relatively easy to implement using MEF. 所有都支持或相对容易使用MEF实施。 For instance for one large C# app we have, all plugins are copied into something like bin\\Plugins. 例如,对于我们拥有的一个大型C#应用程序,所有插件都被复制到bin \\ Plugins之类的文件中。 When the application starts it looks for dlls in bin\\Plugins, filters the list based on a textfile containing regexes, then loads plugins from the filtered list. 当应用程序启动时,它将在bin \\ Plugins中查找dll,根据包含正则表达式的文本文件过滤列表,然后从过滤后的列表中加载插件。 When sending the application to customers they get all, or only some plugins. 将应用程序发送给客户时,他们可以获得全部或仅一些插件。 When developping we use the text file to cut down application load time. 开发时,我们使用文本文件来减少应用程序的加载时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM