小编Stu*_*ing的帖子

模拟使用 IHttpClientFactory.CreateClient 创建的 HttpClient

正如标题所暗示的,我有一些代码可以调用IHttpClientFactory.CreateClient()来创建一个 HttpClient 实例。

我在 .Net Core 3.1 中这样做

我需要嘲笑这个。根据这个问题“C# Mock IHttpclient & CreateClient”,以下应该有效......

[Test]
public void Mytest() {

    var httpClientFactory = new Mock<IHttpClientFactory>(MockBehavior.Strict);

    httpMessageHandler = new Mock<HttpMessageHandler>(MockBehavior.Strict);
    httpMessageHandler.Protected()
        // Setup the PROTECTED method to mock
        .Setup<Task<HttpResponseMessage>>(
            "SendAsync",
            ItExpr.IsAny<HttpRequestMessage>(),
            ItExpr.IsAny<CancellationToken>()
        )
        // prepare the expected response of the mocked http call
        .ReturnsAsync(new HttpResponseMessage()
        {
            StatusCode = HttpStatusCode.BadRequest,
        })
        .Verifiable();

    var httpClient = new HttpClient(httpMessageHandler.Object);

    httpClientFactory.Setup(_ => _.CreateClient())   // This fails
        .Returns(httpClient).Verifiable();

    systemUnderTest = new MyService(httpClientFactory.Object);
    var result …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing httpclientfactory

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

访问所选Kendo TreeView节点的子节点

我有一个Ajax填充树视图....

@(Html.Kendo().TreeView()
    .Name("fao")
    .HtmlAttributes(new {@class="fixed-height" })
    .DataTextField("Text")
    .TemplateId("treeview-item-template")
    .DataSource(ds => ds
        .Read(r => r
            .Action("_ModuleData", "Home")
        )
        .Model(m => m
            .Children("Items")
            .HasChildren("HasChildren")
        )
    )
)
Run Code Online (Sandbox Code Playgroud)

我需要更新一些隐藏在每个子项目中的数据 - 在模板中 - 当一个动作(在控件之外触发)发生时.

完整性模板看起来像这样......

<script id="treeview-item-template" type="text/kendo-ui-template">
    #= item.Text #<input type='hidden' class='hidden-data' data-fal='#= item.Fal#' data-uid='#=item.uid#'/>
</script>
Run Code Online (Sandbox Code Playgroud)

现在,我有触发器的代码,并且工作得很好.

我有代码来更新隐藏数据.再次.别担心.

我无法弄清楚的是如何简单地获取触发器触发时所选节点的所有子节点(和granchild等)节点.

如果我在最初点击节点时试图找到孩子们,我希望能够说出类似...

function doSomething(e)
{
    for(n=0; n<e.node.nodes.length; n++)
    {
        doSomethingElse(e.node.nodes[n]);
    }
}
Run Code Online (Sandbox Code Playgroud)

但似乎不存在这样的功能.

有没有人有任何建议我怎么可能这样做?

javascript telerik-mvc kendo-ui kendo-treeview

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

Blazor 应用程序中的结构以及为什么我不能拥有一个?

因此,我使用dotnet new blazorserver. 在VS中打开解决方案并运行它。

美好的。

如果我添加一个新文件夹,Components并在该文件夹中添加一个新的 Razor 组件 -Component1.razor那么在我的index.razor页面中,我添加一条 using 语句来指向我的Components文件夹,并添加标记以包含组件本身并运行应用程序、索引页面显示,但没有我的组件的迹象。进一步查看渲染的 HTML 的源代码,我看到一个空元素<component1></component1>

如果我将新组件移至该Pages文件夹并重新运行应用程序,该组件将正确呈现。

如果我在下面创建一个子文件夹Pages并将组件移到那里并重新运行应用程序,则该组件将无法渲染。

难道我做错了什么?我是不是期待太多了?我是否应该能够拥有一个结构,这意味着我不必将每个组件都放在一个文件夹中?

blazor visual-studio-2019 blazor-server-side

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

序列化'这个'

好的,如果我有这样的课......

[serializable]
public class MyClass() : ISerializable
{
  public Dictionary<string, object> Values {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我知道我要做什么来序列化它(对于那些试图找到快速答案的人来说,答案是这样的)......

protected MyClass(SerializationInfo info, StreamingContext context)
{
  Values = (Dictionary<string, object>)info.GetValue("values", typeof(Dictionary<string, object>));
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
  info.AddValue("values", Values);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果我想要定义一个继承自Dictionary的类,我该怎么办?

我到目前为止......

[serializable]
public class MyClass() : Dictionary<string, object>, ISerializable
{
  public void GetObjectData(SerializationInfo info, StreamingContext context)
  {
    info.AddValue("me", this);
  }
}
Run Code Online (Sandbox Code Playgroud)

但后来我迷路了.我写不出来......

protected MyClass(SerializationInfo info, StreamingContext context)
{
  this = (MyClass)info.GetValue("me", typeof(MyClass));
}
Run Code Online (Sandbox Code Playgroud)

'cos'这个'是r/o.那么,我该怎么办?我对GetObjectData()的实现是否正确?

我不相信它会有所作为,但万一它确实如此,我在.Net 4.0下写这个

.net c# serialization

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

使用IdentityServer3获取声明

我正在关注使用OAuth来保护Pluralsight上的ASP.NET API课程.我已经使用许多InMemoryUsers设置了IdentityServer,其中一个看起来像这样......

public static List<InMemoryUser> Get()
{
    return new List<InMemoryUser>
       {
           new InMemoryUser
           {
               Username = "user@domain.com",
               Password = "password",
               Subject = "user@domain.com",
               Claims = new[]
                        {
                            new Claim(Constants.ClaimTypes.Id, "96cddc1de66641829237b7f09869b1c8"),
                            new Claim(Constants.ClaimTypes.Name, "Some Full name example
                        }
           },
       };
}
Run Code Online (Sandbox Code Playgroud)

如果我授权用户并使用提供的访问令牌来调用API,则该用户的声明集合看起来像这样......

((User as System.Security.Claims.ClaimsPrincipal).Identities.First() as System.Security.Claims.ClaimsIdentity).Claims.ToList()
Count = 10
    [0]: {iss: https://localhost:44375}
    [1]: {aud: https://localhost:44375/resources}
    [2]: {exp: 1468920204}
    [3]: {nbf: 1468916604}
    [4]: {client_id: my_clientid}
    [5]: {scope: openid}
    [6]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: user@domain.com}
    [7]: {http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationinstant: 1468916604}
    [8]: {http://schemas.microsoft.com/identity/claims/identityprovider: …
Run Code Online (Sandbox Code Playgroud)

oauth-2.0 asp.net-web-api2 identityserver3

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

调用许多函数中的任何一个,直到得到指定的结果

我有这个丑陋的代码看起来有点像这样......

TestResult GetFirstTestResult()
{
    var result = TestMethod1();
    if(result is EmptyResult)
    {
        result = TestMethod2();
    }

    if(result is EmptyResult)
    {
        result = TestMethod3();
    }

    // ...

    if(result is EmptyResult)
    {
        result = TestMethodN();
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

基本上,我需要运行一些测试,直到找到一个有一些值的测试.

现在虽然上面的代码并不漂亮,但是一个小的(ish)值N是可管理的.可悲的是,就我而言,N可能会变得相当大.

有没有办法用循环写这个,这个伪代码的行...

TestResult GetFirstTestResult()
{
    TestResult[] results = {TestMethod1(), TestMethod2(), TestMethod3(), .. TestMethodN()};
    return results.First(test=>!test.Result is Emptyresult);
}
Run Code Online (Sandbox Code Playgroud)

这样每个测试方法都在循环中调用,所以实际上只执行了最小数量的测试方法?

编辑 感谢RicSimon,我有这个......

TestResult GetFirstTestResult()
{
    return new Func<TestResult>[]
        {
            TestMethod1,
            TestMethod2, …
Run Code Online (Sandbox Code Playgroud)

c# algorithm design-patterns

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

Azure.Messaging.ServiceBus.ServiceBusClient 的连接字符串

我正在尝试开始使用 Azure ServiceBus,但文档之神反对我。我正在关注这篇 MS 文章,它说这段代码将允许我发送消息......

class Program
{
    static string connectionString = "<NAMESPACE CONNECTION STRING>";
    static string queueName = "<QUEUE NAME>";

    static async Task Main(string[] args)
    {
        await SendMessageAsync();
    }

    static async Task SendMessageAsync()
    {
        // create a Service Bus client 
        ServiceBusClient client = new ServiceBusClient(connectionString);

        // create a sender for the queue 
        ServiceBusSender sender = client.CreateSender(queueName);

        // create a message that we can send
        ServiceBusMessage message = new ServiceBusMessage("Hello world!");

        // send the message
        await sender.SendMessageAsync(message);
        Console.WriteLine($"Sent …
Run Code Online (Sandbox Code Playgroud)

connection-string azure azureservicebus azure-servicebus-queues

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