Why System.ServiceModel does not work with a WPF Project created with Visual studio 2022?

418 Views Asked by At

Im my WPF Application created with visual studio I want to communicate via named pipes with this (manually created) code:

var pipeFactory = new ChannelFactory<IWCFStarter>(new NetNamedPipeBinding(),
                                                  new EndpointAddress("net.pipe://localhost/someadress"));
IWCFStarter pipeChannel = pipeFactory.CreateChannel();
bool result = false;
result = pipeChannel.UpdateArcGISProAddOn("ARCBAR", "{294628d0-3e47-4c2a-8b0c-a14fb5496711}");

This code works fine with a WPF app created with a older version of Visual studio.

But with the new WPF App (created with Visual studio 2022) the following exception is thrown at runtime:

System.BadImageFormatException
  HResult=0x80131058
  Message=Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (0x80131058)
  Source=ARCBARUnittests

Inner Exception 1:
BadImageFormatException: Cannot load a reference assembly for execution.

Why is this? The Assembly System.ServiceModel is visible in the project unter Dependencies/Assemblies.

My .csproj-File looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <NeutralLanguage>de-AT</NeutralLanguage>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Landesversionsdienst\WCFContracts\WCFContracts.csproj" />
    <ProjectReference Include="..\Framework\AdresssucheUT\AdresssucheUT.csproj" />
    <ProjectReference Include="..\Framework\Adresssuche\Adresssuche.csproj" />
    <ProjectReference Include="..\Framework\ExceptionHandlingUT\ExceptionHandlingUT.csproj" />
    <ProjectReference Include="..\Framework\LoggingUT\LoggingUT.csproj" />
    <ProjectReference Include="..\Framework\MyStringToolsUT\MyStringToolsUT.csproj" />
    <ProjectReference Include="..\Framework\OSToolsUT\OSToolsUT.csproj" />
    <ProjectReference Include="..\Framework\RadBaseGUIElementsUT\RadBaseGUIElementsUT.csproj" />
    <ProjectReference Include="..\Framework\RadWeb\RadWeb.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="System.ServiceModel">
      <HintPath>..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.ServiceModel.dll</HintPath>
    </Reference>
  </ItemGroup>

I tried:

  • Removing 'System.ServiceModel' from the references and addes the nuget-package 'System.ServiceModel.NetNamedPipe':

The code compiles, but at runtime throws this exception.

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

The same error after installing the nuGet packages: 'System.ServiceModel.Primitives' and 'System.Private.ServiceModel'. Installing the packages 'System.ServiceModel.Duplex', 'System.ServiceModel.Http', 'System.ServiceModel.NetTcp' and 'System.ServiceModel.Security' did also not work.

1

There are 1 best solutions below

0
On BEST ANSWER

The problem was another reference to System.ServiceModel in a referenced project.

So at the end I removed all references to: System.ServiceModel from all projects in the workspace. Then I added the nuget-Packages:

  • System.ServiceModel.NetNamedPipe and
  • System.ServiceModel.Primitives

So the code compiles and runs.