Deffiss / testenvironment-docker

MIT License
117 stars 30 forks source link

Update to .NET6 #58

Closed alan994 closed 2 years ago

alan994 commented 2 years ago

I'm trying to run this with .NET 6. Lib works, but when I try to instantiate ServiceProvider I get a conflict error because TestEnvironment.Docker is using the 5.0.0 version of Microsoft Extension libraries.

Error CS0433 The type 'ServiceCollection' exists in both 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'Microsoft.Extensions.DependencyInjection, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'   Message.BrokerTest

Is there any plan on updating this?

Deffiss commented 2 years ago

Hi @alan994 , library will be upgraded to net6 for sure. Let me investigate a little bit this issue to understand if there are potential workarounds.

Deffiss commented 2 years ago

I've just tried to run tests on .NET 6 with the following code:

var s = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
s.AddScoped(typeof(DockerEnvironmentBuilder));
var sp = s.BuildServiceProvider();

And had no issues. Could you please add more details about your specific case?

alan994 commented 2 years ago

Hi @Deffiss, I have net6.0 lib project that has references to .net6 versions of Microsoft libs.

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
    <PackageReference Include="NATS.Client" Version="0.14.3" />
    <PackageReference Include="System.Text.Json" Version="6.0.1" />
  </ItemGroup>

</Project>

Then I have xUnit project that has reference to your lib and my project. When I tried to add new ServiceCollection in one of my test classes (in constructor) I got this error.

I managed to workaround this by using aliases. Now my xUnit project looks like this.

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

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>

        <IsPackable>false</IsPackable>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
        <PackageReference Include="TestEnvironment.Docker" Version="2.0.0" />
        <PackageReference Include="xunit" Version="2.4.1" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="coverlet.collector" Version="3.1.0">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="../../src/Message.Broker/Message.Broker.csproj" />
    </ItemGroup>

    <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
        <ItemGroup>
            <ReferencePath Condition="'%(FileName)' == 'Microsoft.Extensions.DependencyInjection.Abstractions'">
                <Aliases>BetaLib</Aliases>
            </ReferencePath>
        </ItemGroup>
    </Target>

    <ItemGroup>
        <None Update="appSettings.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
        <None Update="Dockerfile">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    </ItemGroup>
</Project>

And my test class looks something like this.

extern alias BetaLib;
using Beta = BetaLib.Microsoft.Extensions.DependencyInjection;

using Message.Broker;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TestEnvironment.Docker;
using TestEnvironment.Docker.ContainerLifecycle;
using Xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

namespace Message.BrokerTest;

public class UnitTest1 : IDisposable
{
    IDockerEnvironment env;
    IServiceProvider serviceProvider;
    IConfiguration configuration;
    public UnitTest1()
    {
        var ports = new Dictionary<ushort, ushort>();
        ports.Add(4222, 4222);
        ports.Add(8222, 8222);
        ports.Add(6222, 6222);
        var id = "pero";
        env = new DockerEnvironmentBuilder()    
            .SetName($"Test-{id}")
            .AddFromDockerfile(p => p with
            {

                Name = $"Nats-{id}",
                Dockerfile = "Dockerfile",
                Ports = ports,
                ContainerWaiter = new HttpContainerWaiter("http://localhost", port: 8222)
            })
            .Build();

        env.UpAsync().GetAwaiter().GetResult();

        this.configuration = new ConfigurationBuilder()
            .AddJsonFile("appSettings.json", optional: true, reloadOnChange: true)
            .Build();

        this.serviceProvider = new Beta.ServiceCollection()
            .AddNats(configuration)
            .BuildServiceProvider();

    }

    public void Dispose()
    {
        this.env.DownAsync().GetAwaiter().GetResult();
    }

    private void EnsureStream()
    {

    }

    [Fact]
    public async Task Test1()
    {
        var sender = (IMessageSender)this.serviceProvider.GetService(typeof(IMessageSender));
        await sender.SendMessage("pero", new { FirstName = "Alan", LastName = "Jagar" });
        //TODO...
       Assert.True(true);
    }
}

BetaLib is just a random name it can be anything.

Deffiss commented 2 years ago

Not sure that you need explicit references to:

    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
     <PackageReference Include="System.Text.Json" Version="6.0.1" />

They should be referenced by Microsoft.NET.Sdk. Could you try to remove and try one more time?

alan994 commented 2 years ago

image image

When I comment them then build breaks :(

I think it is included in Microsoft.NET.Sdk.Web by default, but I'm not sure.

Deffiss commented 2 years ago

Seems you are right :( Any chance that you could change your project and test sdks to Microsoft.NET.Sdk.Web? It may solve the issue

alan994 commented 2 years ago

My issue is solved by using an alias :) I will just remove this code when a new version of your lib be available.

Deffiss commented 2 years ago

Great! I'll close the issue for now then.

Deffiss commented 2 years ago

Hi @alan994, .NET 6 support was added in #60, please check out the latest nuget package.

alan994 commented 2 years ago

Thanks for the info, I will update tomorrow