小编Rai*_*ner的帖子

如何编写一个可以处理Task和ValueTask的方法?

想象一下,您想编写一个类似于以下方法的方法。它包装了一个返回一个ValueTask<T>带有简单性能监控代码的函数:

static async Task Measure<T>(Func<ValueTask<T>> body)
{
    Console.WriteLine($"Starting perf test");
    var sw = Stopwatch.StartNew();
    await body();
    sw.Stop();
    Console.WriteLine(sw.Elapsed);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:有没有办法编写一次这个函数,以便它可以接收Func<ValueTask<T>> Func<Task<T>>

当然,您可以简单地复制代码并仅更改参数的类型。

static async Task Measure<T>(Func<Task<T>> body) { ... }
Run Code Online (Sandbox Code Playgroud)

实现将完全相同。我在问自己在必须处理ValueTask和时是否可以避免这种代码重复Task。直到现在,我也想不出一个好的解决办法。有任何想法吗?

c# c#-7.0

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

自动创建Azure AD B2C租户

是否可以通过编程方式创建Azure AD B2C租户(例如,使用Powershell,REST API)?

我们正在开发一个多租户SaaS解决方案,我们希望在新租户注册时自动创建Azure B2C租户.

azure-active-directory azure-ad-b2c

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

访问ConfigureServices中的IApplicationEnvironment

在我的ConfigureServices方法中,我想读取一个文件(在我的例子中是用于签署令牌的证书,但它可以是设置服务所需的任何文件).因此我需要知道ApplicationBasePath来自IApplicationEnvironment.

目前我通过获取这样的IApplicationEnvironment服务来解决问题:

public void ConfigureServices(IServiceCollection services)
{
    ...
    string basePath;
    var serviceProvider = services.BuildServiceProvider();
    try
    {
        basePath = serviceProvider.GetRequiredService<IApplicationEnvironment>().ApplicationBasePath;
    }
    finally
    {
        (serviceProvider as IDisposable)?.Dispose();
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

这种方法有效,但我不确定这是否是正确的方法.所以我的问题是:

  1. 有没有更好的方法来读取文件ConfigureServices
  2. 是否有更好的方法来获取应用程序基础路径ConfigureServices
  3. 如果我的方法是正确的,我能IDisposable正确处理吗?

c# asp.net-core

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

Azure ARM模板依赖:如何引用VM扩展?

我有一个Azure ARM Teamplate,应该用Docker创建一个Linux VM.Docker是使用Azure安装的DockerExtension.安装Docker之后,我需要使用Azure运行自定义脚本CustomScriptForLinux.因此,第二个脚本应该依赖于Docker安装.这是一个示例,显示我是如何在ARM模板中编写的:

{
  ...,
  "variables": {
    "extensionName": "DockerExtension",
    "vmName": "Docker",
    ...
  },
  "resources": [
    ...,
    {
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "name": "[concat(variables('vmName'), '/', variables('extensionName'))]",
      "tags": {
        "displayName": "DockerExtension"
      },
      "apiVersion": "2015-06-15",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
      ],
      "properties": {
        "publisher": "Microsoft.Azure.Extensions",
        "type": "DockerExtension",
        "typeHandlerVersion": "1.1",
        "autoUpgradeMinorVersion": true,
        "settings": { }
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "name": "[concat(variables('vmName'), '/config')]",
      "apiVersion": "2015-06-15",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/extensions/', variables('vmName'), '/', variables('extensionName'))]"
      ],
      "properties": {
        "publisher": "Microsoft.OSTCExtensions",
        "type": "CustomScriptForLinux", …
Run Code Online (Sandbox Code Playgroud)

azure docker azure-resource-manager

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