Alex
Alex

Reputation: 43

Setup Project does not include a library: System.DirectoryServices.AccountManagement is not supported on this platform

Edit: I've discovered that the Setup file was not generating the App.deps.json file and App.dll.config file.

  1. Without the first, the AccountManagement was not found.
  2. Without the .dll.config file - the app was not reading the config contents.

I have a WPF application, that uses the System.DirectoryServices.AccountManagement to access Active Directory.

When I build the app either in Debug or Release configuration - all is working just fine.

However, when I create a Setup Project, then there's an exception thrown:

It might be worth mentioning that it was previously a .NET5 app, which was upgraded to .NET6 using the Microsoft upgrader.

Here is the Project properties:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <ApplicationIcon>Assets\rfid.ico</ApplicationIcon>
  </PropertyGroup>
  <ItemGroup>
    <None Remove="Assets\logo-white.png" />
    <None Remove="Assets\logo.png" />
    <None Remove="Assets\user.svg" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="MyCompany.DutScanner" Version="5.2.1" />
    <PackageReference Include="MyCompany.TestSystemLogger" Version="1.2.1" />
    <PackageReference Include="LoadingSpinner.WPF" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1" />
    <PackageReference Include="Serilog" Version="2.11.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
    <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.336902">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0" />
  </ItemGroup>
  <ItemGroup>
    <Resource Include="Assets\logo-white.png" />
    <Resource Include="Assets\logo.png" />
    <Resource Include="Assets\user.svg" />
  </ItemGroup>
  <ItemGroup>
    <None Update="App.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\PublishProfiles\" />
  </ItemGroup>
  <ItemGroup>
    <None Include="..\.editorconfig" Link=".editorconfig" />
  </ItemGroup>
</Project>

Yet, an exception is thrown whenever an attempt to make connection to the ActiveDirectory is made:

Exception: System.DirectoryServices.AccountManagement is not supported on this platform

Frameworks installed on the machine:

Machine: Windows 10, x64

What I have attempted so far:

the App.deps.json file contents:

https://pastebin.com/Ew7v4bFK

Please help! :/

Upvotes: 2

Views: 3598

Answers (1)

Alex
Alex

Reputation: 43

Okay, I got it working. Posting the solution here for anyone, who might have a similar issue in the future.

  1. The App.deps.json file can be generated by changing PreserveCompilationContext to true in the .csproj file. By default it is turned off to false. A few links: Why is aspnetcore looking for deps.json in production env? Runtime Configuration Files What is deps.json, and how do I make it use relative paths?

  2. the .dll.config file can be also turned on by changing these settings:

<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateDependencyFile>true</GenerateDependencyFile>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

The four magical lines that saved me are at the end:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWPF>true</UseWPF>
        <ApplicationIcon>Assets\rfid.ico</ApplicationIcon>

        <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
        <GenerateDependencyFile>true</GenerateDependencyFile>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
        <PreserveCompilationContext>true</PreserveCompilationContext>
    </PropertyGroup>
...
</Project>

Upvotes: 2

Related Questions