This project contains the Edge Hub.
Sequence | Description | Code reference |
---|---|---|
1 | Edge hub service starts. | Program.Main |
2 | Load configuration from application json file (appsettings_hub.json) and environment variables. | Program.Main |
3 | Load certificates for Edge hub and install certificate chain if available. | |
4 | Initialize web hosting (ASP.Net Core) and build dependency injection container. | Hosting , IModule.Load |
5 | Instantiate each protocol including amqp, mqtt and http if it is enabled. | IProtocolHead |
6 | Start enabled protocol(s) asynchronously. Each protocol will start a listener for incoming requests. | IProtocolHead.StartAsync |
- Create an IOT edgeHub module connection string. Create a Visual studio project following process here: https://docs.microsoft.com/azure/iot-hub/iot-hub-portal-csharp-module-twin-getstarted Section Update the module twin using .NET device SDK but replace the program by the snippet below:
using System;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common.Exceptions;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
const string connectionString = "Put iothubowner connection string here";
const string deviceID = "Put IOT edge device ID here";
static void Main(string[] args)
{
AddModuleAsync().Wait();
}
private static async Task AddModuleAsync()
{
RegistryManager registryManager =
RegistryManager.CreateFromConnectionString(connectionString);
Module parentEdgeHub;
try
{
parentEdgeHub = await registryManager.GetModuleAsync(deviceID, "$edgeHub");
parentEdgeHub.Authentication.Type = AuthenticationType.Sas;
parentEdgeHub = await registryManager.UpdateModuleAsync(parentEdgeHub);
}
catch (ModuleAlreadyExistsException)
{
parentEdgeHub = await registryManager.GetModuleAsync(deviceID, "$edgeHub");
}
Console.WriteLine("Generated module key: {0}", parentEdgeHub.Authentication.SymmetricKey.PrimaryKey);
}
}
}
That snippet should create connection string for the module $edgeAgent.
- Set environment variable
EdgeModuleHubServerCertificateFile
to the path of a SSL Certificate file (e.g. C:\edgeDevice.pfx); for debug purpose, you can create and use a self-signed certificate. Remember to restart Visual Studio to take effect.EdgeModuleHubServerCAChainCertificateFile
to the path of the CA. If certificate is self signed then is it the same as above; OrEdgeHubDevServerCertificateFile
to the path of a SSL Certificate file (e.g. C:\edgeDevice.pfx); for debug purpose, you can create and use a self-signed certificate. Remember to restart Visual Studio to take effect.EdgeHubDevTrustBundleFile
to the path of the CA. If certificate is self signed then is it the same as above;EdgeHubDevServerPrivateKeyFile
to the path of the private key (*.key file)
- Update following values in appsettings_hub.json in Microsoft.Azure.Devices.Edge.Hub.Service project.
IotHubConnectionString
- Copy the $edgeHub module connection string. It should have been created in step 1.configSource
- Set it astwin
to read confirm from twin, orlocal
to read Edge Hub config from configuration file.
- Set Microsoft.Azure.Devices.Edge.Hub.Service as startup project in Visual Studio.
- Make sure to rebuild the solution.
- You can start debugging Edge Hub by hitting F5 in Visual Studio.
If you want to run a leaf device connecting to Edge hub using Visual Studio, then continue to read below:
- You can either use samples in Azure IoT C# SDK or write your own leaf device application using DeviceClient in Azure IoT C# SDK. Make sure to note the hostname you write down in the certificate. If that hostname is not an IP address please follow step 3b too.
- This example uses the direct method sample in Azure IoT C# SDK, you can find it under iothub\device\samples\DeviceClientMethodSample.
- Open that sample application in a new instance of Visual Studio, update connection string and add
GatewayHostName="Host name you have put in the certificate
to it in order to connect to Edge hub running locally using Visual Studio. 3b. If the host name is not IP address you will have to alias the host name to the local ip address: 127.0.0.1; In Windows edit with administrator rights c:\windows\system32\drivers\etc\hosts and add the line: "127.0.0.1 your hostname" - Make sure Edge hub is running first, then you can start sample application by hit F5 in Visual Studio.
- Look at Edge hub console window, you will see logs about connecting to a new device.
- You can invoke direct method call from Azure IoT Portal or from Device Explorer.
Method name
: WriteToConsoleMethod payload
: Any valid json
- You should see your method payload printed out in sample application console window.