简体   繁体   English

.NET应用程序架构允许缺少装配

[英].NET Application architecture allowing missing assembly

I am searching for the correct assembly setup and abstraction to get this issue solved: 我正在寻找正确的程序集设置和抽象来解决这个问题:

I got an app using an assembly to access an external system. 我有一个使用程序集访问外部系统的应用程序。 Now this assembly is not available on every computer as it must be installed before my application (it comes with the complete external system setup). 现在这个程序集在每台计算机上都不可用,因为它必须在我的应用程序之前安装(它带有完整的外部系统设置)。

When I now start my app on a computer where this external system is not installed I get: "FileNotFoundException" (I've got an Application.ThreadException handler) 当我现在在未安装此外部系统的计算机上启动我的应用程序时,我得到:“FileNotFoundException”(我有一个Application.ThreadException处理程序)

I would like to only "lously" reference that assembly and be able to run without it (do offer configuration, advice, simulation). 我想“只是”引用该程序集并且能够在没有它的情况下运行(提供配置,建议,模拟)。

I guess I have to implement a wrapper but I am not sure where to start. 我想我必须实现一个包装器,但我不知道从哪里开始。 Will the wrapper reference the real assembly or use reflection to call it when available? 包装器是否会引用实际组件或使用反射在可用时调用它?

I think you have already kind of answered your question. 我想你已经回答了你的问题。 The best way to go about it is by implementing a "wrapper" that can take objects from this assembly and utilize the concrete objects of this assembly, then implement a stub that will take over when this assembly is not found. 实现它的最佳方法是实现一个“包装器”,它可以从该程序集中获取对象并利用该程序集的具体对象,然后实现一个存根,该存根将在未找到该程序集时接管。

This wrapper could be a proxy object if you implement the Proxy Pattern and it should be able to have some intelligent logic to switch to a different concrete object(s) when the above-mentioned assembly is not found. 如果您实现代理模式 ,则此包装器可以是代理对象,并且当找不到上述程序集时,它应该能够具有一些智能逻辑以切换到不同的具体对象。 Since you are using c#.net it should be as simple as checking whether the assembly exists in a specific location without trapping the check logic within a try/catch block. 由于您使用的是c#.net,因此它应该像检查程序集是否存在于特定位置一样简单,而不会在try / catch块中捕获检查逻辑。

Edit 编辑

By the way, the wrapper cannot reference the assembly because the assembly is not included in your project...you are manually loading the assembly which is different. 顺便说一句,包装器不能引用程序集,因为程序集不包含在您的项目中...您手动加载不同的程序集。 Then, in order to instantiate objects of this assembly and call methods of these objects you need to use reflection. 然后,为了实例化此程序集的对象并调用这些对象的方法,您需要使用反射。

You could handle the AppDomain.AssemblyResolve event. 您可以处理AppDomain.AssemblyResolve事件。 It's called when an assembly cannot be found, and it gives your code the opportunity to track down the specified assembly and return it back to the app domain. 当无法找到程序集时调用它,它使代码有机会跟踪指定的程序集并将其返回到app域。

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

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