.Net trying to load 'System.Net.WebProxy' from wrong assembly 'Microsoft.Extensions.Options'

295 Views Asked by At

My application in AspNetCore 3.1, runs locally without any issues with dotnet run. But when running the application in Docker i'm getting the following exception:

System.TypeLoadException: Could not load type 'System.Net.WebProxy' from assembly 'Microsoft.Extensions.Options, Version=3.1.23.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.\n at[...]

This exception makes no sense, since System.Net.WebProxy isn't part of Microsoft.Extensions.Options and has nothing to do with the library. I'm guessing this isn't the root cause, but I can't find anything to point towards any other issue.

I've tried changing the dependency to use a different version of Microsoft.Extensions.Options and have tried different base images in my dockerfile. Reinstalled dotnet SDK etc. I'm at a total loss.

The following Method is where the application throws.

using MyApp.Model.Extensions;
using MyApp.Wrappers;
using Microsoft.AspNetCore.Http;
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
namespace My.App.Factories
{
    public class MyHttpClientFactory : IMyHttpClientFactory
    {

        public MyHttpClientFactory(IHttpContextAccessor httpContextAccessor)
        {
        }

        [...]

        private static HttpClientHandler GetClientHandler(string proxyAddress)
        {
            WebProxy proxy = null;

            if (!string.IsNullOrEmpty(proxyAddress))
            {
                proxy = new WebProxy(proxyAddress);
            }

            var httpClientHandler = new HttpClientHandler
            {
                Proxy = proxy,
                UseProxy = proxy != null,
            };

            return httpClientHandler;
        }
    }
}
FROM openshift.repo.***.se/***/ubi-dotnet-31-***:latest AS builder
USER 0

ARG configuration=Release
ARG verbosity=normal

WORKDIR /usr/app

COPY . .

RUN dotnet publish \
        --runtime linux-x64 \   
        --configuration ${configuration} \
        --output /usr/app/output \
        --verbosity ${verbosity} \
        /p:PublishTrimmed=true \
        /p:PublishSingleFile=false \ 
        ./src/My.App

FROM openshift.repo.***.se/***/ubi-dotnet-31-runtime-***:latest

WORKDIR /usr/app
 
COPY --from=builder /usr/app/output/ ./

USER 1001

EXPOSE 8080

ENV ASPNETCORE_URLS=http://+:8080

ENTRYPOINT ["/usr/app/My.App"]
0

There are 0 best solutions below