TypeLoadException when using WithHeader method. #508

Closed
opened 2025-12-29 08:29:16 +01:00 by adam · 19 comments
Owner

Originally created by @eshref on GitHub (May 4, 2023).

Originally assigned to: @StefH on GitHub.

Hi, friends.

I am getting

System.TypeLoadException
Could not load type 'FluentAssertions.Collections.SelfReferencingCollectionAssertions`2' from assembly 'FluentAssertions, Version=6.6.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a'.
   at WireMock.FluentAssertions.WireMockAssertions.WithHeader(String expectedKey, String[] expectedValues, String because, Object[] becauseArgs)

error when I use below assertion code in my test which is net472 project.

_wireMockServer
                .Should()
                .HaveReceivedACall()
                .AtAbsoluteUrl("some-url").And
                .WithHeader("header-name", "header-value");

I am using below versions of packages in test project.

image

Any help is appreciated.

Originally created by @eshref on GitHub (May 4, 2023). Originally assigned to: @StefH on GitHub. Hi, friends. I am getting ``` System.TypeLoadException Could not load type 'FluentAssertions.Collections.SelfReferencingCollectionAssertions`2' from assembly 'FluentAssertions, Version=6.6.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a'. at WireMock.FluentAssertions.WireMockAssertions.WithHeader(String expectedKey, String[] expectedValues, String because, Object[] becauseArgs) ``` error when I use below assertion code in my test which is net472 project. ``` csharp _wireMockServer .Should() .HaveReceivedACall() .AtAbsoluteUrl("some-url").And .WithHeader("header-name", "header-value"); ``` I am using below versions of packages in test project. ![image](https://user-images.githubusercontent.com/34239435/236134975-cd877581-e51b-4fc7-92fe-bfdb0b8a8e8b.png) Any help is appreciated.
adam added the bug label 2025-12-29 08:29:16 +01:00
adam closed this issue 2025-12-29 08:29:16 +01:00
Author
Owner

@StefH commented on GitHub (May 12, 2023):

Can you please provide the full project ?

@StefH commented on GitHub (May 12, 2023): Can you please provide the full project ?
Author
Owner

@StefH commented on GitHub (May 31, 2023):

@eshref
Can you please provide a full project with this error?

@StefH commented on GitHub (May 31, 2023): @eshref Can you please provide a full project with this error?
Author
Owner

@eshref commented on GitHub (Jun 1, 2023):

Hi, @StefH. Sorry for late response.

Here is my csproj:

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

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <NoWarn>0067</NoWarn>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="WireMock.Net" Version="1.5.13" />
    <PackageReference Include="FluentAssertions" Version="6.6.0" />
    <PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.13" />
        <PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.13" />
    <PackageReference Include="xunit" Version="2.4.2" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.2.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

And this is test class:

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

using FluentAssertions;

using WireMock.FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

using Xunit;


namespace TestProject1
{
    public class UnitTest1 : IDisposable
    {
        private readonly WireMockServer _wireMockServer;


        public UnitTest1()
        {
            _wireMockServer = WireMockServer.Start(8081);
        }


        public void Dispose()
        {
            _wireMockServer.Stop();
            _wireMockServer.Dispose();
        }


