C# Windows 服务和远程处理
我想创建一个 Window 服务,它可以从某个位置加载多个 DLL,并使用远程处理在特定 TCP 端口(假设为 9000)上发布它们。
每个 DLL 都包含一个将发布的类。
例如 (Test.dll)
namespace Test
{
class Service
{
// methods here
}
}
服务应使用远程处理 tcp://
发布它
我知道如何使用远程处理,但我想知道如何实现服务,以便在启动时动态加载 DLL,在停止时卸载它们。
也许还有另一种方法可以做到这一点?
I'd like to create a Window Service that can load several DLLs from a certain location and publish them using Remoting on a specific TCP port (let's say 9000).
Each DLL contains a single class that will published.
For example (Test.dll)
namespace Test
{
class Service
{
// methods here
}
}
The service should publish it using Remoting tcp://<servername>:9000/Test.dll
I know how to use Remoting but I'd like to know how to implement the service so that it can load the DLLs dynamically when starting and unload them when stopping.
May be there's another way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Assembly 类中的许多方法来实现此目的,它们被加载到 AppDomain 中,当您停止服务时,这些方法将随服务一起卸载。
查看:http://msdn.microsoft.com /en-us/library/system.reflection. assembly.loadfile.aspx
You can use the many methods in the Assembly class to achieve this, they're loaded into the AppDomain which will be unloaded with the service when you stop it.
Check out: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfile.aspx
您可以轻松地在 .NET 框架中加载程序集。查看 Assembly 类的 Load 方法,了解如何执行此操作的指导:
http://msdn.microsoft.com/en-us/library/xbe1wdx9%28v=VS.71%29.aspx
You can easily load assemblies in the .NET framework. Take a look at the Assembly class' Load method for guidance on how to do this:
http://msdn.microsoft.com/en-us/library/xbe1wdx9%28v=VS.71%29.aspx
如果您正在寻找强大的解决方案,请查看 MEF(托管扩展性框架)。
但是,如果您只想将 DLL 作为简单插件加载,我可以从我的 TournamentApi 中获取一些代码示例:
这需要一个加载的程序集并实例化程序集中每个
IPluginEnumerator
的实例,并让它返回它支持的每个IPluginFactory
(抽象工厂)。请随意查看 TournamentApi 项目的源代码,了解其余代码。
If you are looking for a robust solution, look into MEF, the Managed Extensibility Framework.
If, however, you just want to load the DLLs as simple plugins, I have some code samples from my TournamentApi:
This takes a loaded assembly and instantiates an instance of each
IPluginEnumerator
in the assembly and has it return eachIPluginFactory
(Abstract Factory) that it supports.Please feel free to take a look at the source code to the TournamentApi project for the rest of the code.