简体   繁体   中英

Turn existing ASP.NET Core Web App to Edge module running on Azure IoT Edge

I have an existing ASP.NET Core Web App running as a docker container and would like to provide it as a Edge Module running on Azure IoT Edge. From the docs i know i can run Azure Functions, Stream Analytics and Custom modules (which from my understanding are just console applications integrating with the Azure IoT Edge Runtime).

What is the best way to turn my ASP.NET Core Web App into an Edge Module and interact with Edge Hub?

Would the best approach be to use a custom module as a template, move my ASP.NET Core project over to fit the file structure and edit the dockerfiles to run my main ASP.NET Core Assembly?

Thank you for any advise!

Update: I was following the approach stated above. I created a custom edge module and tried to convert it to the simplest possible ASP.NET Core Web App using the following steps:

  1. Add package reference <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
  2. Add Startup class

    public class Startup {

      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.Run(async r => await r.Response.Body.WriteAsync(Encoding.UTF8.GetBytes("seas"))); loggerFactory.AddConsole(); } } 
  3. Add this method to Program.cs:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup();

  4. Replace content of Main with CreateWebHostBuilder(args).Build().Run();

I can start the container as part of Azure IoT Edge but the container constantly keeps restarting, so i assume, my approach was not really right. Unfortunately i also can't get access to the console of the container because it's restarting every few seconds...

output of sudo docker ps ...

4b23cdad5bc5        localhost:5000/simpleweb:0.0.1-amd64                              
"dotnet SimpleWeb.dll"    5 minutes ago       Restarting (150) 58 seconds ago

...

PS: I am using iot edge dev container for testing following this quickstart: https://github.com/Azure/iotedgedev/wiki/quickstart-with-iot-edge-dev-container

If you app is already containerized, there shouldn't be really much that you need to add, to make this an Edge module:

  • Add nuget package Microsoft.Azure.Devices.Client
  • Init ModuleClient somewhere in your code, probably in some startup routine: ModuleClient moduleClient = await ModuleClient.CreateFromEnvironmentAsync(transportType);

  • Use the moduleClient to send and receive messages alongside your asp.net stuff

  • Add this container as a module in your deployment.json (and make it available in a container repo)

This should be pretty straightforward. You don't need to start from the module templates or Dockerfiles. If you look at them, there is really no magic going on.

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