I want to execute C# code in a PS script by using Add-Type. But I receive:
Metadata file 'Microsoft.Azure.ServiceBus.dll' could not be found. The C# code uses a NuGet package called "Microsoft.Azure.ServiceBus".
My referenced assembly seems not to work. Here is my code:
PS
$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>
}
}
}
Whats the root of this issue and how can I fix it? Unfortunately I am not very familiar with PS nor C#.
The problem you're receiving is essentially:
File not found.The root cause is your relative path of the referenced assembly in -
since
Microsoft.Azure.ServiceBusisn't in the GAC.I suspect if you use
Set-Locationwhere theMicrosoft.Azure.ServiceBus.dllfile is, you will resolve your problem.