具有下游设备的物联网Edge Gateway(IoT Edge Gateway with downstream device)

我已经创建了一个在Docker中运行的Azure IoT边缘网关,并带有一个自定义模块,它只是一个传递(接收消息并向上游发送)。

我创建了一个简单的.net控制台应用程序向网关发送消息,以便对其进行评估。

这是我在控制台应用程序中的代码。

DeviceClient client = DeviceClient.CreateFromConnectionString("HostName=<my iot hub in azure>.azure-devices.net;DeviceId=<the name of the device>;SharedAccessKey=<my access key>;GatewayHostName=<the name of the IoT Edge Device>"); Message message = new Message(); message.Properties.Add("testproperty", "test"); client.SendEventAsync(message).Wait();

看起来,当调用SendEventAsync方法时,它会挂起forerver。 我等了5分钟,应用程序停滞不前。 如果我从连接字符串中删除GatewayHostName,它会立即执行,并且我的消息直接发送到Azure中的IoT Hub。

为什么它不会将消息发送到物联网边缘网关?

I have created an Azure IoT edge gateway running in Docker with a custom module that is just a pass through (takes the message and sends it upstream).

I created a simple .net console application to send a message to the gateway so that it can be evaluated.

This is the code that I have in the console app.

DeviceClient client = DeviceClient.CreateFromConnectionString("HostName=<my iot hub in azure>.azure-devices.net;DeviceId=<the name of the device>;SharedAccessKey=<my access key>;GatewayHostName=<the name of the IoT Edge Device>"); Message message = new Message(); message.Properties.Add("testproperty", "test"); client.SendEventAsync(message).Wait();

It appears that when the SendEventAsync method is called that it will hang forerver. I waited 5 minutes and the app is stuck waiting. If I remove the GatewayHostName from the connection string it executes immediately and my message is sent directly to the IoT Hub in Azure.

Why will it not send the message to the IoT Edge gateway?

最满意答案

client.SendEventAsync调用隐式地尝试打开到服务器的连接(在这种情况下是IoT Edge网关),但无法完成,这就是为什么您看到呼叫挂起的原因。

我觉得问题出在你的连接字符串中 - “HostName = .azure-devices.net; DeviceId =; SharedAccessKey =; GatewayHostName =

这里的GatewayHostName不是Edge设备的名称 ,而是运行模块的物理设备的主机名 (fqdn hostname,如果可用)。 所以连接字符串应该是这样的 - “HostName = .azure-devices.net; DeviceId =; SharedAccessKey =; GatewayHostName =

如果您的代码在自定义模块中执行,那么您只需使用环境变量$ EdgeHubConnectionString,它应该包含您可以使用的连接字符串。

如果它是下游设备,那么除了使用正确的连接字符串之外,还需要确保设备信任用于接受网关设备连接的证书。 你可以在这里找到更多信息 - https://docs.microsoft.com/en-us/azure/iot-edge/how-to-create-transparent-gateway

The client.SendEventAsync call implicitly tries to open a connection to the server (in this case the IoT Edge gateway), which is not able to complete and that is why you are seeing the call hang.

And I feel the problem is in your connection string - "HostName=.azure-devices.net;DeviceId=;SharedAccessKey=;GatewayHostName="

Here the GatewayHostName is not the name of the Edge device, but instead the hostname of the physical device on which the modules are running (fqdn hostname, if available). So the connection string should be something like this - "HostName=.azure-devices.net;DeviceId=;SharedAccessKey=;GatewayHostName="

If your code is executing in your custom module, then you can simply use the the environment variable $EdgeHubConnectionString, which should contain the connection string you can use.

If it is a downstream device, then along with using the right connection string, you also need to make sure the device trusts the certificate used to accept the connection by the gateway device. You can find more info on that here - https://docs.microsoft.com/en-us/azure/iot-edge/how-to-create-transparent-gateway

具有下游设备的物联网Edge Gateway(IoT Edge Gateway with downstream device)

我已经创建了一个在Docker中运行的Azure IoT边缘网关,并带有一个自定义模块,它只是一个传递(接收消息并向上游发送)。

我创建了一个简单的.net控制台应用程序向网关发送消息,以便对其进行评估。

这是我在控制台应用程序中的代码。

DeviceClient client = DeviceClient.CreateFromConnectionString("HostName=<my iot hub in azure>.azure-devices.net;DeviceId=<the name of the device>;SharedAccessKey=<my access key>;GatewayHostName=<the name of the IoT Edge Device>"); Message message = new Message(); message.Properties.Add("testproperty", "test"); client.SendEventAsync(message).Wait();

看起来,当调用SendEventAsync方法时,它会挂起forerver。 我等了5分钟,应用程序停滞不前。 如果我从连接字符串中删除GatewayHostName,它会立即执行,并且我的消息直接发送到Azure中的IoT Hub。

为什么它不会将消息发送到物联网边缘网关?

I have created an Azure IoT edge gateway running in Docker with a custom module that is just a pass through (takes the message and sends it upstream).

I created a simple .net console application to send a message to the gateway so that it can be evaluated.

This is the code that I have in the console app.

DeviceClient client = DeviceClient.CreateFromConnectionString("HostName=<my iot hub in azure>.azure-devices.net;DeviceId=<the name of the device>;SharedAccessKey=<my access key>;GatewayHostName=<the name of the IoT Edge Device>"); Message message = new Message(); message.Properties.Add("testproperty", "test"); client.SendEventAsync(message).Wait();

It appears that when the SendEventAsync method is called that it will hang forerver. I waited 5 minutes and the app is stuck waiting. If I remove the GatewayHostName from the connection string it executes immediately and my message is sent directly to the IoT Hub in Azure.

Why will it not send the message to the IoT Edge gateway?

最满意答案

client.SendEventAsync调用隐式地尝试打开到服务器的连接(在这种情况下是IoT Edge网关),但无法完成,这就是为什么您看到呼叫挂起的原因。

我觉得问题出在你的连接字符串中 - “HostName = .azure-devices.net; DeviceId =; SharedAccessKey =; GatewayHostName =

这里的GatewayHostName不是Edge设备的名称 ,而是运行模块的物理设备的主机名 (fqdn hostname,如果可用)。 所以连接字符串应该是这样的 - “HostName = .azure-devices.net; DeviceId =; SharedAccessKey =; GatewayHostName =

如果您的代码在自定义模块中执行,那么您只需使用环境变量$ EdgeHubConnectionString,它应该包含您可以使用的连接字符串。

如果它是下游设备,那么除了使用正确的连接字符串之外,还需要确保设备信任用于接受网关设备连接的证书。 你可以在这里找到更多信息 - https://docs.microsoft.com/en-us/azure/iot-edge/how-to-create-transparent-gateway

The client.SendEventAsync call implicitly tries to open a connection to the server (in this case the IoT Edge gateway), which is not able to complete and that is why you are seeing the call hang.

And I feel the problem is in your connection string - "HostName=.azure-devices.net;DeviceId=;SharedAccessKey=;GatewayHostName="

Here the GatewayHostName is not the name of the Edge device, but instead the hostname of the physical device on which the modules are running (fqdn hostname, if available). So the connection string should be something like this - "HostName=.azure-devices.net;DeviceId=;SharedAccessKey=;GatewayHostName="

If your code is executing in your custom module, then you can simply use the the environment variable $EdgeHubConnectionString, which should contain the connection string you can use.

If it is a downstream device, then along with using the right connection string, you also need to make sure the device trusts the certificate used to accept the connection by the gateway device. You can find more info on that here - https://docs.microsoft.com/en-us/azure/iot-edge/how-to-create-transparent-gateway