WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.39k stars 207 forks source link

TinyMapper error #973

Open rmeshksar opened 1 year ago

rmeshksar commented 1 year ago

I created a console application and enabled Admin Interface. Now when I try to use admin API, I am getting this error:

System.TypeInitializationException: The type initializer for 'WireMock.Util.TinyMapperUtils' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'TinyMapper, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required.

It seams the similar issue that is mentioned here: https://github.com/WireMock-Net/WireMock.Net/wiki/Could-not-load-file-or-assembly-RestEase

I added Brutal.Dev.StrongNameSigner to the console application. First I had hard time compiling the project and I had to close and open Visual Studio, and finally when the project got compiled, still I got the same error.

I am using Wiremock .Net 1.5.32

This is the full log:

Mock server started on port:8443
2023-07-19 8:00:42 PM [Info] : By Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)
2023-07-19 8:00:42 PM [Debug] : Server settings {
  "Port": 8443,
  "UseSSL": null,
  "HostingScheme": null,
  "StartAdminInterface": true,
  "ReadStaticMappings": true,
  "WatchStaticMappings": null,
  "WatchStaticMappingsInSubdirectories": true,
  "ProxyAndRecordSettings": null,
  "Urls": null,
  "StartTimeout": 10000,
  "AllowPartialMapping": null,
  "AdminUsername": null,
  "AdminPassword": null,
  "AdminAzureADTenant": null,
  "AdminAzureADAudience": null,
  "RequestLogExpirationDuration": null,
  "MaxRequestLogCount": null,
  "CorsPolicyOptions": null,
  "AllowCSharpCodeMatcher": null,
  "AllowBodyForAllHttpMethods": null,
  "AllowOnlyDefinedHttpStatusCodeInResponse": null,
  "DisableJsonBodyParsing": null,
  "DisableRequestBodyDecompressing": null,
  "DisableDeserializeFormUrlEncoded": null,
  "HandleRequestsSynchronously": null,
  "ThrowExceptionWhenMatcherFails": null,
  "CertificateSettings": null,
  "CustomCertificateDefined": false,
  "ClientCertificateMode": 0,
  "AcceptAnyClientCertificate": false,
  "WebhookSettings": null,
  "UseRegexExtended": true,
  "SaveUnmatchedRequests": null,
  "DoNotSaveDynamicResponseInLogEntry": null,
  "QueryParameterMultipleValueSupport": null
}
2023-07-19 8:00:42 PM [Info] : Server using .NET Framework 4.6.1 or higher
2023-07-19 8:00:42 PM [Info] : The Static Mapping folder 'C:\MockServer\bin\Debug\__admin\mappings' does not exist, reading Static MappingFiles will be skipped.
Press any key to stop the server
2023-07-19 8:00:50 PM [Error] : Providing a Response for Mapping 'dafb6c48-6a92-401b-8007-b7fb287588b2' failed. HttpStatusCode set to 500. Exception: System.TypeInitializationException: The type initializer for 'WireMock.Util.TinyMapperUtils' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'TinyMapper, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
   at WireMock.Util.TinyMapperUtils..ctor()
   at WireMock.Util.TinyMapperUtils..cctor()
   --- End of inner exception stack trace ---
   at WireMock.Util.TinyMapperUtils.get_Instance()
   at WireMock.Server.WireMockServer.SettingsGet(IRequestMessage requestMessage)
   at WireMock.ResponseProviders.DynamicResponseProvider.ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings)
   at WireMock.Mapping.ProvideResponseAsync(IRequestMessage requestMessage)
   at WireMock.Owin.WireMockMiddleware.<InvokeInternalAsync>d__9.MoveNext()
