Why is IRequestMessage.BodyAsMimeMessage null in new versions (> 1.8.7) #698

Closed
opened 2025-12-29 08:32:40 +01:00 by adam · 7 comments
Owner

Originally created by @jheier on GitHub (Jun 13, 2025).

Originally assigned to: @StefH on GitHub.

We are using IRequestMessage.BodyAsMimeMessage to do some extra processing when Mocking a request containing a Mime-message. All things are working as expected when using WireMock.Net 1.8.7. In the versions > 1.8.7 our code is broken; the value is null.

Looking a the WireMock-code we think that TypeLoader cannot find the needed DLL when using TypeLoader.TryFindTypeInDlls. The implementation uses Directory.GetCurrentDirectory() but that could be another directory than the folder containing the DLL's.

Question: Is this an extra restriction in the new versions? It should be added to the documentation.

Originally created by @jheier on GitHub (Jun 13, 2025). Originally assigned to: @StefH on GitHub. We are using IRequestMessage.BodyAsMimeMessage to do some extra processing when Mocking a request containing a Mime-message. All things are working as expected when using WireMock.Net 1.8.7. In the versions > 1.8.7 our code is broken; the value is null. Looking a the WireMock-code we think that TypeLoader cannot find the needed DLL when using TypeLoader.TryFindTypeInDlls. The implementation uses **Directory.GetCurrentDirectory()** but that could be another directory than the folder containing the DLL's. _Question_: Is this an extra restriction in the new versions? It should be added to the documentation.
adam added the bug label 2025-12-29 08:32:40 +01:00
adam closed this issue 2025-12-29 08:32:40 +01:00
Author
Owner

@StefH commented on GitHub (Jun 13, 2025):

Can you post your complete code please?

Because this code works when using 1.8.11:

// Copyright © WireMock.Net

using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

using var server = WireMockServer.Start();

var textPlainContent = "This is some plain text";
var textPlainContentType = "text/plain";
var textPlainContentTypeMatcher = new ContentTypeMatcher(textPlainContentType);
var textPlainContentMatcher = new ExactMatcher(textPlainContent);
var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher);

var textJson = "{ \"Key\" : \"Value\" }";
var textJsonContentType = "text/json";
var textJsonContentTypeMatcher = new ContentTypeMatcher(textJsonContentType);
var textJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true);
var jsonMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textJsonContentTypeMatcher, null, null, textJsonContentMatcher);

var imagePngBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC");
var imagePngContentMatcher = new ExactObjectMatcher(imagePngBytes);
var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, null, null, null, imagePngContentMatcher);

var matchers = new IMatcher[]
{
    textPlainMatcher,
    jsonMatcher,
    imagePngMatcher
};

server
    .Given(Request.Create()
        .UsingPost()
        .WithPath("/multipart")
        .WithMultiPart(matchers)
    )
    .RespondWith(Response.Create()
        .WithBodyAsJson(new
            {
                Method = "{{request.Method}}",
                BodyAsMimeMessage = "{{request.BodyAsMimeMessage.TextBody}}"
        }
        )
        .WithTransformer()
    );

var formDataContent = new MultipartFormDataContent
{
    { new StringContent(textPlainContent, Encoding.UTF8, textPlainContentType), "text" },
    { new StringContent(textJson, Encoding.UTF8, textJsonContentType), "json" }
};

var fileContent = new ByteArrayContent(imagePngBytes);
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
formDataContent.Add(fileContent, "somefile", "image.png");

var client = server.CreateClient();

var response = await client.PostAsync("/multipart", formDataContent);
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);

Output =

{"Method":"POST","BodyAsMimeMessage":"This is some plain text"}
@StefH commented on GitHub (Jun 13, 2025): Can you post your complete code please? Because this code works when using 1.8.11: ``` c# // Copyright © WireMock.Net using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json; using WireMock.Matchers; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; using var server = WireMockServer.Start(); var textPlainContent = "This is some plain text"; var textPlainContentType = "text/plain"; var textPlainContentTypeMatcher = new ContentTypeMatcher(textPlainContentType); var textPlainContentMatcher = new ExactMatcher(textPlainContent); var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher); var textJson = "{ \"Key\" : \"Value\" }"; var textJsonContentType = "text/json"; var textJsonContentTypeMatcher = new ContentTypeMatcher(textJsonContentType); var textJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true); var jsonMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textJsonContentTypeMatcher, null, null, textJsonContentMatcher); var imagePngBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC"); var imagePngContentMatcher = new ExactObjectMatcher(imagePngBytes); var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, null, null, null, imagePngContentMatcher); var matchers = new IMatcher[] { textPlainMatcher, jsonMatcher, imagePngMatcher }; server .Given(Request.Create() .UsingPost() .WithPath("/multipart") .WithMultiPart(matchers) ) .RespondWith(Response.Create() .WithBodyAsJson(new { Method = "{{request.Method}}", BodyAsMimeMessage = "{{request.BodyAsMimeMessage.TextBody}}" } ) .WithTransformer() ); var formDataContent = new MultipartFormDataContent { { new StringContent(textPlainContent, Encoding.UTF8, textPlainContentType), "text" }, { new StringContent(textJson, Encoding.UTF8, textJsonContentType), "json" } }; var fileContent = new ByteArrayContent(imagePngBytes); fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png"); formDataContent.Add(fileContent, "somefile", "image.png"); var client = server.CreateClient(); var response = await client.PostAsync("/multipart", formDataContent); var content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content); ``` Output = ``` {"Method":"POST","BodyAsMimeMessage":"This is some plain text"} ```
Author
Owner