        [Fact]
        public async Task Test1()
        {
            // arrange
            var httpClient = new HttpClient();

            _wireMockServer.Given(
                    Request.Create()
                        .WithPath("/someurl")
                        .UsingPost())
                .RespondWith(
                    Response.Create()
                        .WithStatusCode(HttpStatusCode.OK));

            // act
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:8081/someurl")
            {
                Headers =
                {
                    { "header-name", "header-value" }
                }
            };

            HttpResponseMessage response = await httpClient.SendAsync(httpRequestMessage);

            // assert
            response.StatusCode.Should().Be(HttpStatusCode.OK);

            _wireMockServer
                .Should()
                .HaveReceivedACall()
                .AtAbsoluteUrl("http://localhost:8081/someurl").And
                .WithHeader("header-name", "header-value");
        }
    }
}
@eshref commented on GitHub (Jun 1, 2023): Hi, @StefH. Sorry for late response. Here is my csproj: ``` <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net472</TargetFramework> <NoWarn>0067</NoWarn> </PropertyGroup> <ItemGroup> <PackageReference Include="WireMock.Net" Version="1.5.13" /> <PackageReference Include="FluentAssertions" Version="6.6.0" /> <PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.13" /> <PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.13" /> <PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="coverlet.collector" Version="3.2.0"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> </ItemGroup> </Project> ``` And this is test class: ``` using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; using FluentAssertions; using WireMock.FluentAssertions; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; using Xunit; namespace TestProject1 { public class UnitTest1 : IDisposable { private readonly WireMockServer _wireMockServer; public UnitTest1() { _wireMockServer = WireMockServer.Start(8081); } public void Dispose() { _wireMockServer.Stop(); _wireMockServer.Dispose(); } [Fact] public async Task Test1() { // arrange var httpClient = new HttpClient(); _wireMockServer.Given( Request.Create() .WithPath("/someurl") .UsingPost()) .RespondWith( Response.Create() .WithStatusCode(HttpStatusCode.OK)); // act var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:8081/someurl") { Headers = { { "header-name", "header-value" } } }; HttpResponseMessage response = await httpClient.SendAsync(httpRequestMessage); // assert response.StatusCode.Should().Be(HttpStatusCode.OK); _wireMockServer .Should() .HaveReceivedACall() .AtAbsoluteUrl("http://localhost:8081/someurl").And .WithHeader("header-name", "header-value"); } } } ```
Author
Owner

@StefH commented on GitHub (Jun 1, 2023):

Using your slightly modified csproj and the same test file it works fine in VS2022.

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

    <PropertyGroup>
        <TargetFramework>net472</TargetFramework>
        <NoWarn>0067</NoWarn>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="WireMock.Net" Version="1.5.13" />
        <PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.13" />
        <PackageReference Include="xunit" Version="2.4.2" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="coverlet.collector" Version="3.2.0">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
    </ItemGroup>

</Project>

image

