2011-06-03 105 views
1

我正在創建一個自定義powershell 1.0 cmdlet,這將允許我將異常從powershell腳本提供到Microsoft企業庫v5.0異常處理塊。問題與Microsoft企業庫5.0和自定義powershell cmdlet

我從外部文件加載我的異常處理配置,因爲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: 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設置來解決。

+0

不知道你真的想把額外的東西塞進system32區域。你應該真的把你的PowerShell的東西打包到一個模塊中,然後組裝問題將被管理。您遇到的另一個問題是,powershell進程使用的是不同於獨立版本的app.config文件;你必須找到另一種配置EL塊的方法。這不是我可以幫忙的,雖然... – beefarino 2011-06-06 17:56:44

+0

@beefarino感謝您的建議。將東西推入系統文件夾絕對不是理想的選擇。不幸的是我堅持使用PowerShell 1.0,所以我不認爲模塊是我的選擇。 – Atriarch 2011-06-08 17:25:58

回答

0

原來我沒有給我的配置文件,這是造成怪異的絕對路徑。我最初只是將其設置爲「widgit.dll.config」,並將其扔到%systemroot%\ System32下,因爲這是我的PowerShell的默認工作目錄,似乎擺脫了最初的「文件未找到」錯誤,我是有(在我瞭解整個問題之前不想更改工作目錄)。在預感上,我將配置複製到powershell目錄並將其重命名爲powershell.exe.config,並解決了我的其他問題。

將東西放入系統目錄並不是最優雅的解決方案,但對於我目前的問題來說,這已經足夠了。