小编Jas*_*SFT的帖子

配置服务时如何通过依赖注入在 Azure Function V3 中注入或使用 IConfiguration

通常在 .NET Core 项目中,我会创建一个“boostrap”类来配置我的服务以及 DI 注册命令。这通常是一个扩展方法IServiceCollection,我可以在其中调用类似的方法,.AddCosmosDbService并且在包含该方法的静态类中所需的一切都是“自包含”的。但关键是该方法IConfigurationStartup类中获取。

我过去曾在 Azure Functions 中使用 DI,但尚未遇到此特定要求。

当函数部署在 Azure 中时,我使用IConfiguration来绑定到具有与我的local.settings.json以及开发/生产应用程序设置中的设置匹配的属性的具体类。

CosmosDbClientSettings.cs

/// <summary>
/// Holds configuration settings from local.settings.json or application configuration
/// </summary>    
public class CosmosDbClientSettings
{
    public string CosmosDbDatabaseName { get; set; }
    public string CosmosDbCollectionName { get; set; }
    public string CosmosDbAccount { get; set; }
    public string CosmosDbKey { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

BootstrapCosmosDbClient.cs

public static class BootstrapCosmosDbClient
{
    /// …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection azure-functions .net-core-configuration

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

SignalR .NET客户端连接到Blazor .NET Core 3应用程序中的Azure SignalR服务

我正在尝试在ASP.NET Core 3.0 Blazor(服务器端)应用程序与Azure SignalR服务之间建立连接。最后,我将SignalR客户端(服务)注入到一些Blazor组件中,以便它们可以实时更新我的​​UI / DOM。

我的问题是,.StartAsync()在集线器连接上调用方法时,我收到以下消息:

响应状态代码不表示成功:404(未找到)。

BootstrapSignalRClient.cs

该文件加载了我对SignalR服务的配置,包括URL,连接字符串,键,方法名称和集线器名称。这些设置在静态类中捕获,SignalRServiceConfiguration并在以后使用。

public static class BootstrapSignalRClient
{
    public static IServiceCollection AddSignalRServiceClient(this IServiceCollection services, IConfiguration configuration)
    {
        SignalRServiceConfiguration signalRServiceConfiguration = new SignalRServiceConfiguration();
        configuration.Bind(nameof(SignalRServiceConfiguration), signalRServiceConfiguration);

        services.AddSingleton(signalRServiceConfiguration);
        services.AddSingleton<ISignalRClient, SignalRClient>();

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

SignalRServiceConfiguration.cs

public class SignalRServiceConfiguration
{
    public string ConnectionString { get; set; }
    public string Url { get; set; }
    public string MethodName { get; set; }
    public string Key { get; set; }
    public string HubName { …
Run Code Online (Sandbox Code Playgroud)

c# signalr signalr.client asp.net-core-signalr azure-signalr

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

如何在 ASP.NET Core 2.x 中以会话状态存储和检索对象?

我如何将 EmployeeId 和 DesignationId 添加到对象,然后从会话状态中检索它?

这是我的登录控制器代码:

DataTable dt = sql.GetDataTable("select * from EmpDetails where EmailId = '" + EmailId + "'");
string strempstatus = dt.Rows[0]["Status"].ToString();
string EmpStatus = strempstatus.TrimEnd();

//Models.UserDetails detail = new Models.UserDetails();

if (EmpStatus == "Verified")
{
    //i want to create object which store below two variable value
    string EmployeeId = dt.Rows[0]["Id"].ToString();
    string DesignationId = dt.Rows[0]["D_Id"].ToString();

    //I want to stored object in below session
    HttpContext.Session.SetString("EmployeeData", EmployeeId);

    HttpContext.Session.SetInt32("EmployeeID", Convert.ToInt32(EmployeeId));
    //For Destroy Session
    //HttpContext.Session.Remove("EmployeeID");

    Int32? Saved = HttpContext.Session.GetInt32("EmployeeID");

    if (DesignationId …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

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

无法调用/加载.NET Core程序集,但.NET Standard在PowerShell中使用Add-Type可以正常工作

在尝试为PowerShell创建.NET Core类库时,我发现.NET Standard和.NET Core之间存在一些奇怪的差异.

按照指南,我创建了一个简单的.NET Standard 2.0类库,并且能够使用在PowerShell 5.1和PowerShell 6.0.1中成功加载它Add-Type -Path [path].

namespace Animal
{
    public class Dog
    {
        public string Bark()
        {
            return "Woof!";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以调用该.Bark()方法并在PowerShell 5.1和PowerShell Core 6.0.1中查看输出.

当我使用.NET Core 2.2模板构建同一个类时,我无法在PowerShell实例中使用它.代码几乎相同:

namespace AnimalCore
{
    public class Dog
    {
        public string Bark()
        {
            return "Woof!";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PowerShell 5.1:

Add-Type:无法加载一个或多个请求的类型.检索LoaderExceptions属性以获取更多信息.

PowerShell Core 6.0.1

无法找到类型[AnimalCore.Dog]

我很高兴继续使用.NET标准模板来构建我的程序集但是我想知道这里有什么区别以及如何编译这个类库以便在.NET Core上使用?

c# powershell

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

错误:对 Cosmos DB 和 Entity Framework Core 3.1.2 进行“更新”调用时出现“备用键属性 'id' 为 null”

假设我有一个PersonModel如下的简单模型,我FirstName从 ASP.NET Core 3.1 Blazor 或 MVC UI(Web 表单)更新属性:

public class PersonModel 
{
  public Guid PersonModelId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我将模型发送到 http 触发的 Azure 函数并绑定到模型。这是我实现 EF Core 并执行以下操作来更新它的地方:

public class PersonModel 
{
  public Guid PersonModelId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我使用此模型创建或删除实体没有任何问题,并且根据以下文章,我没有看到创建一个根据任何特定存储库要求(即分区键或主键等)“干净”的模型有任何问题但是我似乎无法运行更新代码而不出现以下错误:

无法跟踪“PersonModel”类型的实体,因为备用键属性“id”为 null。

有谁知道模型的具体要求并使用此方法来更新实体?有没有像文档文章中的示例一样简单/优雅的替代方案?

我已经尝试过, …

c# entity-framework entity-framework-core asp.net-core

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

Microsoft Graph Client SDK - 按名称过滤组

这是一个相当简单的问题,但我无法将httpClient查询图表的基本方法转换为 SDK 方法。我正在使用以下内容并且工作正常:

    var filter = "IT";
    var response = await httpClient.GetAsync($"{webOptions.GraphApiUrl}/beta/groups?$filter=startswith(displayName, '{filter}')&$select=id,displayName");
Run Code Online (Sandbox Code Playgroud)

...现在我尝试使用 SDK 进行过滤,如下所示:

    var groups = await graphServiceClient.Groups
        .Request()
        .Filter($"displayName startswith {filter}")
        .Select("id, displayName")
        .GetAsync();
Run Code Online (Sandbox Code Playgroud)

我也尝试过.Filter($"startswith("displayName", {filter}))其他变体。

我收到invalid filter clause错误。有任何想法吗?

microsoft-graph-api

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

从 C# 中的键/值集合中读取特定的 HTTP 标头值

我正在尝试从 HTTP 方法中读取两个标头值,但我的 C# 知识存在一些空白。Results View调试时我可以在本地人中看到 a ,我也可以看到它是 aSystem.Collections.Generic但我不知道如何访问此列表以获取我的两个值。

这是我的 Azure Function App 代码:

[FunctionName("Tenant")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Tenant/{DomainName}")]HttpRequestMessage req, string DomainName, [Inject(typeof(ITableOps))]ITableOps _tableOps, TraceWriter log)
{
    var headers = req.Headers;
    var settings = _tableOps.GetTenantSettingsAsync(DomainName);
    return req.CreateResponse(HttpStatusCode.OK, settings.Result);
}
Run Code Online (Sandbox Code Playgroud)

结果视图

从这个键/值列表中,我想命名键apikeydomainName并且我尝试了各种奇怪的方法来做到这一点:

 headers.ToList()[0]
Run Code Online (Sandbox Code Playgroud)

或者

var key = headers.Where(x => x.Key == "apikey")
Run Code Online (Sandbox Code Playgroud)

鉴于第二个例子,key最终成为另一个列表:

列表

我已经看到了很多关于使用 foreach 循环的例子,但就我而言,我认为我可以使用 lambda 来提取值,因为我期待两个非常具体的键名。我意识到这很容易获得,但我不知道如何进行。

c#

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

Blazor 表单提交需要两次单击才能刷新视图

我有以下 Blazor 组件,我正在测试 API 调用以获取天气数据。出于某种原因,需要单击提交按钮两次才能使表格显示更新后的对象的属性。

当页面初始化时,我从浏览器中获取位置就好了,天气数据显示在表中没有问题。

获取数据.razor

@page "/fetchdata"
@using BlazingDemo.Client.Models
@using AspNetMonsters.Blazor.Geolocation

@inject HttpClient Http
@inject LocationService LocationService

<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>

<div>
    <EditForm Model="@weatherForm" OnValidSubmit="@GetWeatherDataAsync">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <InputText Id="cityName" bind-Value="@weatherForm.CityName" />
        <button type="submit">Submit</button>
    </EditForm>
</div>
<br />

@if (weatherData == null)
{
    <Loader/>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>City</th>
                <th>Conditions</th>
                <th>Temp. (C)</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>@weatherData.Name</td>
                <td>@weatherData.Weather[0].Main</td>
                <td>@weatherData.Main.Temp</td>
            </tr>
        </tbody>
    </table>
}

@functions {

    WeatherFormModel weatherForm = …
Run Code Online (Sandbox Code Playgroud)

webassembly blazor

3
推荐指数
2
解决办法
2702
查看次数

正则表达式匹配字符串中的两个单词

我正在尝试匹配(查找)字符串中的两个单词,如下所示:

Mac OS X/10.11.5 (15F34); ExchangeWebServices/6.0 (243);
Run Code Online (Sandbox Code Playgroud)

如果我们看到" Mac"和" ExchangeWebServices",我想匹配(true),但两个单词之间的字符将是未知/随机的.有人可以用正则表达式语法帮助我吗?

谢谢!

regex

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

构建 PSCustomObject 时的 PowerShell 条件语句

我想在创建 PSCustomObject 时检查变量是否存在。我有相当多的对象需要查询并将数据收集到我的新对象中,因此我不想用“if”语句复制整个代码块,因为我试图简洁。

[array]$newObject += [PSCustomObject][ordered]@{
  JitterInterArrival = (if ($_.QoeReport.AudioStreams){$_.QoeReport.AudioStreams[0].JitterInterArrival}else{"N/A"}
}
Run Code Online (Sandbox Code Playgroud)

我知道上面的块会产生一个错误,即“if”语句无法识别。是否有另一种方法可以在定义 PSCustomObject 时包含代码块?

powershell pscustomobject

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