小编mik*_*eyy的帖子

如何在 Blazor 服务器端重定向到不同的路由

在 Blazor 客户端中,可以使用以下方法实现重定向

using Microsoft.AspNetCore.Blazor.Browser.Services;
(...)
BrowserUriHelper.Instance.NavigateTo("/route")
Run Code Online (Sandbox Code Playgroud)

但是,这在 Blazor 服务器项目中不起作用,因为它会生成以下错误:

无法将“Microsoft.AspNetCore.Blazor.Server.Circuits.RemoteJSRuntime”类型的对象转换为“Microsoft.JSInterop.IJSInProcessRuntime”。

Blazor-Server 中的重定向如何?

c# blazor-server-side

14
推荐指数
2
解决办法
2万
查看次数

DefaultIfEmpty().Max() 仍然抛出“序列不包含元素”。

在我将我的项目更新到 dotnet core 3.0RC1(也可能在 preview9 中)之后,我的代码曾经可以工作

var value = context.Products.Where(t => t.CategoryId == catId).Select(t => t.Version).DefaultIfEmpty().Max();
Run Code Online (Sandbox Code Playgroud)

开始投掷 System.InvalidOperationException: Sequence contains no elements。有问题的表是空的。

如果我添加ToList()它看起来像这样DeafultIfEmpty().ToList().Max(),它会再次开始工作。找不到有关重大更改的任何信息。当我跑

var expectedZero = new List<int>().DefaultIfEmpty().Max();
Run Code Online (Sandbox Code Playgroud)

它工作正常。这让我觉得 EF Core 可能有问题。然后我在 xUnit 中使用完全相同的设置创建了测试,但测试正在通过(表也是空的,使用 InMemoryDatabase 而不是实时 SQL Server 实例)。

我真的很困惑。相关堆栈跟踪:

System.InvalidOperationException: Sequence contains no elements.
   at int lambda_method(Closure, QueryContext, DbDataReader, ResultContext, int[], ResultCoordinator)
   at bool Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+QueryingEnumerable<T>+Enumerator.MoveNext()
   at TSource System.Linq.Enumerable.Single<TSource>(IEnumerable<TSource> source)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
   at TSource System.Linq.Queryable.Max<TSource>(IQueryable<TSource> source)
   at ... (my …
Run Code Online (Sandbox Code Playgroud)

c# .net-core ef-core-3.0

9
推荐指数
1
解决办法
1655
查看次数

如何在 RenderFragment 中定义模板化组件及其可用的 @context

我有一个模板化组件(工具提示),它有一个参数,并将上下文中的该参数传递给子内容。该参数是 的包装器ElementReference。这样做的目的是在设置后返回到子项的参考工具提示。我想要做的是将工具提示组件的特定实例存储在RenderFragment多个位置的可重复使用中。但我得到了错误The name 'context' does not exists in the current context


这是最初的问题,但事实证明它过于简单化了。请转到第二条分隔线,样本更类似于我的情况。

这是一个示例(不是那个工具提示,而是具有完全相同问题的简化代码)。
模板化组件RenderFragTempl

@inherits ComponentBase

<span id="@(Ref)">    
    @IntChildContent(Ref)
</span>

@code {
    [Parameter] public RenderFragment<int> IntChildContent { get; set; }

    [Parameter] public int Ref { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)

并调用Index.razor

@page "/"
@using BlazorServer.Pages.StackOverflow

<RenderFragTempl Ref="10">
    <IntChildContent>
        <p>@context</p>
    </IntChildContent>
</RenderFragTempl>
<br />
@test

@code {
    //compiler error CS0103: The name 'context' does not exist in the current context
    private readonly …
Run Code Online (Sandbox Code Playgroud)

razor blazor blazor-server-side blazor-client-side blazor-webassembly

5
推荐指数
1
解决办法
2881
查看次数

如何模拟由 Visual Studio 自动生成的 SOAP 客户端?

我有一个自动生成的 SOAP 客户端。它是由 Visual Studio 向导生成的(添加连接的服务 -> Microsoft WCF Web 服务参考提供程序)。我想模拟该客户端,因此当调用方法时,将以 SOAP 响应的格式返回预定义的结果。不幸的是,我无法让它工作 - 我的结果要么为空(而不是在 .Returns 中定义),要么得到异常。

我正在尝试在我的设计中应用干净的架构。因此,这个 SOAP 客户端进入了我的基础设施层,我在其中为其创建了一个存储库。该存储库创建 DTO,因此可以将它们分派到我的持久性中。存储库通过依赖项注入接收 SOAP 客户端。我还想对存储库进行测试,只是为了验证 DTO 生成是否正确。因此,我想模拟这个 SOAP 服务,这样我就可以将其提供给存储库并测试返回的 DTO。

自动生成的界面:

    public interface ApplicationSoap
    {
        [System.ServiceModel.OperationContractAttribute(Action = "http://Application/GetAppVersion", ReplyAction = "*")]
        Task<ExtApp.GetAppVersionResponse> GetAppVersionAsync(ExtApp.GetAppVersionRequest request);
    }

Run Code Online (Sandbox Code Playgroud)

和自动生成的客户端类:

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.1-preview-30514-0828")]
    public partial class ApplicationSoapClient : System.ServiceModel.ClientBase<ExtApp.ApplicationSoap>, ExtApp.ApplicationSoap
    {

        static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.Threading.Tasks.Task<ExtApp.GetAppVersionResponse> ExtApp.ApplicationSoap.GetAppVersionAsync(ExtApp.GetAppVersionRequest request)
        {
            return base.Channel.GetAppVersionAsync(request);
        }

        public System.Threading.Tasks.Task<ExtApp.GetAppVersionResponse> GetAppVersionAsync(int appVer)
        {
            ExtApp.GetAppVersionRequest inValue = new ExtApp.GetAppVersionRequest(); …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq soap-client .net-core

4
推荐指数
1
解决办法
4394
查看次数

将 HttpClient 与 .AddClientAccessTokenHandler() 扩展一起使用时,不包含角色声明

我正在使用IdentityModel.AspNetCore扩展.AddClientAccessTokenHandler()来自动向 API 提供HttpClient访问令牌(至少我知道我可以使用它)。某些 API 端点是根据角色进行授权的。但由于某种原因,添加到请求的访问令牌不包含角色声明。如果我不使用.AddClientAccessTokenHandler()并手动检索令牌并使用它进行设置,SetBearerToken(accessTone)那么我可以到达我的角色授权端点。

我的启动是:

services.AddAccessTokenManagement(options =>
{
    options.Client.Clients.Add("auth", new ClientCredentialsTokenRequest
    {
        Address = "https://localhost:44358/connect/token", 
        ClientId = "clientId",
        ClientSecret = "clientSecret",
    });
});
Run Code Online (Sandbox Code Playgroud)

WebApi 调用:

var response = await _httpClient.GetAsync("api/WeatherForecast/SecretRole");
Run Code Online (Sandbox Code Playgroud)

身份服务器配置:

public static IEnumerable<ApiResource> GetApis() =>
    new List<ApiResource>
    {
        new ApiResource("WebApi", new string[] { "role" })
            { Scopes = { "WebApi.All" }}
    };

public static IEnumerable<ApiScope> GetApiScopes() =>
    new List<ApiScope>
        { new ApiScope("WebApi.All") };

public static IEnumerable<IdentityResource> GetIdentityResources() => …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core identitymodel

1
推荐指数
1
解决办法
8815
查看次数