需要将我的Azure函数运行为异步方法吗?

huangapple go评论63阅读模式
英文:

Do I need to run my Azure function as an async method?

问题

以下是翻译好的部分:

QUESTIONS - 为什么我的Azure函数项目(独立进程)在program.cs文件中没有命名空间、类名或主方法?创建函数项目时,Azure工具为什么没有生成这些内容,但仍然可以运行?我需要它吗?

此外,Microsoft示例(最后一个示例)都使用异步方法,我应该将我的program.cs文件设为异步吗?我认为在Azure函数中这样做会失去意义,因为每个函数都在自己的进程中运行?

我创建了一个net7.0的Azure函数项目,注意到我的program.cs文件的代码如下,没有命名空间、program类或主方法:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .Build();
host.Run();

而在Microsoft在Github上提供的示例中,它们都包含了命名空间、program类和主方法,如下所示:

namespace Function
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string sqlConnectionString = Environment.GetEnvironmentVariable("SqlConnectionString");
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(s =>
                {
                    s.AddDbContext<BloggingContext>(
                        options => options.UseSqlServer(sqlConnectionString)
                    );
                })
                .Build();

            await host.RunAsync();
        }
    }
}
英文:

QUESTIONS - Why does my Azure function project (isolated process) not contain a namespace, class name or main method in the program.cs file? How come the Azure tools didn't generate this when I created the function project, but still runs? So do I need it?

Also the Microsoft examples (bottom example) all use async methods, should I make my program.cs file async, I thought with Azure functions that would defeat the purpose because each function gets run in its own process?

I created a net7.0 azure function project and noticed that my program.cs file has code like this below with no namespace, program class or main method:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .Build();
host.Run();

<!-- end snippet -->

And looking at examples that Microsoft has on Github they all have namespaces, program classes and main methods like below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

namespace Function
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string sqlConnectionString = Environment.GetEnvironmentVariable(&quot;SqlConnectionString&quot;);
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(s =&gt; {
                    s.AddDbContext&lt;BloggingContext&gt;(
                        options =&gt; options.UseSqlServer(sqlConnectionString)
                    );
                })
                .Build();

            await host.RunAsync();
        }
    }
}

<!-- end snippet -->

答案1

得分: 1

这是一个部分回答,希望能有所帮助。

你在谈论的是在.NET 6中引入的“顶级语句”(top level statements)。在顶级语句的入口中,命名空间和主方法是隐式的,如果在其中使用了await,它将被生成为异步的。

微软的大多数示例要么是在此引入之前编写的,要么为了清晰起见而添加了这些元素。

我不知道有人在生产中使用真正的顶级语句(除非是用于 web/service/di 方面的 IHostBuilder ),我曾经使用它们来提取主方法,但在文件的顶部包括一个没有主体的命名空间声明以进行说明。

namespace Function;
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .Build();
await host.RunAsync();

在功能上等同于:

namespace Function
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .Build();
            await host.RunAsync(); 
        }
    }
}
英文:

This is a partial answer, hopefully it helps.

You are talking about 'top level statements' which were introduced in .NET 6. In a top level statement entrypoint, the namespace and main method are implicit, and it'll be generated as async if you use await anywhere within it.

Most of the examples from Microsoft were either written before this introduction, or had these elements added for clarity.

I don't know anyone that uses true top level statements in production (unless it's the IHostBuilder for web/service/di concerns), I've used them to extract the main method but included a bodyless namespace declaration at the top of the file to clarify that.

namespace Function;
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .Build();
await host.RunAsync();

Is functionally identical to:

namespace Function
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .Build();
            await host.RunAsync(); 
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月1日 14:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定