0

Whenever I try to run said function, I keep getting this particular exception:

EXCEPTION: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.

Does anyone know the fix for this? I'm running it in the Azure portal itself.

I have tried almost everything, and I can't seem to find a solution regarding this exception.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
keimpe
  • 1

1 Answers1

0
> EXCEPTION: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.

This error occurs due to the System.Windows.Forms not being installed as a package inside the Azure Powershell Function, You can add the namespace in your code like below:-

using namespace System.Net
using namespace System.Windows.Forms

Output:-

enter image description here

Visit App Files > select requirements.psd and add the Winforms Powershell Module like below:-

@{
     'Az' = '10.*'
     'WinFormPS' = '1.0.0'
}

enter image description here

If you are using a .Net Function app this error occurs if the Windows Forms module is not specified in your Csproj correctly, If you want to add Nuget package or want to edit Csproj directly from the Portal for your function app, Refer my SO thread answer here.

According to this Github issue answer by seriouz regarding same error code as yours,

You need to add the package reference of WinForms dll in your .csproj file like below:-

<ItemGroup>  
    <Reference Include="YourAssembly">
        <HintPath>Path/To/YourAssembly.dll</HintPath>     
     </Reference>
 </ItemGroup>

According to this SO thread answer by Baala Srinivas K:-

Add your framework reference like below:-

 <FrameworkReference Include="Microsoft.WindowsDesktop.App" />  

One more option is to build your Powershell or .Net Function in your Local VS Code and Visual Studio check all the dependencies and then publish those Functions in Azure Portal:-

Powershell:-

enter image description here

.Net :-

enter image description here

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11