Microsoft Enterprise Library 5.0 和自定义 powershell cmdlet 的问题
我正在创建一个自定义 powershell 1.0 cmdlet,它允许我将 powershell 脚本中的异常提供给 Microsoft Enterprise Library v5.0 异常处理块。
由于 cmdlet 被编译到 dll 中,因此我从外部文件加载异常处理配置,然后尝试使用该配置创建 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:尝试获取 ExceptionManager 类型的实例时发生激活错误,键“” ---> Microsoft.Practices.Unity.ResolutionFailedException:依赖关系解析失败,类型 =“Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager”,名称 =“(无)”。 while:解决时发生异常。异常是: InvalidOperationException - 无法构造 ExceptionManager 类型。您必须配置容器来提供此值。
令人沮丧的是,当在具有完全相同配置的独立控制台应用程序中使用时,代码运行得非常好。我不太确定为什么会出现此错误;我已确保使用配置文件中引用的相同程序集,并确保在项目中引用所有必要的企业库 dll。
此外,我必须将 Enterprise Library dll 复制到 powershell 安装目录 (%SystemRoot%\system32\WindowsPowerShell\v1.0) 中,否则我会收到关于在配置文件时无法找到正确库的 FileNotFoundExceptions正在处理中。我对 powershell 或企业库没有太多经验,但我猜测这可能可以通过使用 AppDomain 设置来解决。
I'm creating a custom powershell 1.0 cmdlet that will allow me to feed exceptions from powershell scripts to the Microsoft Enterprise Library v5.0 exception handling block.
I load up my Exception handling config from an external file since the cmdlet is compiled into a dll, and then try to create an instance of ExceptionManager using the config.
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>();
This fails when I call my command from powershell with the following error:
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.
The frustrating thing is that the code works perfectly fine when used in a stand-alone console application with the exact same configuration. I'm not really sure why I'm getting this error; I've made sure that I'm using the same assemblies that are referenced in the config file and I've made sure that I'm referencing all the necessary enterprise library dlls in my project.
Additionally, I've had to copy the Enterprise Library dlls into the powershell install directory (%SystemRoot%\system32\WindowsPowerShell\v1.0), otherwise I would receive FileNotFoundExceptions about not being able to find the correct library while the configuration file was being processed. I don't have too much experience with powershell or enterprise library, but I'm guessing that this might be resolved by playing around with AppDomain settings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我没有给出配置文件的绝对路径,这导致了奇怪的情况。我最初只是将其设置为“widgit.dll.config”,并将其放在%systemroot%\ System32下,因为这是我的powershell的默认工作目录,并且似乎摆脱了最初的“文件未找到”错误有(在我理解整个问题之前不想更改工作目录)。凭直觉,我将配置复制到 powershell 目录中并将其重命名为 powershell.exe.config,这解决了我的另一个问题。
目前,将内容放入系统目录并不是最优雅的解决方案,但足以解决我当前的问题。
Turns out I wasn't giving an absolute path to my configuration file, which was causing weirdness. I originally just set it to "widgit.dll.config", and threw it under %systemroot%\System32, since that's the default working directory of my powershell, and seemed to get rid of the initial "file not found" errors I was having (didn't want to change the working directory until I understood the entire problem). On a hunch, I copied the config into the powershell directory and renamed it powershell.exe.config, and that solved my other problem.
Putting stuff into system directories isn't the most elegant solution at this point, but it's sufficient for my current problem.