Asp.net核心模块未连接到Azure IOT Edge Hub

时间:2018-02-01 17:09:35

标签: asp.net-core-2.0 azure-iot-edge

我正在尝试将ASP.NET Core边缘模块连接到边缘运行时集线器(本地),但它没有连接并且因CONNECT失败而失败:RefusedNotAuthorized异常。我有标准的.net核心模块连接到边缘集线器并发布消息,但ASP.NET核心边缘模块没有。 .net核心和asp.net核心边缘模块都是从Azure IOT Edge门户推送出来的。

  /// <summary>
    /// Initializes the DeviceClient and sets up the callback to receive
    /// messages containing temperature information
    /// </summary>
    static async Task Init(string connectionString, bool bypassCertVerification = false)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " Connection String {0}", connectionString);

        MqttTransportSettings mqttSetting = new MqttTransportSettings(Microsoft.Azure.Devices.Client.TransportType.Mqtt_Tcp_Only);
        // During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production
        if (bypassCertVerification)
        {
            mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
        }
        ITransportSettings[] settings = { mqttSetting };

        try
        {
            // Open a connection to the Edge runtime
            DeviceClient ioTHubModuleClient = DeviceClient.CreateFromConnectionString(connectionString, settings);
            await ioTHubModuleClient.OpenAsync();

            Console.WriteLine(DateTime.Now.ToLongTimeString() + " IoT Hub module client initialized.");
        }
        catch(Exception ex)
        {
            Console.WriteLine(DateTime.Now.ToLongTimeString() + ex.Message);
        }
    }

2 个答案:

答案 0 :(得分:0)

我已经使用您提供的代码测试了这个问题,它可以工作。我认为您需要检查设备的连接字符串。 如果使用不正确的连接字符串连接设备客户端,则会发生错误cmTemplate: { editoptions: { dataEvents: [ { type: "click", fn: function () { $(this).select(); } } ] } } 。您可以从Azure门户(IoT Edge-&gt; - &gt;连接字符串主键)复制连接字符串。

答案 1 :(得分:0)

目前,模块需要两种授权。一个是连接字符串,它将使模块能够连接到IoTHub,但是我们在edgeHub上有一个服务器证书来建立与edgeHub的连接。该证书通过文件系统和建立文件路径的环境变量传递给模块。

您的模块中是否有“InstallCert()”函数,是否正在调用?

        static void InstallCert()
    {
        string certPath = Environment.GetEnvironmentVariable("EdgeModuleCACertificateFile");
        if (string.IsNullOrWhiteSpace(certPath))
        {
            // We cannot proceed further without a proper cert file
            Console.WriteLine($"Missing path to certificate collection file: {certPath}");
            throw new InvalidOperationException("Missing path to certificate file.");
        }
        else if (!File.Exists(certPath))
        {
            // We cannot proceed further without a proper cert file
            Console.WriteLine($"Missing path to certificate collection file: {certPath}");
            throw new InvalidOperationException("Missing certificate file.");
        }
        X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadWrite);
        store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(certPath)));
        Console.WriteLine("Added Cert: " + certPath);
        store.Close();
    }