【问题标题】:C# Windows Service and RemotingC# Windows 服务和远程处理
【发布时间】:2010-06-24 14:55:08
【问题描述】:

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

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

例如(Test.dll)

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

服务应该使用 Remoting tcp://<servername>:9000/Test.dll 发布它

我知道如何使用 Remoting,但我想知道如何实现该服务,以便它可以在启动时动态加载 DLL 并在停止时卸载它们。

可能还有其他方法可以做到这一点?

【问题讨论】:

    标签: c# .net service remoting


    【解决方案1】:

    您可以使用 Assembly 类中的许多方法来实现这一点,它们被加载到 AppDomain 中,当您停止服务时,该服务将被卸载。

    查看:http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfile.aspx

    【讨论】:

      【解决方案2】:

      您可以轻松地在 .NET 框架中加载程序集。查看 Assembly 类的 Load 方法以获取有关如何执行此操作的指导:

      http://msdn.microsoft.com/en-us/library/xbe1wdx9%28v=VS.71%29.aspx

      【讨论】:

        【解决方案3】:

        如果您正在寻找可靠的解决方案,请查看 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 项目的源代码以获取其余代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-26
          • 1970-01-01
          • 2019-03-06
          • 2010-09-13
          相关资源
          最近更新 更多