@StefH commented on GitHub (Jun 1, 2023): Using your slightly modified csproj and the same test file it works fine in VS2022. ``` xml <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net472</TargetFramework> <NoWarn>0067</NoWarn> </PropertyGroup> <ItemGroup> <PackageReference Include="WireMock.Net" Version="1.5.13" /> <PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.13" /> <PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="coverlet.collector" Version="3.2.0"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> </ItemGroup> </Project> ``` ![image](https://github.com/WireMock-Net/WireMock.Net/assets/249938/e28e547d-029b-49fd-9811-4791770d6a6a)
Author
Owner

@eshref commented on GitHub (Jun 1, 2023):

Why you removed FluentAssertions 6.6.0? We use this version of FA package.

@eshref commented on GitHub (Jun 1, 2023): Why you removed FluentAssertions 6.6.0? We use this version of FA package.
Author
Owner

@StefH commented on GitHub (Jun 1, 2023):

Ah I see. That causes probably the issue.

Version 5 and 6 from FluentAssertions are not compatible.

The only thing what can be done is that I create 2 packages for WireMock.Net.FluentAssertions, one with 5 and one with 6.

@StefH commented on GitHub (Jun 1, 2023): Ah I see. That causes probably the issue. Version 5 and 6 from FluentAssertions are not compatible. The only thing what can be done is that I create 2 packages for WireMock.Net.FluentAssertions, one with 5 and one with 6.
Author
Owner

@eshref commented on GitHub (Jun 1, 2023):

Wouldn't it be better to to upgrade FA dependency and release new breaking change instead of supporting two packages?

@eshref commented on GitHub (Jun 1, 2023): Wouldn't it be better to to upgrade FA dependency and release new breaking change instead of supporting two packages?
Author
Owner

@StefH commented on GitHub (Jun 1, 2023):

The issue is that version 6 is not supported by lower frameworks. But I can make a workaround for that I think.

@StefH commented on GitHub (Jun 1, 2023): The issue is that version 6 is not supported by lower frameworks. But I can make a workaround for that I think.
Author
Owner

@StefH commented on GitHub (Jun 1, 2023):

Can you try preview version 1.5.26-ci-17452 ?

@StefH commented on GitHub (Jun 1, 2023): Can you try preview version 1.5.26-ci-17452 ?
Author
Owner

@eshref commented on GitHub (Jun 2, 2023):

Did you push this version to nuget? I am getting below warnings after manually updating versions:

warning NU1603: TestProject1 depends on WireMock.Net (>= 1.5.26-ci-17452) but WireMock.Net 1.5.26-ci-17452 was not found. An approximate best match of WireMock.Net 1.5.26 was resolved.
warning NU1603: TestProject1 depends on WireMock.Net.FluentAssertions (>= 1.5.26-ci-17452) but WireMock.Net.FluentAssertions 1.5.26-ci-17452 was not found. An approximate best match of WireMock.Net.FluentAssertions 1.5.26 was resolved.
@eshref commented on GitHub (Jun 2, 2023): Did you push this version to nuget? I am getting below warnings after manually updating versions: ``` warning NU1603: TestProject1 depends on WireMock.Net (>= 1.5.26-ci-17452) but WireMock.Net 1.5.26-ci-17452 was not found. An approximate best match of WireMock.Net 1.5.26 was resolved. warning NU1603: TestProject1 depends on WireMock.Net.FluentAssertions (>= 1.5.26-ci-17452) but WireMock.Net.FluentAssertions 1.5.26-ci-17452 was not found. An approximate best match of WireMock.Net.FluentAssertions 1.5.26 was resolved. ```
Author
Owner

@StefH commented on GitHub (Jun 2, 2023):

It is on myget.

https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH commented on GitHub (Jun 2, 2023): It is on myget. https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

@eshref commented on GitHub (Jun 2, 2023):

Added this package and run test, same problem occured.

@eshref commented on GitHub (Jun 2, 2023): Added this package and run test, same problem occured.
Author
Owner

@StefH commented on GitHub (Jun 2, 2023):

Can you try
1.5.26-ci-17457

@StefH commented on GitHub (Jun 2, 2023): Can you try 1.5.26-ci-17457
Author
Owner

@eshref commented on GitHub (Jun 2, 2023):

This one worked, thanks. 🙏🏼

@eshref commented on GitHub (Jun 2, 2023): This one worked, thanks. 🙏🏼
Author
Owner

@eshref commented on GitHub (Jun 2, 2023):

I would be glad, if you share solution :)

@eshref commented on GitHub (Jun 2, 2023): I would be glad, if you share solution :)
Author
Owner

@StefH commented on GitHub (Jun 2, 2023):

I would be glad, if you share solution :)

You mean a new official NuGet ?

@StefH commented on GitHub (Jun 2, 2023): > I would be glad, if you share solution :) You mean a new official NuGet ?
Author
Owner

@eshref commented on GitHub (Jun 2, 2023):

I would be glad, if you share solution :)

You mean a new official NuGet ?

Yes, but also I meant what changes did you do in this myget package? Only above changes (https://github.com/WireMock-Net/WireMock.Net/pull/949)

@eshref commented on GitHub (Jun 2, 2023): > > I would be glad, if you share solution :) > > You mean a new official NuGet ? Yes, but also I meant what changes did you do in this myget package? Only above changes (https://github.com/WireMock-Net/WireMock.Net/pull/949)
Author
Owner

@StefH commented on GitHub (Jun 2, 2023):

That commit was actually not correct.

I did a commit op master which solved it.

@StefH commented on GitHub (Jun 2, 2023): That commit was actually not correct. I did a commit op master which solved it.
Author
Owner

@eshref commented on GitHub (Jun 2, 2023):

Thanks for your quick help.

@eshref commented on GitHub (Jun 2, 2023): Thanks for your quick help.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#508