3

I'm working on a WPF project that targets .NET Framework 4.8

1/

Now I'm researching about running that application with .NET 7 but is it possible to do that?

For example, I tried dotnet wpf.exe but it shown some errors

A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Users\admin\Documents\Debug\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'C:\Users\admin\Documents\Debug\wpf.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'C:\Users\admin\Documents\Debug\wpf.runtimeconfig.json' file and specify the appropriate framework.

This is some info about the environment (from dotnet --info)

I've installed a new Windows 10 22H2 so I think .NET Framework 4.8 is included

.NET SDK:
 Version:   7.0.101
 Commit:    bb24aafa11

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.101\

Host:
  Version:      7.0.1
  Architecture: x64
  Commit:       97203d38ba

.NET SDKs installed:
  7.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

2/

If the above is impossible, is there a way to build the .NET Framework project so it can run with .NET 7 without changing (migrating) the current project structure? (Something like I can continue working on .NET Framework project as before then use a specific dotnet command to build it to .NET 7 executable. Assuming all packages are compatible with .NET 7)

IFZR
  • 316
  • 1
  • 12
  • 1
    Just a comment, since I don't know whether 1 or 2 are possible. However, I can offer option 3 as a workaround: Create a new .NET 7 project in the same solution as your .NET Framework 4.8 project and then move your code into a so-called [shared project](https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/shared-projects?tabs=windows). This allows you to use the same code-base for both the .NET Framework 4.8 and the .NET 7 version of your project. If there are cases where different code is required, you can use conditional compilation directives. – Heinzi Jan 04 '23 at 08:17
  • 2
    To get a .NET 7 build, your project must target .NET 7. However, you can multi-target your project so that it produces both .NET 4.8 AND .NET 7 outputs. You might find [this blog post](https://weblog.west-wind.com/posts/2017/jun/22/multitargeting-and-porting-a-net-library-to-net-core-20) useful. It's about multi-targeting .NET Core 2.0 and .NET Framework, but the principles apply to multi-targeting .NET 7 and .NET Framework. – Matthew Watson Jan 04 '23 at 09:16
  • Wouldn't it just be simpler to migrate to net 7? – Andy Jan 04 '23 at 09:16
  • [Here](https://github.com/koszeggy/KGySoft.Drawing.Tools/blob/master/DebuggerVisualizers/Wpf/KGySoft.Drawing.DebuggerVisualizers.Wpf.Test/KGySoft.Drawing.DebuggerVisualizers.Wpf.Test.csproj) is a simple example of a WPF app with multiple targets. The only trick is that you should add the `true` element and the target framework moniker must have the `-windows` postfix when targeting .NET 5 or above with WPF. – György Kőszeg Jan 04 '23 at 10:33

1 Answers1

1

Now I'm researching about running that application with .NET 7 but is it possible to do that?

No. A WPF application that targets the .NET Framework cannot run on the .NET Core/5/6/7 runtime.

You will either have to migrate your application to .NET 7 or stick with running it on the old .NET Framework runtime.

You may target multiple frameworks using an SDK-style project but that's another story. If you want to take advantage of the features available in .NET 7, you will have to migrate and this includes the project file as well. You can still be compatible with .NET Framework if you want though.

But there is no option of doing nothing with the code base and simply run the .NET Framework app as-is on .NET Core or later versions.

The migration itself doesn't necessarily have to be that tricky depending on your code base. Please refer to the link above for more information.

mm8
  • 163,881
  • 10
  • 57
  • 88