Cannot load System.ServiceModel.Primitives.dll in UWP App

Abhishek 26 Reputation points
2020-11-25T18:42:39.457+00:00

Hello

Have a UWP app which references a .Net standard 2.0 library.

This library uses System.ServiceModel.Primitives nuget package version 4.8.0

When running the app, it throws the exception "Cannot load System.ServiceModel.Primitives.dll version 4.8.0"

Have installed this same package in UWP app as well but the latest file does not get copied over to the output folder.

Tried to use app.config binding redirection but it does not seem to work.

Kindly advise

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,346 questions
Universal Windows Platform (UWP)
{count} vote

2 answers

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,406 Reputation points
    2020-11-26T05:34:09.817+00:00

    Hi Abhishek,

    I think it is the special feature of uwp project and the nuget package. Also, the nuget package is a system nuget package, it will not be output on the bin folder for new standard project. You should add these in csproj file so that it will appear.

    Also, create a net standard lib project and a uwp project, install the same nuget package, use the same node, the uwp project still cannot output the dll. It is the special feature of the uwp project.

    As a suggestion,

    1. You can just copy the dll from C:\Users\'username'\.nuget\packages\system.servicemodel.primitives\4.8.0\lib into the bin folder of the uwp project.
    2. you can add an automation msbuild script

    Modify the csproj file of the uwp project

    add <GeneratePathProperty>true</GeneratePathProperty> into your project and then add a msbuild target:

    <ItemGroup>  
     <PackageReference Include="System.ServiceModel.Primitives">   
          <Version>4.8.0</Version>  
    <GeneratePathProperty>true</GeneratePathProperty>  
        </PackageReference>  
    </ItemGroup>  
    
     <Target Name="CopyTheFile" AfterTargets="AfterCompile" Condition="!Exists('$(TargetDir)System.ServiceModel.Primitives.dll')">   
    <ItemGroup>  
    <File Include="$(PkgSystem_ServiceModel_Primitives)\lib\netstandard2.0\System.ServiceModel.Primitives.dll"></File>  
    </ItemGroup>  
    <Copy SourceFiles="@(File)" DestinationFolder="$(TargetDir)"></Copy>  
    </Target>  
    

    Best Regards,
    Dylan


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our **documentation to enable e-mail notifications if you want to receive the related email notification for this thread.**

    0 comments No comments

  2. Abhishek 26 Reputation points
    2020-11-28T11:37:26.597+00:00

    Thanks Dylan.

    Implemented the automation build script as per above, and the file was successfully copied into the target folder.

    However, still getting error as follows
    System.IO.FileLoadException: 'Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.8.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

    Tried to make an app.config file and implement bindingRedirect -- still same issue.

    Pl advise