WinForms Migration First Steps

The following steps describe a basic migration of a Desktop WinForms application to a Web Application based on Wisej.NET.

With Wisej.NET, it's so much easier to migrate your existing WinForms application to a full fledged Web Application.

To do so, we have prepared this checklist to guide you through the process.

  1. Make sure to install Wisej.NET's VSIX package from Wisej.NET Build page

  2. Take note of all embedded resources, references and build customizations within the Project

Make sure that you have a backup ready of your application before proceeding with the migration process to prevent loss of critical data or source code.

From this point, you can choose one of two options to migrate your solution, either upgrading to the New Project SDK Format or staying with the current Project Format.

New Project SDK Format

Starting with Wisej.NET 3, we started supporting .NET 6. This enabled Wisej.NET to be cross-platform, and added many more features like Dependency Injection, running in a Docker Container, and much more.

You can read about everything new in Wisej.NET from HERE.

To start with the migration process, please proceed with the following steps:

  1. Unload the Project

  2. Delete the content of the .csproj file

  3. Copy the following text into the .csproj file

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFrameworks>net48;net6.0</TargetFrameworks>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <EmbeddedResourceUseDependentUponConvention>true</EmbeddedResourceUseDependentUponConvention>
    <RootNamespace>$(MSBuildProjectName.Replace(" ", "_").Replace("-", "_"))</RootNamespace>
    <NoWarn>CA1416</NoWarn>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))'=='net'">
    <OutputPath>bin\</OutputPath>
    <StartupObject></StartupObject>
    <OutputType>Library</OutputType>
    <RunCommand>$(ProgramFiles)\IIS Express\iisexpress.exe</RunCommand>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <RunArguments>/path:"$(MSBuildProjectDirectory)" /port:5000</RunArguments>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))'!='net'">
    <StartupObject>$(RootNamespace).Startup</StartupObject>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))'=='net'">
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Windows.Forms"><Aliases>swf</Aliases></Reference>
    <Reference Include="System.Data.DataSetExtensions" />
    <Compile Remove="Startup.cs" />
    <Content Include="Startup.cs"/>
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Themes\" />
  </ItemGroup>

  <ItemGroup>
    <Content Update="Default.json">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
    <Content Update="Web.config">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <None Remove="Default.html" />
    <None Remove="favicon.ico" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="Default.html">
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
    </Content>
    <Content Include="favicon.ico">
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Wisej-3" Version="3.0.*" />
    <PackageReference Include="System.Data.SqlClient" Version="4.*" />
  </ItemGroup>

</Project>

Make sure to add the missing project references, missing packages and/or assemblies

Make sure to include .NET Framework 4.8 (net48) in your TargetFrameworks parameter since it's needed for the designer. You can ommit .NET 6 (net6) if you have compatibility issues.

4. Add a Startup.cs or Startup.vb file to your project

5. Add the following text into Startup.cs / Startup.vb

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Wisej.Core;

namespace $safeprojectname$
{
	/// <summary>
	/// The Startup class configures services and the app's request pipeline.
	/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940.
	/// </summary>
	public class Startup
	{
		public Startup(IConfiguration configuration)
		{
			Configuration = configuration;
		}
		public IConfiguration Configuration { get; }

		public static void Main(string[] args)
		{
			var builder = WebApplication.CreateBuilder(new WebApplicationOptions
			{
				Args = args,
				WebRootPath = "./"
			});

			var app = builder.Build();
			app.UseWisej();
			app.UseFileServer();
			app.Run();
		}
	}
}

Do not forget to update your Namespace

Old Project Format

You can create a new Wisej.NET project and copy over all the files from the original Win-Forms application.

If you choose this approach then you can skip steps 1, 2 and 3 as the assemblies and the configuration files will be added automatically.

Or you can edit your existing WinForms project file and make the following changes:

1. Change the settings of the existing project. You can do this by editing the .csproj file in a text editor and make the following changes - Add the project type GUIDs under the ProjectGuid node: <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids> - Change the OutputType from WinExe to Library - Add the import nodes for the web applications under the main Project node: <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v14.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" /> Summary of changes:

2. Open the WinForms solution and add Wisej.NET's NuGet Package.

Make sure to check the target .NET framework version as it has to be .NET Framework 4.8 or you'll get errors upon installing the NuGet Package.

3. Add the configuration files to the project. You will need the following files: a. Default.html b. Default.json c. Web.config

All these files are generated by default when creating a new Wisej.NET project, so you can simply copy them over from a new project. You can find a detailed explanation of these here: http://wisej.com/docs/html/Configuration.htm

You might also want to adjust the Title in default.html to match with your migrated project.

Replacing WinForms Namespaces with Wisej.NET

1. Replace all occurrences of System.Windows.Forms with Wisej.Web

2. Build the application and resolve the compiler errors: in most of the cases you will get compiler errors due to some missing properties and/or methods that are obso-lete in Wisej. You can simply comment out these.

If you find many similiar code lines to clean up, regular expressions in Visual Studio Replace can come in quite handy. Use for example .*UseVisualStyleBackColor.*\n to remove all those lines as they are not needed anymore.

3. Change the Main method; in a typical WinForms application you probably had something like this in the Main method:

The first two lines can be commented out as they are not needed in a web appli-cation. The third one has to be changed to show instead the Login dialog:

3. Change the startup method in the Default.json file to the Main method from Pro-gram.cs:

4. Check the Output Path. It should just be \Bin. If it´s \Bin\Debug please change it to \Bin

If you have been using ConnectionStrings in App.Config, copy them over to Web.Config. (in configuration section).

5. You´re done. Congratulations. You should now be able to run your Wisje web application.

Last updated