2023-07-19 8:00:50 PM [DebugRequestResponse] : Admin[True] {
  "Guid": "88b09893-2829-4821-a01d-f6775dadc672",
  "Request": {
    "ClientIP": "::1",
    "DateTime": "2023-07-19T20:00:50.824846Z",
    "Path": "/__admin/settings",
    "AbsolutePath": "/__admin/settings",
    "Url": "http://localhost:8443/__admin/settings",
    "AbsoluteUrl": "http://localhost:8443/__admin/settings",
    "ProxyUrl": null,
    "Query": {},
    "Method": "GET",
    "Headers": {
      "Connection": [
        "keep-alive"
      ],
      "Accept": [
        "*/*"
      ],
      "Accept-Encoding": [
        "gzip, deflate, br"
      ],
      "Host": [
        "localhost:8443"
      ],
      "User-Agent": [
        "PostmanRuntime/7.32.3"
      ],
      "Postman-Token": [
        "d61474f4-03be-4b8d-bce3-2fd64ccf7f97"
      ]
    },
    "Cookies": {},
    "Body": null,
    "BodyAsJson": null,
    "BodyAsBytes": null,
    "BodyEncoding": null,
    "DetectedBodyType": null,
    "DetectedBodyTypeFromContentType": null
  },
  "Response": {
    "StatusCode": 500,
    "Headers": {
      "Content-Type": [
        "application/json"
      ]
    },
    "BodyDestination": null,
    "Body": null,
    "BodyAsJson": {
      "Guid": null,
      "Status": "The type initializer for 'WireMock.Util.TinyMapperUtils' threw an exception."
    },
    "BodyAsBytes": null,
    "BodyAsFile": null,
    "BodyAsFileIsCached": null,
    "BodyOriginal": null,
    "BodyEncoding": null,
    "DetectedBodyType": 2,
    "DetectedBodyTypeFromContentType": null,
    "FaultType": null,
    "FaultPercentage": null
  },
  "MappingGuid": "dafb6c48-6a92-401b-8007-b7fb287588b2",
  "MappingTitle": null,
  "RequestMatchResult": {
    "TotalScore": 2.0,
    "TotalNumber": 2,
    "IsPerfectMatch": true,
    "AverageTotalScore": 1.0,
    "MatchDetails": [
      {
        "Name": "PathMatcher",
        "Score": 1.0
      },
      {
        "Name": "MethodMatcher",
        "Score": 1.0
      }
    ]
  },
  "PartialMappingGuid": "dafb6c48-6a92-401b-8007-b7fb287588b2",
  "PartialMappingTitle": null,
  "PartialRequestMatchResult": {
    "TotalScore": 2.0,
    "TotalNumber": 2,
    "IsPerfectMatch": true,
    "AverageTotalScore": 1.0,
    "MatchDetails": [
      {
        "Name": "PathMatcher",
        "Score": 1.0
      },
      {
        "Name": "MethodMatcher",
        "Score": 1.0
      }
    ]
  }
}
StefH commented 1 year ago

Can you please provide a full working solution / project to show this error?

rmeshksar commented 1 year ago

Here is the project: WireMockIssue.zip

It is a .NET 4.7.1 console application. I just added WireMock.Net from Nuget and wrote this main function:

static void Main(string[] args)
{
    WireMockServer.Start(new WireMockServerSettings
    {
        Port = 8443,
        StartAdminInterface = true
    });

    Console.WriteLine("Press any key to stop the server");
    Console.ReadLine();
}

Now when I am making a GET request:

localhost:8443/__admin/settings

It throws error which I mentioned.

StefH commented 1 year ago

I changed to another mapper.

Can you try preview version 1.5.32-ci-17647 ?

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

rmeshksar commented 1 year ago

1.5.32-ci-17647 works fine.

Is there anything can be done for RestEase as well. I had a lot of issues with it and the provided solution did not work for me.

I noticed if I create a console application with .NET 4.7.1 and ONLY install WireMock.Net.RestClient and Brutal.Dev.StrongNameSigner then I still get this error : 'Could not load file or assembly 'RestEase, Version=1.5.7.0, Culture=neutral, PublicKeyToken=null'

But if I install WireMock.Net as well this solution works !

In order to solve it for our project: First I got RestEase source code and compiled it with signed mode but that did not work. Then I got the source code of WireMock and removed signed form RestClient project and compiled. This solution worked and it is fine for me for now but not ideal.

StefH commented 9 months ago

https://github.com/WireMock-Net/WireMock.Net/pull/1051

StefH commented 9 months ago

@rmeshksar Can you also try this version? 1.5.46-ci-18287

I did replace TinyMapper again with a different Mapper.

(RestEase I'm looking into, but no progress yet)