Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以在blazor中使用自动测试客户端吗_C#_Asp.net Core_Mono_Autorest_Blazor - Fatal编程技术网

C# 我可以在blazor中使用自动测试客户端吗

C# 我可以在blazor中使用自动测试客户端吗,c#,asp.net-core,mono,autorest,blazor,C#,Asp.net Core,Mono,Autorest,Blazor,当我尝试在webassembly上运行blazor应用程序时,我从mono平台获得了一个System.PlatformNotSupportedException。 我使用Autorest自动生成了一个web api客户端。一切都很好,但是当我在浏览器中加载代码时,我在浏览器控制台中得到了下面的错误 使用VS2017的预览版本 module.printErr @ MonoPlatform.ts:192 WASM: [System.PlatformNotSupportedException] Ope

当我尝试在webassembly上运行blazor应用程序时,我从mono平台获得了一个
System.PlatformNotSupportedException
。 我使用Autorest自动生成了一个web api客户端。一切都很好,但是当我在浏览器中加载代码时,我在浏览器控制台中得到了下面的错误

使用VS2017的预览版本

module.printErr @ MonoPlatform.ts:192
WASM: [System.PlatformNotSupportedException] Operation is not supported on this platform.
WASM:   at System.Net.WebProxy.CreateDefaultProxy () <0x204ed08 + 0x00004> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Configuration.DefaultProxySectionInternal.GetSystemWebProxy () <0x204ebc0 + 0x00000> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Configuration.DefaultProxySectionInternal.GetDefaultProxy_UsingOldMonoCode () <0x204ea80 + 0x00000> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Configuration.DefaultProxySectionInternal.GetSection () <0x204e8c8 + 0x00022> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.WebRequest.get_InternalDefaultWebProxy () <0x204e610 + 0x0002c> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.HttpWebRequest..ctor (System.Uri uri) <0x2043eb0 + 0x000d2> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Http.HttpClientHandler.CreateWebRequest (System.Net.Http.HttpRequestMessage request) <0x20434d0 + 0x00016> in <3a9393eaef104ec489024eb855a8f163>:0 
WASM:   at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () <0x203ea60 + 0x00076> in <3a9393eaef104ec489024eb855a8f163>:0 
WASM: --- End of stack trace from previous location where exception was thrown ---
...
module.printErr@monopplatform.ts:192
WASM:[System.PlatformNotSupportedException]操作在此平台上不受支持。
WASM:位于:0中的System.Net.WebProxy.CreateDefaultProxy()处
WASM:位于:0中的System.Net.Configuration.DefaultProxySectionInternal.GetSystemWebProxy()处
WASM:位于System.Net.Configuration.DefaultProxySectionInternal.GetDefaultProxy_,使用LDMonocode()位于:0
WASM:位于:0中的System.Net.Configuration.DefaultProxySectionInternal.GetSection()处
WASM:位于System.Net.WebRequest.get_InternalDefaultWebProxy()中:0
WASM:at System.Net.HttpWebRequest..ctor(System.Uri)位于:0
WASM:位于:0中的System.Net.Http.HttpClientHandler.CreateWebRequest(System.Net.Http.HttpRequestMessage请求)
WASM:at System.Net.Http.HttpClientHandler+d_u64.MoveNext()位于:0
WASM:---引发异常的前一个位置的堆栈结束跟踪---
...

Blazor/webassembly应用程序在浏览器沙箱中运行,因此仅限于浏览器网络的可能性。这意味着所有网络流量都需要通过浏览器网络请求系统


看起来它试图使用代理。。这是不受支持的。

是的,这是可能的。但您必须使用blazor框架注入的HttpClient,如下所述:

(谢谢你的链接!)

在自动测试使用的Microsoft.Rest.ServiceClient中,HttpClient被标记为受保护。因此,要从blazor注入HttpClient,可以创建自动生成的client类的新部分,并添加
SetHttpClient
方法:

自动测试生成的类:

public partial class YourApi : ServiceClient<YourApi>, IYourApi
{
    ...
}

好简单

现在使用Refit更容易了

const string baseUri = "http://localhost:55088/";
services.AddRefitClient<IApiService>()
   .ConfigureHttpClient(c => c.BaseAddress = new Uri(baseUri))
   .ConfigurePrimaryHttpMessageHandler(() => new BrowserMessageHandler());
常量字符串baseUri=”http://localhost:55088/";
services.addClient()
.ConfigureHttpClient(c=>c.BaseAddress=新Uri(baseUri))
.ConfigurePrimaryHttpMessageHandler(()=>new BrowserMessageHandler());
当然,您仍然需要自己进行序列化/反序列化,但我使用这些扩展,以便在必要时也可以访问HttpResponseMessage

    public static async Task<T> As<T>(this Task<HttpResponseMessage> task)
    {
        var response = await task.ConfigureAwait(false);
        return await response.As<T>();
    }

    public static async Task<T> As<T>(this HttpResponseMessage message)
    {
        var json = await message.Content.ReadAsStringAsync();
        return Json.Deserialize<T>(json);
    }
公共静态异步任务作为(此任务)
{
var response=await task.ConfigureAwait(false);
return wait response.As();
}
公共静态异步任务作为(此HttpResponseMessage消息)
{
var json=wait message.Content.ReadAsStringAsync();
返回Json.Deserialize(Json);
}

我希望这能让别人的生活比我工作前轻松一点

所以你认为这是安全问题?这听起来更像是平台的限制。stacktrace的这一部分有点滑稽:
GetDefaultProxy\u使用LDMonocode
这是webassembly的限制。我完全可以理解。如果webassembly能够超过js,这将造成重大的潜在安全漏洞。另外,请参阅此处如何在blazor中执行网络请求:
    public static async Task<T> As<T>(this Task<HttpResponseMessage> task)
    {
        var response = await task.ConfigureAwait(false);
        return await response.As<T>();
    }

    public static async Task<T> As<T>(this HttpResponseMessage message)
    {
        var json = await message.Content.ReadAsStringAsync();
        return Json.Deserialize<T>(json);
    }