Azure / azure-iot-sdk-csharp

A C# SDK for connecting devices to Microsoft Azure IoT services
Other
464 stars 493 forks source link

[Technical Question] Develop and Deploy C# Azure Function, custom IoT Edge Module in .NET SDK 6 #3339

Closed shivatyagi313 closed 11 months ago

shivatyagi313 commented 1 year ago

When I create a C# Azure Function IoT Edge Module, by default the target framework is netstandard2.0. The .csproj file of the module looks like this :

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion></AzureFunctionsVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp3.1|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
    <TreatSpecificWarningsAsErrors />
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EdgeHub" Version="1.0.7" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.2.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

and the Dockerfile.amd64 looks like this :

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/azure-functions/dotnet:3.0

ENV AzureWebJobsScriptRoot=/app

ENV AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=build-env /app/out /app

This azure function requires .net SDK 3 to Build and Push. The function triggers and works fine when a new message is received.

I want the module to be build in .NET 6, so in order to do that I manually changed the target framework of the module to .net 6 upgraded all the nuGet packages, updated the Dockerfile.amd64 to .net sdk 6, so the module was build successfully but now it is not triggering when a new message is received.

Is is possible to upgrade the C# Azure function IoT edge module to work in .net6 framework? if yes, then what could be a good approach to make the module work in .net 6, as the way I tried is not working and does not looks like a good approach.

smondalgithub commented 1 year ago

@shivatyagi313 Is there any update on this?

ancaantochi commented 1 year ago

From functions runtime docs I see that it needs an upgrade of the runtime to 4.x and there are some breaking changes between 3.x and 4.x https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=v4&pivots=programming-language-csharp#languages

shivatyagi313 commented 1 year ago

From functions runtime docs I see that it needs an upgrade of the runtime to 4.x and there are some breaking changes between 3.x and 4.x https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=v4&pivots=programming-language-csharp#languages

Yes @ancaantochi , I tried updating the runtime from 3.0 to 4.0, and I was able to build the module as well, but the problem is that, it is not triggering, on arrival of new messages, after changing the target framework and runtime.

shivatyagi313 commented 1 year ago

@shivatyagi313 Is there any update on this?

No updates yet @smondalgithub. Can you please check on this?

smondalgithub commented 1 year ago

@ancaantochi can you please review this?

ancaantochi commented 1 year ago

@shivatyagi313 Did you also update the edgehub binding (Microsoft.Azure.WebJobs.Extensions.EdgeHub) ? That also needs to be updated as it uses Microsoft.Azure.WebJobs 3.0.36. I see that 4.x version is Microsoft.Azure.WebJobs.Extensions, this package includes Microsoft.Azure.WebJobs: https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started#install-webjobs-nuget-packages

shivatyagi313 commented 1 year ago

Microsoft.Azure.WebJobs.Extensions.EdgeHub @ancaantochi , thanks for the reply, Yes I have updated all the nuget packages as per version 4.x. Still the modules are not triggering. Please help!

ancaantochi commented 1 year ago

Do you have a fork where you made the changes, can you please send me a link? Also can you send the functions module logs?

shivatyagi313 commented 1 year ago

Hi @ancaantochi , Thanks, but I don't see any errors when the modules are deployed and running, its just it doesn't trigger when it receives any new messages.

Only able to see the below in Iot hub -> module -> troubleshoot logs Hosting environment: Production Content root path: /azure-functions-host Now listening on: http://[::]:80 Application started. Press Ctrl+C to shut down.

timtay-microsoft commented 11 months ago

It sounds like this is more a question of edgehub behavior, so feel free to re-open this thread in that repo if you are still running into issues here.