0

I had several modules running within an edge gateway previously. That gateway was hosted in a standard .net framework Windows service, and the module were just instantiated objects. The modules sent messages back and forth to each other and it was all very easy to just start the service in the debugger and do whatever I needed.

Now edge has moved to a container-centric model where you push your built containers, which contain .net core executables, to an azure container store and those get deployed to your running docker service on your target machine. I have the deployment working to where I can see the containers are running on my dev machine but I have no idea how I am supposed to debug them in Visual Studio 2017? Do I attach to process? What if I want to debug 5 modules at once to make sure all the message passing is happening as desired?

Tim
  • 412
  • 7
  • 18

2 Answers2

1

Edge Hub does not really care who is calling into it as long as the right credentials are provided. The caller could be in a Docker container, in another process on the host or a downstream device. So during development, when using Visual Studio it should be possible for you to simply F5 (launch debugger) your module project and have it connect and communicate with Edge Hub. You'd just need to make sure that you provide it the right context. For example, you may have to setup your VS debug settings so that the EdgeHubConnectionString value is setup properly.

Right now though, unfortunately, the Azure portal (or the Azure CLI) does not have support for fetching module credentials which you'd need to build your connection string to connect to Edge Hub. Support for this should be arriving soon.

In the meantime you can workaround this by doing a deployment the normal way (i.e. via "Set Modules" in the portal and having the Edge Agent deploy the container for you) and then run the following command (or something like it) from a bash terminal to print the value of the connection string:

docker inspect                                                         \
     --format='{{range $e := .Config.Env}}{{printf "%s\n" $e}}{{end}}' \
     YourContainerName |                                               \
  grep EdgeHubConnectionString |                                       \
  cut -c 25-

Replace YourContainerName in the command above with the name of your module/container.

In addition to this you'll also want to skip TLS cert validation when you connect to Edge Hub. This is because when running on your PC it is not setup to trust the server cert that Edge Hub uses. You can use the following snippet to skip cert validation in your module:

var mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);
mqttSetting.RemoteCertificateValidationCallback =
    (sender, certificate, chain, sslPolicyErrors) => true;
ITransportSettings[] settings = { mqttSetting };

DeviceClient deviceClient =
     DeviceClient.CreateFromConnectionString(connectionString, settings);

After this you should be able to debug your module from VS.

Raj
  • 2,557
  • 2
  • 19
  • 17
  • I use powershell and to help anyone coming to this thread later, I ran this command to get the environment strings Raj mentioned: docker ps | % { $_ -Replace '\s\s+',',' } | ConvertFrom-Csv | Select-Object -Property *, @{ Name="Environment"; Expression = { docker inspect $_."CONTAINER ID" | convertfrom-json | % { $hash = @{}; foreach ($line in $_.Config.Env) { $tokens = $line -split '=',2 $hash."$($tokens[0])" = $tokens[1] } [PSCustomObject]$hash } } } – Tim Jan 03 '18 at 00:06
  • If this solves the issue could you please mark this as the answer? Thanks. – Raj Jan 04 '18 at 19:48
1

There is a post talking about how to debug C# module in Visual Studio Code here: https://blogs.msdn.microsoft.com/visualstudio/2017/12/12/easily-create-iot-edge-custom-modules-with-visual-studio-code/. Please make sure you are debugging on the same machine as deployment target.

  • VS code isn't really a comparable experience to full VS 2017 and I don't use it. I will edit the question to be more specific. – Tim Jan 10 '18 at 15:15