简体   繁体   English

Azure function 绑定与手动实例化 class

[英]Azure function binding vs. manually instantiating class

For Azure functions:对于 Azure 函数:
Is there any difference using the Blob binding or manually creating the reference for it?使用 Blob 绑定或手动为其创建引用有什么区别吗?

1: Binding 1:绑定

public Task Run(
    [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
    [Blob("images")] CloudBlobContainer container)
{
    // Code
}

2: Manual 2:手动

public Task Run(
    [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
{
    var account = CloudStorageAccount.Parse(connectionstring);
    var client = account.CreateCloudBlobClient();
    var container = client.GetContainerReference("images");

    // Code
}

I'm only talking a difference in performance.我只是在谈论性能上的差异。
Is it not the exact same thing happening "under the hood" in both examples?在这两个示例中,“幕后”发生的事情不是完全相同吗?

I believe both of the implementation gives same performance.我相信这两种实现都具有相同的性能。

Yes, As Skin Said, Microsoft suggests you avoid hardcoding access to the integration services with Azure Functions.是的,正如 Skin Said 所说,Microsoft 建议您避免使用 Azure 函数对集成服务进行硬编码访问

Scenarios like when the Function is receiving or sending the data (queue message), you can use the Function Parameters for receiving data and return value of the function for sending data.像Function正在接收或发送数据(queue message)的场景,可以使用886982359588的参数接收数据,function的返回值发送数据。

One of the practical examples, I come to know the difference and usage of this bindings and hardcode in the Azure functions from Pluralsight course is:其中一个实际示例,我从 Pluralsight 课程中了解到 Azure 函数中此绑定和硬编码的区别和用法是:

Here in below code, I took Blob output binding the Function level:在下面的代码中,我将 Blob output 绑定到 Function 级别:

public class GenerateLicenseFile
{
[FunctionName("GenerateLicenseFile")]
public void Run([QueueTrigger("orders", Connection = "AzureWebJobsStorage")]Order order,
[Blob("licenses/{rand-guid}.lic")]TextWriter outputBlob,
ILogger log)
{
outputBlob.WriteLine($"OrderId: {order.OrderId}");
outputBlob.WriteLine($"Email: {order.Email}");
outputBlob.WriteLine($"ProductId: {order.ProductId}");
outputBlob.WriteLine($"PurchaseDate: {DateTime.UtcNow}");

To bring the Order Id as the license File name,要将订单 ID 作为许可证文件名,

public  static  class  GenerateLicenseFile

{

[FunctionName("GenerateLicenseFile")]
public  static  async  Task  Run(
[QueueTrigger("orders", Connection  =  "AzureWebJobsStorage")] Order  order,
IBinder  binder,
ILogger  log)
{
var  outputBlob  =  await  binder.BindAsync<TextWriter>(
new  BlobAttribute($"licenses/{order.OrderId}.lic")
{
Connection  =  "AzureWebJobsStorage"
});
outputBlob.WriteLine($"OrderId: {order.OrderId}");
outputBlob.WriteLine($"Email: {order.Email}");
outputBlob.WriteLine($"ProductId: {order.ProductId}");
outputBlob.WriteLine($"PurchaseDate: {DateTime.UtcNow}");

There are some scenarios mentioned when to use bindings as parameters and hardcode in the Azure Functions, please refer this Microsoft Documentation for more information.在 Azure 函数中提到了一些何时使用绑定作为参数和硬编码的场景,请参阅此Microsoft 文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM