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 开发者_JS百科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.
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.
精彩评论