3

我有一个 WCF 服务库 ( MyWCFService),它用于MEF加载插件并由 Windows 服务托管(所有 .NET 4.0)。我现在正在尝试以新的方式运行它AppDomain并启用它ShadowCopyFiles,希望我可以在运行时更新插件。这是 Windows 服务项目中的代码。

程序.cs

 static class Program
    {  
        static void Main()
        {  
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
               new MyService() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }

我的服务.cs

  public partial class MyService: ServiceBase
    {
        internal static ServiceHost MyServiceHost = null;
        public MyService()
        {
             // this works but is deprecated..
             AppDomain.CurrentDomain.SetShadowCopyFiles();

            //this is not working.. DLLs still get locked. Require for a new AppDomain
            //AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";  

            InitializeComponent();
        }   

        protected override void OnStart(string[] args)
        {
            if(MyServiceHost  !=null)
            {
                MyServiceHost.Close();
            }
            try
            {
                MyServiceHost= new ServiceHost(typeof(MyWCFService));         
                MyServiceHost.Open();
            }
            catch(Exception)
            {
            }          
        }

        protected override void OnStop()
        {
            if (MyServiceHost!= null)
            {
                MyServiceHost.Close();
                MyServiceHost= null;
            }
        }       
    }

有什么办法吗?我做了很多搜索,但仍然不知道如何使它与我当前的设置一起工作(或者我只是无法理解......)

我试图创建一个新的AppDomain内部Main()并用于 domain.DoCallBack(new CrossAppDomainDelegate(() => { ServiceBase.Run(ServicesToRun); }))启动服务,但我无法启动它并继续获取"Error 1053: The service did not respond to the start or control request in a timely fashion".

然后我尝试通过AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";在启动服务MyWCFService.cs之前设置为当前应用程序域启用卷影复制,InitializeComponent();但 dll 仍然被锁定。但是,如果我使用AppDomain.CurrentDomain.SetShadowCopyFiles();(不推荐使用的方法)启用卷影副本,则一切正常。我更困惑。

4

1 回答 1

1

好的,我最终创建了一个继承自那里的 shell/代理类MarshalByRefObject并从那里启动服务,这里是代码:

ServiceShell.cs

public class ServiceShell:MarshalByRefObject
{
     internal static ServiceHost MyServiceHost = null;

     public void Run()
     {
         if (MyServiceHost != null)
         {
             MyServiceHost.Close();
         }
         try
         {
             MyServiceHost = new ServiceHost(typeof(MyWCFService));
             MyServiceHost.Open();
         }
         catch (Exception)
         {
         }          
     }

     public void Stop()
     {
         if (MyServiceHost!= null)
         {
             MyServiceHost.Close();
             MyServiceHost = null;
         }
     }
}

我的服务.cs

public partial class MyService: ServiceBase
{        
    AppDomain domain;
    ServiceShell runner;

    public MyService()
    {   
        var setup = new AppDomainSetup
        {
            ShadowCopyFiles = "true"
        };
        domain = AppDomain.CreateDomain("MyServiceHostDomain", AppDomain.CurrentDomain.Evidence, setup);

        runner = (ServiceShell)domain.CreateInstanceAndUnwrap
            (typeof(ServiceShell).Assembly.FullName, typeof(ServiceShell).FullName);
        InitializeComponent();

    }

    protected override void OnStart(string[] args)
    {          
        runner.Run();
    }

    protected override void OnStop()
    {
        runner.Stop();
        AppDomain.Unload(domain);
    }       
}
于 2014-06-26T08:45:07.800 回答