Windows服务抛出System.IO.FileNotFoundException如果启用“允许服务与桌面交互”,则工作正常

时间:2016-08-10 08:30:09

标签: c# windows-services .net-2.0 windows-server

我们有4个Windows服务可以在我们的服务器上运行,但是当它们安装在客户端的服务器上时,其中3个工作正常,但是在异常后抛出一个

System.IO.FileNotFoundException: Could not load file or assembly 'myservice.core, Version=0.0.0.9999, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'myservice.core, Version=0.0.0.9999, Culture=neutral, PublicKeyToken=null'
   at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
   at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
   at System.Web.Hosting.HostingEnvironment.CreateInstance(Type type)
   at System.Web.Hosting.ApplicationHost.CreateApplicationHost(Type hostType, String virtualDir, String physicalDir)
   at Advam.ParkBankServer.Listener.AppListener.OnStart(String[] args)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

但是,如果我们转到服务的属性并选择“允许服务与桌面交互”,则错误消失

enter image description here

但我们真的不希望它以交互方式运行,因为我们有一个不同的代码,当应用程序以交互方式运行时运行。这个交互式代码仅用于调试和安装目的,我们可以删除代码,但这需要我们经历测试周期,无论如何看到下面的交互代码

public static void Main(string[] args)
{
    log.Debug("Main. . ");
    if (System.Environment.UserInteractive)
    {
        string parameter = string.Concat(args);

        switch (parameter)
        {
            case "--install":

                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                break;

            case "--uninstall":

                ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                break;

            default:

                MyService service = new MyService();
                service.OnStart(args);
                Console.ReadKey();
                service.OnStop();
                break;
        }
    }
    else
    {
        ServiceBase.Run(new ServiceBase[] { new MyService() });
    }
}

顺便说一句,这个dll也被其他服务使用,但它们运行正常但是我能想到的唯一区别是这个服务在其中托管了一些Web服务而其他服务没有。所以无论如何我们无需删除交互式代码就可以修复它。

我没有检查版本,但可能是“windows server 2008”

0 个答案:

没有答案