@jheier commented on GitHub (Jun 14, 2025):

// Relevant code fragment
public void Configure(WireMockServer server)
    {
        // Simulate an other current directory for some reason, somewhere in the program but before the first
        // request is handled by WireMock.
        Directory.SetCurrentDirectory(@"C:\Windows");

        server.Given(Request.Create().UsingPost())
              .RespondWith(Response.Create()
                    .WithCallback(request =>
                    {
                        // Fails using WireMock.Net 1.8.11
                        // Fails using WireMock.Net 1.8.8
                        // Succeeds using WireMock.Net 1.8.7
                        if (request.BodyAsMimeMessage == null) throw new InvalidProgramException("Not expected");
                        return new ResponseMessage { StatusCode = 200 };
                    }));
    }

Also, is after WireMock.Net 1.8.8 is the MimeKitLite Wiki still relevant?

@jheier commented on GitHub (Jun 14, 2025): ``` c# // Relevant code fragment public void Configure(WireMockServer server) { // Simulate an other current directory for some reason, somewhere in the program but before the first // request is handled by WireMock. Directory.SetCurrentDirectory(@"C:\Windows"); server.Given(Request.Create().UsingPost()) .RespondWith(Response.Create() .WithCallback(request => { // Fails using WireMock.Net 1.8.11 // Fails using WireMock.Net 1.8.8 // Succeeds using WireMock.Net 1.8.7 if (request.BodyAsMimeMessage == null) throw new InvalidProgramException("Not expected"); return new ResponseMessage { StatusCode = 200 }; })); } ``` Also, is after WireMock.Net 1.8.8 is the [MimeKitLite](https://github.com/wiremock/WireMock.Net/wiki/MimeKit-and-MimeKitLite) Wiki still relevant?
Author
Owner

@StefH commented on GitHub (Jun 14, 2025):

https://github.com/wiremock/WireMock.Net/pull/1320

@StefH commented on GitHub (Jun 14, 2025): https://github.com/wiremock/WireMock.Net/pull/1320
Author
Owner

@StefH commented on GitHub (Jun 14, 2025):

@jheier
There are indeed some changes related to MimeKitLite, and I have to check the wiki.

However in PR https://github.com/wiremock/WireMock.Net/pull/1320

I did create a new console-app and unit test which uses the latest NuGet, and both run fine.
Could it be that you mimemessage is not correctly detected anymore as a mimemessage?
What header do you use?

And which .NET version do you use?

Can you maybe post your complete test ?

@StefH commented on GitHub (Jun 14, 2025): @jheier There are indeed some changes related to MimeKitLite, and I have to check the wiki. However in PR https://github.com/wiremock/WireMock.Net/pull/1320 I did create a new console-app and unit test which uses the latest NuGet, and both run fine. Could it be that you mimemessage is not correctly detected anymore as a mimemessage? What header do you use? And which .NET version do you use? Can you maybe post your complete test ?
Author
Owner

@StefH commented on GitHub (Jun 14, 2025):

@jheier
Sorry, now I see the Directory.SetCurrentDirectory(@"C:\Windows"); indeed breaks the code, I'll fix it.

@StefH commented on GitHub (Jun 14, 2025): @jheier Sorry, now I see the `Directory.SetCurrentDirectory(@"C:\Windows");` indeed breaks the code, I'll fix it.
Author
Owner

@StefH commented on GitHub (Jun 14, 2025):

@jheier
can you test 1.8.11-fix-05-ci-110201 ?

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

@StefH commented on GitHub (Jun 14, 2025): @jheier can you test 1.8.11-fix-05-ci-110201 ? (https://github.com/wiremock/WireMock.Net/wiki/MyGet-preview-versions)
Author
Owner

@jheier commented on GitHub (Jun 15, 2025):

Indeed this fixes the problem.

@jheier commented on GitHub (Jun 15, 2025): Indeed this fixes the problem.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#698