Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assembly with same name is already loaded exception when run second script #671

Closed
nowwwak opened this issue May 13, 2022 · 1 comment
Closed

Comments

@nowwwak
Copy link

nowwwak commented May 13, 2022

Hi, when I am running 2 scripts in a row I am getting FileLoadException "Assembly with same name is already loaded".
I am using:

  • nuget package Dotnet.Script 1.3.1
  • VS2022
  • .NET 6.0

In order to reproduce this issue simply call ExecuteScriptCommand.Run for two different scripts and second call should throw exception.

Sample code that shows this issue:

  1. Program.cs
using Dotnet.Script.Core;
using Dotnet.Script.Core.Commands;
using Dotnet.Script.DependencyModel.Logging;
using DotnetScriptNet6;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using Microsoft.Extensions.Logging.Abstractions;

var dotnetScriptLogHelper = new DotnetScriptLogHelper(new NullLoggerFactory());
LogFactory logFactory = dotnetScriptLogHelper.CreateLogFactory();

var fileCommand = new ExecuteScriptCommand(ScriptConsole.Default, logFactory);

var fileCommandOptions1 = new ExecuteScriptCommandOptions
(
    new ScriptFile(@"script1\script1.csx"),
    Array.Empty<string>(),
    OptimizationLevel.Release,
    Array.Empty<string>(),
    false,
    true
);

_ = fileCommand.Run<int, CommandLineScriptGlobals>(fileCommandOptions1).Result;

var fileCommandOptions2 = new ExecuteScriptCommandOptions
(
    new ScriptFile(@"script2\script2.csx"),
    Array.Empty<string>(),
    OptimizationLevel.Release,
    Array.Empty<string>(),
    false,
    true
);

_ = fileCommand.Run<int, CommandLineScriptGlobals>(fileCommandOptions2).Result;
  1. script1\script1.csx
    Console.WriteLine("Executed script1.csx");

  2. script2\script2.csx
    Console.WriteLine("Executed script2.csx");

  3. DotnetScriptNet6.csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <None Include="..\.editorconfig" Link=".editorconfig" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Dotnet.Script" Version="1.3.1" />
  </ItemGroup>

  <ItemGroup>
    <None Update="script1\script1.csx">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="script2\script2.csx">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

Exception details:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (Assembly with same name is already loaded)
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at Program.<Main>$(String[] args) in C:\Projects\testapps\DotnetScriptNet6\DotnetScriptNet6\Program.cs:line 36

  This exception was originally thrown at this call stack:
    System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(string)
    System.Reflection.Assembly.LoadFrom(string)
    Gapotchenko.FX.Reflection.AssemblyLoadPal.LoadFrom(string)
    Dotnet.Script.Core.ScriptRunner.Execute<TReturn>(string, System.Collections.Generic.IEnumerable<string>) in ScriptRunner.csx
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.TaskAwaiter<TResult>.GetResult()
    Dotnet.Script.Core.Commands.ExecuteLibraryCommand.Execute<TReturn>(Dotnet.Script.Core.Commands.ExecuteLibraryCommandOptions) in ExecuteLibraryCommand.csx
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    ...
    [Call Stack Truncated]

Inner Exception 1:
FileLoadException: Assembly with same name is already loaded

@seesharper
Copy link
Collaborator

If you add the ScriptAssemblyLoadContext to the options like this it should work

var fileCommandOptions2 = new ExecuteScriptCommandOptions
(
    new ScriptFile(@"script2\script2.csx"),
    Array.Empty<string>(),
    OptimizationLevel.Release,
    Array.Empty<string>(),
    false,
    true
)
{
    AssemblyLoadContext = new ScriptAssemblyLoadContext()
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants