简体   繁体   中英

Azure Function v1 using VS 2022 Conflict with Azurite vs AzureStorageEmulator

Can VS 2022 run Azure Function v1 functions? We're attempting to move from VS2019 to VS2022 and the Function project fails to run correctly. It seems that when running it, we get errors related to filename bindings (because it's Azure Function v1, attempting to parse as v2+).

Error Microsoft.Azure.WebJobs.Host: Exception binding parameter 'filename'. Microsoft.Azure.WebJobs.Host: Binding data does not contain expected value 'filename'. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'filename'. Microsoft.Azure.WebJobs.Host: Binding data does not contain expected value 'filename'.

It runs successfully when I stop Azurite, and start AzureStorageEmulator. However it seems there is no way to configure it within the project. I configured the Service Dependencies to use AzureStorageEmulator, but Azurite still starts up. It seems to ignore the AzureStorageEmulator service dependency.

The only reliable method we've found is to run a bat file to automate the AzureStorageEmulator status/stop/start on build.

Is there a way to configure VS 2022 to start AzureStorageEmulator and NOT Azurite? Can VS 2022 work correctly with Azure Functions v1??

So what does a VS-2022 configuration for Azure Function V1 look like? Is it a host.json config? Connected Services? What's the magic bullet?

There are many places you may get unexpected errors when you run the Azure Functions v1 in Visual Studio 2022.

  • When using the binding extensions in Azure Functions for v1 project, you need to check the NuGet Tools or .NET Core CLI Commands whether deprecated or running good from the NuGet Official Site.

  • And if it non-.NET Languages, you need to take care of extension bundles when you're about to migrate from V1 to v2 or earlier. Refer here for more information.

  • We define the service bindings like Input and Output bindings in the function.json file or sometimes it automatically generates the bindings code in that file based on declaring the bindings on function class directly. We need to take care of that file as well when running in latest environment, there might some keywords change.

  • When you're logging the Function runs, there may be TraceWriter class and the compiler tells you to use ILogger or some breaking changes might occur when you update the Azure Functions Core Tools versions.

Our team was able to solve this by updating the Azure v1.x function project with the following *.csproj changes. The solution prevents Azurite from starting, and starts AzureStorageEmulator instead.

New StartAzureStorageEmulator.bat file in the /Resources/Automation/Batch directory.

@echo off
cd "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\"

for /f "tokens=1-2" %%i IN ('"AzureStorageEmulator.exe status"') do (
    IF "%%i"=="IsRunning:" set value=%%j
)

if %value%==True "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" "stop"

"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" "start"
exit /b 0

StartDevelopmentStorage added to the property group.

  <PropertyGroup>
      <StartDevelopmentStorage>False</StartDevelopmentStorage>
  </PropertyGroup>

PostBuild added at the very end after the final ItemGroup node (referencing the New StartAzureStorageEmulator.bat file).

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="if $(ConfigurationName) == Debug call ..\..\Resources\Automation\Batch\StartAzureStorageEmulator.bat" />
  </Target>

As an added benefit, this fix is also VS-2019/2022 compatible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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