gpt4 book ai didi

c# - Microsoft Enterprise Library 5.0和自定义Powershell cmdlet的问题

转载 作者:行者123 更新时间:2023-12-03 00:04:50 26 4
gpt4 key购买 nike

我正在创建一个自定义的powershell 1.0 cmdlet,该cmdlet允许我将powershell脚本中的异常提供给Microsoft Enterprise Library v5.0异常处理块。

由于cmdlet已编译为dll,因此我从外部文件加载了Exception处理配置,然后尝试使用该配置创建ExceptionManager的实例。

Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource config = 
new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource(configFile);
WriteDebug("Config loaded from " + Path.GetFullPath(configFile));
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(config);
exManager = EnterpriseLibraryContainer.CreateDefaultContainer(config).GetInstance<ExceptionManager>();

当我从Powershell调用命令并出现以下错误时,此操作将失败:

Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ExceptionManager, key "" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type ExceptionManager cannot be constructed. You must configure the container to supply this value.



令人沮丧的是,当在具有完全相同配置的独立控制台应用程序中使用该代码时,它们可以很好地工作。我不太确定为什么会收到此错误;我已确保使用与配置文件中引用的程序集相同的程序集,并确保已引用项目中所有必需的企业库dll。

此外,我还必须将企业库dll复制到powershell安装目录(%SystemRoot%\ system32 \ WindowsPowerShell \ v1.0)中,否则我将收到FileNotFoundExceptions,该错误是关于在配置文件被删除时无法找到正确的库。正在处理。我对Powershell或企业库没有太多的经验,但是我猜测可以通过试用AppDomain设置来解决。

最佳答案

原来,我没有给配置文件提供绝对路径,这导致了怪异。我最初只是将其设置为“widgit.dll.config”,然后将其扔到%systemroot%\ System32下,因为这是我的Powershell的默认工作目录,并且似乎摆脱了最初的“找不到文件”错误(在我了解整个问题之前,不希望更改工作目录)。凭直觉,我将配置复制到powershell目录中,并将其重命名为powershell.exe.config,这解决了我的另一个问题。

目前,将内容放入系统目录并不是最优雅的解决方案,但这足以解决我当前的问题。

关于c# - Microsoft Enterprise Library 5.0和自定义Powershell cmdlet的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6232872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com