0

我想使用 Add-Type 在 PS 脚本中执行 C# 代码。但我收到: Metadata file 'Microsoft.Azure.ServiceBus.dll' could not be found。C# 代码使用名为“Microsoft.Azure.ServiceBus”的 NuGet 包。

我引用的程序集似乎不起作用。这是我的代码:

附言

$cmd = {

$Assem = ( 
"Microsoft.Azure.ServiceBus", 
"netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"
) 

$Source = (Get-Content -Path "D:\myPath\csharp-code.cs" -ReadCount 0) -join "`n"

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp 
[ABC.Azure.ServiceBus.TopicSubscriber]::test()
}

# Execute script as Job to avoid "Add-Type"-conflict during development changes
$j = Start-Job -ScriptBlock $cmd

do 
{
  Receive-Job -Job $j
} while ( $j.State -eq "Running" )

C#

using Microsoft.Azure.ServiceBus;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace ABC.Azure.ServiceBus
{
  class TopicSubscriber
  {
    bool test()
    {
        <more code>
    }
  }
}

这个问题的根源是什么,我该如何解决?不幸的是,我对 PS 和 C# 都不是很熟悉。

4

1 回答 1

0

您收到的问题本质上是:File not found.

根本原因是您引用程序集的相对路径 -

$assemblies = @(
    'Microsoft.Azure.ServiceBus'
    'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
)

因为Microsoft.Azure.ServiceBus不在 GAC 中。

我怀疑如果您使用文件Set-Location所在的Microsoft.Azure.ServiceBus.dll位置,您将解决您的问题。

于 2018-10-01T13:47:57.580 回答