我已成功将依赖项注入用于我自己的自定义服务,如here所述。
我想要的是使用框架将Binder作为参数注入到我的自定义服务中的Azure Functions中。 用作函数参数的示例:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, Binder binder, TraceWriter log)
我尝试仅将Binder作为参数添加到服务构造函数中,并让框架创建服务实例,但传入的Binder始终为空。
我的服务构造函数:
public MyService(Binder binder)
这样注册:
builder.Services.AddSingleton<IMyService, MyService>();
有人知道这是否可能吗,如果可以,我在做什么错了?
答案 0 :(得分:0)
您首先必须创建一个这样的类,该类允许您读取/写入:
public static class Bindor
{
public static async Task<string> ReadFromBlob(string path, Binder binder, string connection)
{
var attributes = new Attribute[]
{
new BlobAttribute(path),
new StorageAccountAttribute(connection)
};
using (var reader = await binder.BindAsync<TextReader>(attributes))
{
return await reader.ReadToEndAsync();
}
}
public static async Task BindToBlob(string path, Binder binder, object payload, string connection)
{
var attributes = new Attribute[]
{
new BlobAttribute(path),
new StorageAccountAttribute(connection)
};
using (var writer = await binder.BindAsync<TextWriter>(attributes))
{
writer.Write(payload);
}
}
}
然后,您将在函数中包含Binder binder
作为参数(就像您所做的一样)。
示例用法如下:
//path would be your path to the blob (including the container)
//binder is the parameter from your function signature
//sorry about the confusing naming, Bindor is the class we created, whereas Binder is the functions class
await Bindor.BindToBlob(path, binder, "my payload", connection);