我有一些类库,我在ASP.NET Web API应用程序中使用它来处理我的所有后端内容,例如对多个数据库(如Azure SQL数据库,Cosmos DB等)的CRUD操作.
我不想重新发明轮子,并且能够在我在Visual Studio 2017中创建的新Azure功能中使用它们.我的所有存储库方法都使用接口.那么,我将如何在我的新Azure功能中实现依赖注入?
我没有看到对DI的任何支持,但我有点困惑.似乎Azure Functions基于与WebJobs相同的SDK,我认为去年微软已经开始在WebJobs中支持DI - 我当然知道,因为我使用Ninject实现了它.
有没有解决方法,以便我可以在我的新Azure功能项目中使用我现有的库?
我有一个项目引用2个软件包的问题,然后引用Newtonsoft.Json,但两个版本都不同.我使用的NuGet包Refit和Microsoft.NET.Sdk.Functions.当我尝试构建解决方案时,我收到以下错误:
C:\dev\noddy\noddy-api\src\noddy.Api>dotnet build
C:\dev\noddy\noddy-api\src\noddy.Api\noddy.Api.csproj : error NU1607: Version conflict detected for Newtonsoft.Json. Reference the package directly from the project to resolve this issue. \r
C:\dev\noddy\noddy-api\src\noddy.Api\noddy.Api.csproj : error NU1607: noddy.Api (>= 1.0.0) -> Refit (>= 4.0.1) -> Newtonsoft.Json (>= 10.0.3) \r
C:\dev\noddy\noddy-api\src\noddy.Api\noddy.Api.csproj : error NU1607: noddy.Api (>= 1.0.0) -> Microsoft.NET.Sdk.Functions (>= 1.0.2) -> Newtonsoft.Json (>= 9.0.1).
Run Code Online (Sandbox Code Playgroud)
现在,如果我按照它说的做,并直接从我的项目引用包,我在构建时会遇到另一个错误:
C:\dev\noddy\noddy-api\src\noddy.Api>dotnet add package Newtonsoft.Json
Writing C:\Users\joebloggs\AppData\Local\Temp\tmp7250.tmp
info : Adding PackageReference for package 'Newtonsoft.Json' into project 'C:\dev\noddy\noddy-api\src\noddy.Api\noddy.Api.csproj'.
log : Restoring packages …Run Code Online (Sandbox Code Playgroud) 我正在玩Azure功能.但是,我觉得我很难接受一些非常简单的事情.我想弄清楚如何返回一些基本的JSON.我不确定如何创建一些JSON并将其恢复到我的请求.
曾几何时,我会创建一个对象,填充其属性并对其进行序列化.所以,我开始沿着这条路走下去:
#r "Newtonsoft.Json"
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"Running Function");
try {
log.Info($"Function ran");
var myJSON = GetJson();
// I want myJSON to look like:
// {
// firstName:'John',
// lastName: 'Doe',
// orders: [
// { id:1, description:'...' },
// ...
// ]
// }
return ?;
} catch (Exception ex) {
// TODO: Return/log exception
return null;
}
}
public static ? GetJson()
{
var person = new Person(); …Run Code Online (Sandbox Code Playgroud) 如何重用azure函数中可用的TraceWriter对象来记录外部引用的库中的信息?我尝试使用构造函数传入对象并引用TraceWriter类(web.http.tracing).我没有运气,因为课程看起来不同.
我正在将代码从 .NET 3.0 升级到 .NET 5.0,这对 sintaxis 做了很大的改变。在我之前的代码中,这是在 AZURE FUNCTIONS .NET 5.0 隔离中构建的 http 请求,构建了一个采用参数的 GET api。
这是我之前来自 .NET 3.0 的代码
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
byte[] RSA_Key_to_Correct = new byte[0x80];
string array_var = req.Query["array_var"];
string i = req.Query["i"];
string incrementing_value = req.Query["incrementing_value"];
}
Run Code Online (Sandbox Code Playgroud)
我只是找不到一种方法来使用req从 api 调用中获取参数,就像在 .NET 3.0 上完成的那样string i = req.Query["i"];
在 .NET …
我想通过选择aws-lambda,azure函数或google云函数来开发无服务器项目,然后我想比较每个项目以便为我的项目选择更好的项目.
我需要为我的项目需求选择最稳定的无服务器技术.这些技术比较有什么材料吗?这些无服务器技术是否有任何受支持的框架?
Azure Functions的扩展文档对Azure Functions如何决定何时添加应用程序的更多实例的细节有所了解.
比方说,我有一个由Github webhook触发的功能.10000人同时提交到GitHub库(无合并冲突;)),并在Github上的时间很短的时间调用我的函数10000次.
我能期待什么?特别,
我的Azure功能代码如下所示
public static class MyHttpTriggerFunction
{
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
// some business logic
if (valid)
{
return req.CreateResponse(HttpStatusCode.OK, true);
}
else
{
return req.CreateResponse(HttpStatusCode.BadRequest, "some error message");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试项目中,我正在阅读如下结果:
var result = await MyHttpTriggerFunction.Run(req, log).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)
执行该函数后,当它尝试在结果变量中返回响应时,测试方法失败并出现异常.
**
System.InvalidOperationException:请求没有关联的配置对象,或者提供的配置为null.
**
我确保测试项目具有相同的System.Net.Http.HttpRequestMessageExtensiondll.
如果我更改功能代码不使用CreateResponse扩展方法(此扩展方法来自VS 2017模板的代码)并返回如下所示的响应,我会在测试方法中获得响应并且测试用例运行正常.
var res = new HttpResponseMessage();
if (valid)
{
res.StatusCode = HttpStatusCode.OK;
res.Content = new ObjectContent<bool>(true, new JsonMediaTypeFormatter());
return res;
}
else …Run Code Online (Sandbox Code Playgroud) 通常在 .NET Core 项目中,我会创建一个“boostrap”类来配置我的服务以及 DI 注册命令。这通常是一个扩展方法IServiceCollection,我可以在其中调用类似的方法,.AddCosmosDbService并且在包含该方法的静态类中所需的一切都是“自包含”的。但关键是该方法IConfiguration从Startup类中获取。
我过去曾在 Azure Functions 中使用 DI,但尚未遇到此特定要求。
当函数部署在 Azure 中时,我使用IConfiguration来绑定到具有与我的local.settings.json以及开发/生产应用程序设置中的设置匹配的属性的具体类。
/// <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)
public static class BootstrapCosmosDbClient
{
/// …Run Code Online (Sandbox Code Playgroud) c# dependency-injection azure-functions .net-core-configuration
azure-functions ×10
azure ×6
c# ×6
.net-5 ×1
asp.net ×1
aws-lambda ×1
json ×1
logging ×1
msbuild ×1
unit-testing ×1
webhooks ×1