gpt4 book ai didi

powershell - 'DotNetOpenAuth.Logger' 的类型初始化程序引发异常

转载 作者:行者123 更新时间:2023-12-03 00:19:21 25 4
gpt4 key购买 nike

我的目标是从 PowerShell 调用 Google Admin SDK Directory 更新。我从一个工作的 .NET 应用程序开始,并将其转换为一个类库(我测试过——它可以工作)。我将其加载到模块 list 中

RequiredAssemblies = @('My.GoogleAdminSDK.Directory.dll')

我的 Get-GoogleSDKUser 函数 New-Objects 我的类并在其上调用 GetUser。我得到:
Exception calling "GetUser" with "1" argument(s): "The type initializer for 
'DotNetOpenAuth.Logger' threw an exception."
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\My.googleadminsdkdirectory.admin\My.GoogleA
dminSDKDirectory.Admin.psm1:56 char:9
+ $service.GetUser($googleUserName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TypeInitializationException

我试过为 log4net 添加一个配置文件,它产生了一个空的日志文件,但这没有帮助。

备注 :为了做到这一点,我不得不添加
  <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.12.0" newVersion="1.2.12.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

到 powershell_ise.exe.config 以防止错误:

"Could not load file or assembly 'System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified."



这远非理想。

编辑

PowerShell-calling-my-DLL 版本的 InnerException 是:

Could not load file or assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' or one of its dependencies. The system cannot find the file specified.



花了一些时间才找到 innerException。我有一些雪貂,并找到了使用 Fuslogvw 的建议,所以我尝试了。我关闭并重新打开 PowerShell,启动 Fusion Log Viewer(这是我第一次使用它)并找到三个 log4net 条目。说明如下:
  • log4net
  • log4net,版本=1.2.12.0,文化=中性,PublicKeyToken=669e0ddf0bb1aa2a
  • log4net,版本=1.2.10.0,文化=中性,PublicKeyToken=1b44e1d426115821

  • 当我打开它们时,前两个有“手术成功”。作为文本的第二行。第三个有'操作失败。绑定(bind)结果:hr = 0x80070002。该系统找不到指定的文件。'

    当我深入研究该日志时,我发现:
    LOG: Assembly download was successful. Attempting setup of file: C:\windows\system32\windowspowershell\v1.0\Modules\JLP.GoogleAdminSDKDirectory.Admin\log4net.dll
    LOG: Entering run-from-source setup phase.
    LOG: Assembly Name is: log4net, Version=1.2.12.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a
    WRN: Comparing the assembly name resulted in the mismatch: Build Number
    ERR: The assembly reference did not match the assembly definition found.

    我尝试再次从 Visual Studio 运行它。 fuslogvw 中也有三个条目,第三个有相同的错误。但是 VS 输出中没有任何内容表明存在问题。

    最佳答案

    事实证明,具有不同 PublicKeyTokens 的两个版本的 log4net 给其他人带来了问题。我通过将 log4netwith PublicKeyToken=1b44e1d426115821 的版本添加到 GAC 并将另一个 bindingredirect 添加到配置文件中来使其工作,因为版本不匹配。

    您可以找到 log4net 版本 here .您将看到对 oldkey 和 newkey 版本的引用。 oldkey 有 PublicKeyToken=1b44e1d426115821 所以我只是跑了

    gacutil /i "C:\Users\Me\Downloads\log4net\log4net-1.2.12-bin-oldkey\log4net-1.2.12\b
    in\net\4.0\release\log4net.dll"

    将其粘贴在 GAC 中并添加:
    <dependentAssembly>
    <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.2.12.0" newVersion="1.2.12.0" />
    </dependentAssembly>

    到配置文件中的 assemblyBindings。

    关于powershell - 'DotNetOpenAuth.Logger' 的类型初始化程序引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343991/

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