3

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

Markus S.
  • 2,602
  • 13
  • 44

1 Answers1

4

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.

silent
  • 14,494
  • 4
  • 46
  • 86
  • thank you for your answer. i will try your suggestions and get back to you! – Markus S. Jun 04 '19 at 16:04
  • Hi, I need to convert my webapi service(asp.net core) into iot edge module. Is it possible to do this using above approach. – Mohan Gopi Aug 11 '20 at 11:19
  • I'm not able to add webapi project as iot edge module. The webapi project having api controllers and other things. The custom edge module works as console application right. then how can we add. Can you help on this. – Mohan Gopi Aug 11 '20 at 14:28
  • I need some steps to follow. If you suggest or share me it will be great help for me. – Mohan Gopi Aug 11 '20 at 14:42
  • I wouldnt see any reason why you should not be able to add your webapi project. IoT Edge does not care if the app is a console application or whatever – silent Aug 12 '20 at 10:11
  • The advice above should apply for those projects as well – silent Aug 12 '20 at 10:11
  • I created new "Azure IoT Edge(windows amd64)" project using visual studio 2019. So I created WebAPI project under solution(Included the ModuleClient in the startup.cs file), then we try to add the api project as a module under the edge module, but its not taken. Is it anything I'm missing here. Note: The deployment.template.json present under the edge project. we used to send message to upstream. Can you guide me here, if anything if we missed. – Mohan Gopi Aug 14 '20 at 12:00
  • @silent: Thanks for help. I've created sample project of webapi into iot edge module. https://github.com/MohanGopi2010/IotEdge-WebApi-Sample Let me know if anything we need to add up or missing. Thanks. – Mohan Gopi Sep 07 '20 at 11:23
  • well, does it work? ;) Looking at your template, I guess you are missing to expose the http port yet, so that you can actually call the REST API. Just look at what the edgeHub is doing in the createOptions and replicate that for port 80 on your module https://github.com/MohanGopi2010/IotEdge-WebApi-Sample/blob/master/IOTEdge.WebAPI/deployment.template.json – silent Sep 07 '20 at 11:28
  • @silent Do we need to add tcp or http one. because this service the outsiders(outside the edge device. the consumers needs to call this api.) can communicate. Currently added tcp port at module level. "PortBindings": { "8080/tcp": { "HostPort": "80" } } Please suggest the same. – Mohan Gopi Sep 09 '20 at 09:33
  • hm maybe you cannot bind to port 80? have you tried a higher port, such das 10000? TCP is the correct one – silent Sep 09 '20 at 12:01
  • @silent I added docker files for the api edge module. I can able to deploy that module into edge device. but the web api module is failing to start. Then I checked with module logs, getting the below error. It was not possible to find any compatible framework version The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found. - No frameworks were found. I'm thinking in docker file I'm missing something. can you please help on this. – Mohan Gopi Sep 15 '20 at 11:30