C# Windows 服务和远程处理

发布于 2024-09-07 06:14:13 字数 376 浏览 3 评论 0原文

我想创建一个 Window 服务,它可以从某个位置加载多个 DLL,并使用远程处理在特定 TCP 端口(假设为 9000)上发布它们。

每个 DLL 都包含一个将发布的类。

例如 (Test.dll)

namespace Test
{
  class Service
  {
    // methods here
  }
}

服务应使用远程处理 tcp://:9000/Test.dll 发布它

我知道如何使用远程处理,但我想知道如何实现服务,以便在启动时动态加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清引 2024-09-14 06:14:13

您可以使用 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

温馨耳语 2024-09-14 06:14:13

您可以轻松地在 .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

林空鹿饮溪 2024-09-14 06:14:13

如果您正在寻找强大的解决方案,请查看 MEF(托管扩展性框架)。

但是,如果您只想将 DLL 作为简单插件加载,我可以从我的 TournamentApi 中获取一些代码示例:

/// <summary>
/// Provides methods to load plugins from external assemblies.
/// </summary>
public static class PluginLoader
{
    ...

    /// <summary>
    /// Loads the plugins from a specified assembly.
    /// </summary>
    /// <param name="assembly">The assembly from which to load.</param>
    /// <returns>The plugin factories contained in the assembly, if the load was successful; null, otherwise.</returns>
    public static IEnumerable<IPluginFactory> LoadPlugins(Assembly assembly)
    {
        var factories = new List<IPluginFactory>();

        try
        {
            foreach (var type in assembly.GetTypes())
            {
                IPluginEnumerator instance = null;

                if (type.GetInterface("IPluginEnumerator") != null)
                {
                    instance = (IPluginEnumerator)Activator.CreateInstance(type);
                }

                if (instance != null)
                {
                    factories.AddRange(instance.EnumerateFactories());
                }
            }
        }
        catch (SecurityException ex)
        {
            throw new LoadPluginsFailureException("Loading of plugins failed.  Check the inner exception for more details.", ex);
        }
        catch (ReflectionTypeLoadException ex)
        {
            throw new LoadPluginsFailureException("Loading of plugins failed.  Check the inner exception for more details.", ex);
        }

        return factories.AsReadOnly();
    }
}

这需要一个加载的程序集并实例化程序集中每个 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:

/// <summary>
/// Provides methods to load plugins from external assemblies.
/// </summary>
public static class PluginLoader
{
    ...

    /// <summary>
    /// Loads the plugins from a specified assembly.
    /// </summary>
    /// <param name="assembly">The assembly from which to load.</param>
    /// <returns>The plugin factories contained in the assembly, if the load was successful; null, otherwise.</returns>
    public static IEnumerable<IPluginFactory> LoadPlugins(Assembly assembly)
    {
        var factories = new List<IPluginFactory>();

        try
        {
            foreach (var type in assembly.GetTypes())
            {
                IPluginEnumerator instance = null;

                if (type.GetInterface("IPluginEnumerator") != null)
                {
                    instance = (IPluginEnumerator)Activator.CreateInstance(type);
                }

                if (instance != null)
                {
                    factories.AddRange(instance.EnumerateFactories());
                }
            }
        }
        catch (SecurityException ex)
        {
            throw new LoadPluginsFailureException("Loading of plugins failed.  Check the inner exception for more details.", ex);
        }
        catch (ReflectionTypeLoadException ex)
        {
            throw new LoadPluginsFailureException("Loading of plugins failed.  Check the inner exception for more details.", ex);
        }

        return factories.AsReadOnly();
    }
}

This takes a loaded assembly and instantiates an instance of each IPluginEnumerator in the assembly and has it return each IPluginFactory (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文