Jan*_*n_V 11 c# azure azure-functions
我正在尝试为Azure Functions创建自己的自定义绑定.这项工作基于2篇关于此功能的维基文章:https: //github.com/Azure/azure-webjobs-sdk/wiki/Creating-custom-input-and-output-bindings 和 https://github.com/天青/ WebJobsExtensionSamples
对于示例项目,我指的是Azure Functions/WebJobs绑定扩展示例项目.该项目基于.NET Framework 4.6.
我希望自己的自定义绑定能够与Azure Functions v2一起使用,所以我在所有项目中都使用NetStandard2.
在示例项目中,我看到在启动本地模拟器时加载了绑定扩展.
[3-1-2019 08:48:02]加载的绑定扩展'SampleExtensions'来自'reference by:Method ='FunctionApp.WriterFunction.Run',Parameter ='sampleOutput'.'
但是,在运行我自己的绑定扩展时,我从未看到此行出现.
我所做的是以下内容.首先,我创建了一个新属性.
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
[Binding]
public class MySimpleBindingAttribute : Attribute
{
/// <summary>
/// Path to the folder where a file should be written.
/// </summary>
[AutoResolve]
public string Location { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还有一个相当简单的扩展类
public class MySimpleBindingExtension : IExtensionConfigProvider
{
public void Initialize(ExtensionConfigContext context)
{
var rule = context.AddBindingRule<MySimpleBindingAttribute>();
rule.BindToInput<MySimpleModel>(BuildItemFromAttribute);
}
private MySimpleModel BuildItemFromAttribute(MySimpleBindingAttribute arg)
{
string content = default(string);
if (File.Exists(arg.Location))
{
content = File.ReadAllText(arg.Location);
}
return new MySimpleModel
{
FullFilePath = arg.Location,
Content = content
};
}
}
Run Code Online (Sandbox Code Playgroud)
当然还有模型.
public class MySimpleModel
{
public string FullFilePath { get; set; }
public string Content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
从我在维基上阅读的内容来看,我认为这应该足以在我的Azure Functions项目中使用它.函数看起来像这样.
[FunctionName("CustomBindingFunction")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{name}")]
HttpRequest req,
string name,
[MySimpleBinding(Location = "%filepath%\\{name}")]
MySimpleModel simpleModel)
{
return (ActionResult) new OkObjectResult(simpleModel.Content);
}
Run Code Online (Sandbox Code Playgroud)
在模拟器中运行时,我看到以下错误和警告消息.
[3-1-2019 08:51:37]错误索引方法'CustomBindingFunction.Run'
[3-1-2019 08:51:37] Microsoft.Azure.WebJobs.Host:错误索引方法'CustomBindingFunction.Run'.Microsoft.Azure.WebJobs.Host:无法将参数'simpleModel'绑定到MySimpleModel类型.确保绑定支持参数Type.如果您正在使用绑定扩展(例如Azure存储,ServiceBus,计时器等),请确保已在启动代码中调用扩展的注册方法(例如builder.AddAzureStorage(),builder.AddServiceBus( ),builder.AddTimers()等).
[3-1-2019 08:51:37]函数'CustomBindingFunction.Run'索引失败,将被禁用.
[3-1-2019 08:51:37]找不到工作职能.尝试公开您的工作类和方法.如果您正在使用绑定扩展(例如Azure存储,ServiceBus,计时器等),请确保已在启动代码中调用扩展的注册方法(例如builder.AddAzureStorage(),builder.AddServiceBus( ),builder.AddTimers()等).
现在,此消息在WebJob场景中有意义,但对于Azure功能,您无法在启动代码中设置某些内容.调用函数会抛出以下消息.
[3-1-2019 08:53:13]发生了未处理的主机错误.
[3-1-2019 08:53:13] Microsoft.Azure.WebJobs.Host:无法从Azure WebJobs SDK调用'CustomBindingFunction'.是否缺少Azure WebJobs SDK属性?
我已经阅读了一些关于添加extensions.json文件并使用AzureWebJobs_ExtensionsPath(for v1)的内容,但那些似乎没有做任何事情(或者我做错了什么).
关于如何进行的任何想法?
为了完整起见,这是我目前对NuGet包的引用.
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.3" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
Run Code Online (Sandbox Code Playgroud)
在其他示例的帮助和指导下,我能够找出需要添加的内容以使您的自定义绑定正常工作.
我的猜测是,这将随着时间的推移在即将发布的运行时版本中发生变化,但谁知道......
首先,添加一个小扩展方法,在其中添加扩展名IWebJobsBuilder.
public static class MySimpleBindingExtension
{
public static IWebJobsBuilder AddMySimpleBinding(this IWebJobsBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AddExtension<MySimpleBinding>();
return builder;
}
}
Run Code Online (Sandbox Code Playgroud)
接下来是IWebJobsStartup实施.这将由运行时通过反射在启动时获取.
public class MySimpleBindingStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.AddMySimpleBinding();
}
}
Run Code Online (Sandbox Code Playgroud)
你不能忘记的一件事是将以下内容添加到这个类:
[assembly: WebJobsStartup(typeof(MySimpleBindingStartup))]
Run Code Online (Sandbox Code Playgroud)
你很快就会注意到这一点,因为如果你忘记了,绑定将不会被提起.
希望这有助于每个人遇到同样的问题.如果你想看到完整的代码,我在GitHub存储库中有一个示例项目:https://github.com/Jandev/CustomBindings