microsoft / Power-Fx

Power Fx low-code programming language
MIT License
3.18k stars 317 forks source link

Error using Power Fx nuget packages with Dataverse dependent assemblies #691

Closed filcole closed 1 year ago

filcole commented 1 year ago

When including Microsoft.PowerFx.Core and Microsoft.PowerFx.Interpreter in a dataverse plugin via dependent assemblies I get the following error when the plugin is run within Dataverse: (occurs with version 0.2.3-preview)

Message: The type initializer for 'Microsoft.PowerFx.Core.Types.Enums.EnumStoreBuilder' threw an exception.
            //   System.Resources.MissingSatelliteAssemblyException: The satellite assembly named "Microsoft.PowerFx.Core.resources.dll, PublicKeyToken=31bf3856ad364e35" for fallback culture "en-US" either could not be found or could not be loaded. This is generally a setup problem. Please consider reinstalling or repairing the application.

I get this immediately upon instantiating the engine, e.g. var engine = new RecalcEngine();

To work around this I had to copy Microsoft.PowerFx.Core.resources.dll from the nuget cache to the plugin project folder, and then add the following to the .csproj

  <ItemGroup>
    <None Update="Microsoft.PowerFx.Core.resources.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

I think there might be a glitch in the Power Fx nuget packaging, because I can use other packages via dependent assemblies successfully. (e.g. Newtonsoft.Json, YamlDotNet)

MikeStall commented 1 year ago

The issue here is that Power Fx uses satellite assemblies for resources. Dependent assemblies require those be copied manually.

https://learn.microsoft.com/en-us/power-apps/developer/data-platform/dependent-assembly-plugins#add-a-file-resource

Here's a technique to get the path: https://patriksvensson.se/posts/2019/09/how-to-find-a-nuget-package-path-from-msbuild

Here's a snippet we used for one of our plugins using power fx:

  <ItemGroup>
      <!-- Plex requires the satellite resource dll be copied to the output directory. 
       $(PkgMicrosoft_PowerFx_Core) var is set by GeneratePathProperty=true on the PackageReference. -->
      <None Include="$(PkgMicrosoft_PowerFx_Core)\lib\netstandard2.0\en-US\Microsoft.PowerFx.Core.resources.dll" Link="Microsoft.PowerFx.Core.resources.dll">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
  </ItemGroup>
filcole commented 1 year ago

Thanks @MikeStall. Seems to be working great.

MikeStall commented 1 year ago

Great. curious - what are you working on?

AlaJaber97 commented 1 month ago

this is not work when use this solution to generate WebAssambly files and call it from Javascript image