0

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.

Gener4tor
  • 414
  • 3
  • 12
  • 40
  • Please show us your `.csproj`. – Dai Jun 20 '23 at 08:17
  • 1
    The error is clear - you're using the wrong library. Instead of the .NET Core library, you're using an old .NET Framework library. Typically applications *don't* generate the client code manually, they add a Service Reference. Visual Studio then generates the proxy code from the service's WSDL – Panagiotis Kanavos Jun 20 '23 at 08:24
  • `True` - someone's a glutton for punishment... – Dai Jun 20 '23 at 08:26
  • Remove the reference to `System.ServiceModel` and try adding a Service Reference instead. Named Pipes may be an issue as support was added in .NET 6 [in May 2023](https://devblogs.microsoft.com/dotnet/wcf-client-60-has-been-released/). If VS can't add a Named Pipes you may have to write the code yourself *after* adding the necessary NuGet packages – Panagiotis Kanavos Jun 20 '23 at 08:30
  • The code was created manually without any kind of wizard. After removing System.ServiceModel and adding the nuget-package: System.ServiceModel.NetNamedPipe the code will compile, but at runtime throws another exception. – Gener4tor Jun 20 '23 at 13:05
  • It seems you are trying to make inter-process communication on the same machine. If you are not constrained to WCF or `NetNamedPipeBinding` stuff, you may want to use [MemoryMappedFile Class](https://learn.microsoft.com/en-us/dotnet/api/system.io.memorymappedfiles.memorymappedfile?view=net-7.0) or [System.IO.Pipes Namespace](https://learn.microsoft.com/en-us/dotnet/api/system.io.pipes?view=net-7.0). – Mustafa Özçetin Jun 20 '23 at 13:41
  • @Mustafa Özçetin: The problem is, that the other side of the communication (the service) was already deployed a lot of times. And it works already. This is only a problem in projects created with Visual studio 2022. – Gener4tor Jun 20 '23 at 13:45
  • Do you *have to* create your new WPF app with .NET 6? You can still create a WPF app targeting .NET *Framework* with VS 2022. – Mustafa Özçetin Jun 20 '23 at 14:13
  • @Mustafa Özçetin: This is just a test application. At the end I want to use the function in a ArcGIS Pro AddIn. And for this there seems to be no other way than net6.0-windows as a target network. – Gener4tor Jun 20 '23 at 15:01
  • @Gener4tor You can use ArcGIS from within WinForms-for-.NET4.8 just fine - I don't know where you're getting the that you have to use .NET 6 from... heck, even a console application would be a much better starting point: considerably less complexity and dependencies - and things that can go wrong. Besides, as said, your `` line is completely wrong. – Dai Jun 20 '23 at 22:23

1 Answers1

0

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.

Gener4tor
  • 414
  • 3
  • 12
  • 40