简体   繁体   English

Powershell加载自定义程序集配置文件

[英]Powershell load custom assembly config file

I have a DLL file, created in visual studio from my c# code. 我有一个DLL文件,在我的c#代码中在visual studio中创建。 I need to run my code from powershell, therefore I use the Add-Type method to load my assembly. 我需要从powershell运行我的代码,因此我使用Add-Type方法加载我的程序集。

My Powershell code: Add-Type -Path C:\\MyDirectoryWithDllFiles\\MyAssembly.dll [MyAssembly.MyClass]::MyStaticMethod() 我的Powershell代码: Add-Type -Path C:\\MyDirectoryWithDllFiles\\MyAssembly.dll [MyAssembly.MyClass]::MyStaticMethod()

When I put return "Hello World" in MyStaticMethod, everything works fine. 当我在MyStaticMethod中放回“Hello World”时,一切正常。

Of course, my program needs some functionality and a config file was required. 当然,我的程序需要一些功能,并且需要配置文件。 I updated my powershell code to this: 我将我的powershell代码更新为:

[appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "C:\MyDirectoryWithDllFiles\MyAssembly.dll.config")
Add-Type -AssemblyName System.Configuration
Add-Type -Path C:\MyDirectoryWithDllFiles\MyAssembly.dll
[MyAssembly.MyClass]::MyStaticMethod()

With the following config file: (MyAssembly.dll.config) 使用以下配置文件:(MyAssembly.dll.config)

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="myCustomSection" type="MyAssembly.MyCustomConfigurationSetting, MyAssembly" />
    </configSections>

    <!--this is the custom config section for myCustomSection-->
    <myCustomSection>
        <!-- some items -->
    </myCustomSection>
</configuration>

In MyStaticMethod I get the items from my config file, and it works fine when I run the code from visual studio. 在MyStaticMethod中,我从配置文件中获取项目,当我从visual studio运行代码时,它工作正常。 When I run the powershell code, as described above, I get the following error: 当我运行powershell代码时,如上所述,我收到以下错误:

PS C:\\MyDirectoryWithDllFiles> [MyAssembly.MyClass]::MyStaticMethod() Errors: [System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for myCustomSection: Could not load file or assembly 'MyAssembly' or one of its dependencies. PS C:\\ MyDirectoryWithDllFiles> [MyAssembly.MyClass] :: MyStaticMethod()错误:[System.Configuration.ConfigurationErrorsException:为myCustomSection创建配置节处理程序时出错: 无法加载文件或程序集'MyAssembly'或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。 (C:\\Users\\MyUserName\\AppData\\Local\\Temp\\tmp2EB6.tmp line 4) ---> System.IO.FileNotFoundException: Could not load file or assembly 'MyAssembly' or one of its dependencies. (C:\\ Users \\ MyUserName \\ AppData \\ Local \\ Temp \\ tmp2EB6.tmp第4行)---> System.IO.FileNotFoundException:无法加载文件或程序集“MyAssembly”或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。 at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError) at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host,String typeString,Boolean throwOnError)

It tries to find 'MyAssembly', as how I have defined it in the the in the config file. 它试图找到'MyAssembly',就像我在配置文件中定义它一样。 MyAssembly.dll is a dll I have created. MyAssembly.dll是我创建的一个DLL。 But even though I have this line in my powershell it won't work: Add-Type -Path C:\\MyDirectoryWithDllFiles\\MyAssembly.dll 但即使我在我的powershell中有这一行它也行不通:Add-Type -Path C:\\ MyDirectoryWithDllFiles \\ MyAssembly.dll

Any suggestions how I can get this working? 有什么建议我怎么能这个工作?

Solution: 解:

Thanks to JayKul's answer at https://stackoverflow.com/a/23569983/1408786 感谢JayKul在https://stackoverflow.com/a/23569983/1408786上的回答

I added this to the config section, and now it works fine: Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 我将此添加到配置部分,现在它工作正常: 版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null

<section name="myCustomSection" type="MyAssembly.MyCustomConfigurationSetting, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义配置部分:无法加载文件或程序集 - Custom config section: Could not load file or assembly 尝试在Web配置中创建自定义配置部分时无法加载文件或程序集 - Could not load file or assembly while trying to create custom configuration section in a web config .NET使用应用程序配置文件加载另一个程序集引用的程序集 - .NET use application config file to load an assembly that is referenced by another assembly 自定义成员资格提供程序无法加载文件或程 - Custom membership provider could not load file or assembly 无法加载文件或程序集。 应该被Web.config忽略 - Could not load file or assembly. Should be ignored by Web.config 无法在Web.Config中加载文件或程序集“ AssemblyName” - Could not load file or assembly 'AssemblyName' in Web.Config 无法加载文件或程序集工具\EntityFramework.PowerShell.Utility.dll' - Could not load file or assembly tools\EntityFramework .PowerShell.Utility.dll' 导入模块:无法在 Powershell 中加载文件或程序集 - Import-Module : Could not load file or assembly in Powershell 如何使用配置加载自定义.config文件 - How to load a custom .config file with Configuration 无法在Powershell中加载.NET程序集 - Unable to load .NET assembly in Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM