简体   繁体   English

加载 Blazor 客户端应用程序时如何避免浏览器中的 System.TypeLoadException 未处理异常

[英]How to avoid System.TypeLoadException unhandled exception in browser when loading Blazor client-side application

I have a client-side Blazor application, and when I try to run it for debugging from Visual Studio 2019 16.3.0 Preview 2.0, I get this error message in the Chrome browser (seen when debugging using Shift-Alt-D):我有一个客户端 Blazor 应用程序,当我尝试从 Visual Studio 2019 16.3.0 Preview 2.0 运行它进行调试时,我在 Chrome 浏览器中收到此错误消息(在使用 Shift-Alt-D 调试时看到):

blazor.webassembly.js:1 WASM: System.TypeLoadException: Could not resolve type with token 01000020 from typeref (expected class 'Microsoft.AspNetCore.Blazor.HttpClientJsonExtensions' in assembly 'Microsoft.AspNetCore.Blazor, Version=0.7.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60') blazor.webassembly.js:1 WASM: System.TypeLoadException: 无法从 typeref 解析带有令牌 01000020 的类型(程序集“Microsoft.AspNetCore.Blazor.HttpClientJsonExtensions”中的预期类“Microsoft.AspNetCore.Blazor,版本=0.7.0.0,文化” = 中性,PublicKeyToken=adb9793829ddae60')

I have set a breakpoint as early I could in Main in Program.cs, but this does not get hit.我尽可能早地在 Program.cs 的 Main 中设置了一个断点,但这并没有被击中。

I am using Blazor version 3.0.0-preview8.19405.7.我正在使用 Blazor 版本 3.0.0-preview8.19405.7。 I have been searching for a solution to this problem, but all articles seem to address other issues than mine.我一直在寻找解决此问题的方法,但所有文章似乎都解决了我的问题之外的其他问题。

The issue seems to be about Json and HttpClient, so I thought it might have something to do with the upgrade since it was mentioned in the procedure: https://devblogs.microsoft.com/aspnet/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-8/这个问题似乎是关于 Json 和 HttpClient,所以我认为它可能与升级有关,因为它在程序中提到: https : //devblogs.microsoft.com/aspnet/asp-net-core-and- blazor-updates-in-net-core-3-0-preview-8/

But I actually did not use Json succesfully before upgrading (it was under development), so I don't know for sure if it has something to do with this.但是我在升级之前实际上并没有成功使用Json(它正在开发中),所以我不确定它是否与此有关。

Edit: I have tried to remove the two lines from the code (but kept the reference to Microsoft.AspNetCore.Blazor.HttpClient in the project file):编辑:我试图从代码中删除这两行(但在项目文件中保留了对 Microsoft.AspNetCore.Blazor.HttpClient 的引用):

result = await httpClient.GetJsonAsync<bool>("uri");

and

await httpClient.PutJsonAsync("uri", value);

This solved the problem, but will of cause leave me without any calls to my backend.这解决了问题,但会导致我没有任何后端调用。

So how can I use httpClient?那么如何使用 httpClient 呢? Or which alternatives do I have?或者我有哪些选择?

Edit:编辑:

I am creating httpClient using dependency injection in my constructor我在构造函数中使用依赖注入创建 httpClient

public class ServiceProxy : IServiceProxy
{
    private readonly HttpClient httpClient;
    public ServiceProxy(IHttpClientFactory httpClientFactory)
    {
        httpClient = httpClientFactory.CreateClient();
        httpClient.BaseAddress = new Uri("https://localhost:12345"), UriKind.Absolute);
    }

    public async Task<bool> GetResultAsync()
    {
        bool result
        try
        {
            result = await httpClient.GetJsonAsync<bool>("resource");
        }
        catch (HttpRequestException httpRequestException)
        {
            throw new ConnectionException(string.Format("Http Request failed: {0}", httpRequestException.Message), httpRequestException);
        }
        return result;
    }
}

The dependencies are injected from my startup class依赖项是从我的启动类注入的

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpClient();
        services.AddTransient<IServiceProxy, ServiceProxy>();
    }
}

I have discovered the root cause of this problem and thereby found a solution for it.我已经发现了这个问题的根本原因,从而找到了解决方案。

I upgraded my Visual Studio 2019 to 16.4.0 Preview 1.0, and then something broke, which did not break before.我将我的 Visual Studio 2019 升级到 16.4.0 Preview 1.0,然后有些东西坏了,以前没有坏过。 And when consolidating my NuGet dependencies I was very surprised that one of my .NET Standard dependency projects actually had a reference to Blazor 0.7.0, which was older than the Preview 8 version I was upgrading from.在整合我的 NuGet 依赖项时,我非常惊讶我的一个 .NET Standard 依赖项项目实际上引用了 Blazor 0.7.0,它比我升级的 Preview 8 版本还要旧。

This reference was probably the cause of my pain!这个参考可能是我痛苦的原因! When that reference was removed, I could not call my httpClient.PutJsonAsync and httpClient.GetJsonAsync any more, because this is probably only implemented in the Blazor version of the HttpClient.删除该引用后,我无法再调用我的 httpClient.PutJsonAsync 和 httpClient.GetJsonAsync,因为这可能仅在 HttpClient 的 Blazor 版本中实现。

I rewrote my library to use httpClient.PutAsync and httpClient.GetAsync and using Newtonsoft.Json to serialize and deserialize.我重写了我的库以使用 httpClient.PutAsync 和 httpClient.GetAsync 并使用 Newtonsoft.Json 进行序列化和反序列化。 After fixing some CORS issues in addition, it worked.另外修复了一些 CORS 问题后,它起作用了。

When I run into issues like this, I delete the bin and obj folders while my project is closed.当我遇到这样的问题时,我会在项目关闭时删除 bin 和 obj 文件夹。 I also delete all nuget packages and install them again.我还删除了所有 nuget 包并重新安装它们。

I hope this helps.我希望这有帮助。

Client side blazor does not support IHttpClientFactory .客户端 blazor 不支持IHttpClientFactory Client side HttpClient is not the same as in server side or other version.客户端HttpClient与服务器端或其他版本不同。 It is a wrapper of the XMLHttpRequest in the browser.它是浏览器中XMLHttpRequest的包装器。 (Additionally layered over typescript) (另外分层在打字稿上)

[Blazor client-side apps call web APIs using a preconfigured HttpClient service. [Blazor 客户端应用程序使用预配置的 HttpClient 服务调用 Web API。 Compose requests, which can include JavaScript Fetch API options, using Blazor JSON helpers or with HttpRequestMessage.使用 Blazor JSON 帮助程序或使用 HttpRequestMessage 撰写请求,其中可以包含 JavaScript Fetch API 选项。

Blazor server-side apps call web APIs using HttpClient instances typically created using IHttpClientFactory. Blazor 服务器端应用程序使用通常使用 IHttpClientFactory 创建的 HttpClient 实例调用 Web API。 For more information, see Make HTTP requests using IHttpClientFactory in ASP.NET Core.] 1有关详细信息,请参阅在 ASP.NET Core 中使用 IHttpClientFactory 发出 HTTP 请求。] 1

On client side the HttpClient is out of the box included in the DI.在客户端, HttpClient是包含在 DI 中的开箱即用的。 You can use that to make http calls.您可以使用它来进行 http 调用。

Inject it to code into constructor:将其注入到构造函数中:

private readonly HttpClient _httpClient;

public YourClassName(HttpClient httpClient)
{
    _httpClient = httpClient;
}

//Use the _httpClient in other methods as usual

Injecting into a property注入属性

[Inject]
public HttpClient MyHttpClient { get; set; }

Injecting into a blazor page注入 blazor 页面

@inject HttpClient MyHttpClient

code {
   //Use the MyHttpClientin other methods as usual
}

Additional resources for DI in blazor blazor 中 DI 的其他资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM