Using Playwright for .NET with Native AOT

132 Views Asked by At

I would like to publish my console app using Playwright for .NET as Native AOT produces.

The source code for the program is as follows:

using Microsoft.Playwright;

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new() {
    Args = new[] { "--profile-directory=Default" },
    Channel = "msedge",
    ExecutablePath = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe",
    Headless = false
});
var page = await browser.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet");

When I run the application I created, I get the following error:

Unhandled Exception: System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
   at System.Reflection.Runtime.Assemblies.RuntimeAssemblyInfo.get_CodeBase() + 0x2b
   at Microsoft.Playwright.Helpers.Driver.GetExecutablePath() + 0xa1
   at Microsoft.Playwright.Transport.StdIOTransport.GetProcess() + 0x37
   at Microsoft.Playwright.Transport.StdIOTransport..ctor() + 0x56
   at Microsoft.Playwright.Playwright.<CreateAsync>d__0.MoveNext() + 0x4d
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b
   at Program.<<Main>$>d__0.MoveNext() + 0xd4
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b
   at Program.<Main>(String[] args) + 0x24
   at Playwright!<BaseAddress>+0x279c30

I followed these steps below to create the project and .exe file:

dotnet new console -o Playwright --aot
cd Playwright
dotnet add package Microsoft.Playwright
dotnet publish -r win-x64 -c Release /p:IncludeAllContentForSelfExtract=true

The contents of the .csproj file are as follows:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Playwright" Version="1.40.0" />
  </ItemGroup>

</Project>

My environment is as follows:

  • OS: Microsoft Windows 10 Home 22H2 64 bit
  • .NET: 8.0.100
  • Playwright: 1.40.0
  • Programming language: C#

Is it possible to publish a console app using Playwright for .NET as a Native AOT produces?

0

There are 0 best solutions below