ExecutionContext 在 Microsoft.Azure.Functions.Worker 中的实现?

问题描述 投票:0回答:1

我正在将用net461编写的Azure Function V1升级到net48中的Azure Functions V4,同时在

ExecutionContext
中存在一些
Microsoft.Azure.WebJobs
,这里正在使用
string appDirectoryPath = context.FunctionDirectory;
,请帮助我找到一个可以与
一起使用的实现Microsoft.Azure.Functions.Worker

.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <AzureFunctionsVersion>v1</AzureFunctionsVersion>
  </PropertyGroup>
 <ItemGroup>
   <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
   <PackageReference Include="Microsoft.Azure.ServiceBus" Version="3.4.0" />
   <PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.2.0" />
   <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
   <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
   <PackageReference Include="SSH.NET" Version="2016.1.0" />
   <PackageReference Include="WindowsAzure.Storage" Version="9.3.2" />
 </ItemGroup>

.csproj

  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.3.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.3.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>
.net azure azure-functions
1个回答
0
投票

隔离函数确实有

ExecutionContext
但它没有
FunctionDirectory
属性,请参阅此 MS Doc

如果您想访问功能位置,可以使用

Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
。通过使用这个,我能够获得预期的功能位置。

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.IO;
using System;
using System.Net;
using System.Reflection;
using System.Threading;

namespace _78372475
{
    public class Function1
    {
        private readonly ILogger _logger;

        public Function1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<Function1>();
        }

        [Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");

            var path = Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            _logger.LogInformation("Function Path: {Path}", path);

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!");

            return response;
        }
    }
}

.csproj-

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <RootNamespace>_78372475</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.3-preview1" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
</Project>

输出-

enter image description here

参考文献:-

访问 .NET 5 Azure Function 中的 FunctionAppDirectory - 堆栈内存溢出

© www.soinside.com 2019 - 2024. All rights reserved.