Не удается перехватить исключение DeviceAlreadyExistsException в C# приложении UWP при создании устройства в Azure IOT - PullRequest
0 голосов
/ 11 февраля 2020

Эта программа не может перехватить DeviceAlreadyExistsException.

Пожалуйста, дайте мне знать, почему?

namespace azure_iot_hub_connectivity_through_uwp{
  public sealed partial class MainPage : Page
  {
    private static RegistryManager registryManager;
    private static string connectionString = "";
    private static int rescode = -1;
    private static string devkey = "";
    static Device device;
    public MainPage()
    {
        this.InitializeComponent();
    }
    private async static Task AddDevice(string s)
    {
        string deviceId = s;
        //Device device;
        try
        {
            rescode = 1;
            device = await registryManager.AddDeviceAsync(new Device(deviceId));
        }
        catch (DeviceAlreadyExistsException)
        {
            rescode = 0;
            device = await registryManager.GetDeviceAsync(deviceId);
        }

        //devkey = device.Authentication.SymmetricKey.PrimaryKey;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        connectionString = cs.Text;
        if (dev.Text == "")
            msg.Text = "Enter Device ID";
        else
        {
            registryManager = RegistryManager.CreateFromConnectionString(connectionString);
            AddDevice(dev.Text.ToString()).Wait(3000);
            //Console.ReadKey();
        }

        if (rescode == 1)
            msg.Text = "Device Created ";
        else if (rescode == 0)
            msg.Text = "Device Already Exist\nDevice Key: " + devkey;
    }
  }}

Однако Журналы, показывающие, что выброшено исключение: -

Вышло исключение : «Microsoft. Azure .Devices.Common.Exceptions.DeviceAlreadyExistsException» в Microsoft. Azure .Devices.dll Возникло исключение: «Microsoft. Azure .Devices.Common.Exceptions.DeviceAlreadyExistsException» в System.Private.CoreLib Исключение .dll: «Microsoft. Azure .Devices.Common.Exceptions.DeviceAlreadyExistsException» в System.Private.CoreLib.dll Исключение: «Microsoft. Azure .Devices.Common.Exceptions.DeviceAlreadyExistsException» в System.Private .CoreLib.dll

Пожалуйста, помогите мне.

...