The program crashed when it reached the line `metadata.ConfirmInstallAsync()`, and the error message is shown as in the picture.

Yaguang Ruan 20 Reputation points
2025-03-14T02:01:26.78+00:00
 winrt::fire_and_forget ESimModule::downloadProfile(std::string activationCode, ReactPromise<bool> promise) noexcept
    {

        co_await winrt::resume_background();

        try
        {
            OutputDebugStringA(("downloadProfile: Start with code: " + activationCode + "\n").c_str());
            auto esim = HandleWatcherAdded();
            if (!esim)
            {
                OutputDebugStringA("downloadProfile: No eSIM found\n");
                promise.Reject(ReactError{"No eSIM found"});
                co_return;
            }

            try
            {
                if (activationCode.empty())
                {
                    OutputDebugStringA("downloadProfile: Empty activation code\n");
                    promise.Reject(ReactError{"Activation code cannot be empty"});
                    co_return;
                }

                auto downloadOperation = co_await esim.DownloadProfileMetadataAsync(to_hstring(activationCode));
                if (!downloadOperation)
                {
                    OutputDebugStringA("downloadProfile: Download operation failed\n");
                    promise.Reject(ReactError{"Download operation failed"});
                    co_return;
                }

                auto downloadResult = downloadOperation.Result();
                if (!downloadResult)
                {
                    OutputDebugStringA("downloadProfile: Download result is null\n");
                    promise.Reject(ReactError{"Download result is null"});
                    co_return;
                }

                auto result = downloadResult.Status();
                OutputDebugStringA(("Download Status: " + std::to_string(static_cast<int>(result)) + "\n").c_str());

                if (result != ESimOperationStatus::Success)
                {
                    OutputDebugStringA(("downloadProfile: Download failed with status: " +
                                        ESimOperationStatusToString(result) + "\n")
                                           .c_str());
                    promise.Resolve(false);
                    co_return;
                }

                OutputDebugStringA("Download Profile: success\n");

                auto profileMetadata = downloadOperation.ProfileMetadata();
                if (profileMetadata == nullptr)
                {
                    OutputDebugStringA("Failed to get ProfileMetadata\n");
                    promise.Reject(ReactError{"Failed to get profile metadata"});
                    co_return;
                }

                // 尝试安装配置文件
                try
                {
                    OutputDebugStringA("Attempting to install profile\n");

                    // 保存一个强引用
                    auto metadata = profileMetadata;

                    // 检查 metadata 的状态
                    if (metadata.State() != ESimProfileMetadataState::WaitingForInstall)
                    {
                        OutputDebugStringA("Profile metadata is not in an installable state\n");
                        promise.Reject(ReactError{"Profile metadata is not in an installable state"});
                        co_return;
                    }

                    // 使用强引用执行异步操作
                    auto installOperation = metadata.ConfirmInstallAsync();
                    auto installResult = co_await installOperation;

                    if (!installResult)
                    {
                        OutputDebugStringA("Install result is null\n");
                        promise.Reject(ReactError{"Install result is null"});
                        co_return;
                    }

                    if (installResult.Status() == ESimOperationStatus::Success)
                    {
                        OutputDebugStringA("Install Profile: success\n");
                        promise.Resolve(true);
                    }
                    else
                    {
                        OutputDebugStringA(("Install failed with status: " +
                                            ESimOperationStatusToString(installResult.Status()) + "\n")
                                               .c_str());
                        promise.Resolve(false);
                    }
                }
                catch (winrt::hresult_error const &ex)
                {
                    OutputDebugStringA(("Install exception: " + to_string(ex.message()) + "\n").c_str());
                    promise.Reject(ReactError{"Install failed: " + to_string(ex.message())});
                }
            }
            catch (winrt::hresult_error const &ex)
            {
                OutputDebugStringA(("Download operation exception: " + to_string(ex.message()) + "\n").c_str());
                promise.Reject(ReactError{"Download operation failed: " + to_string(ex.message())});
            }
        }
        catch (...)
        {
            OutputDebugStringA("downloadProfile: Unknown error\n");
            promise.Reject(ReactError{"Unknown error during profile download"});
        }
    }

I'm developing a UWP application that uses the ESIM object. The program can install, download and use the ESIM Profile. However, when the metadata.ConfirmInstallAsync() is executed for the first time, there is no problem. But when the same code and method are run again in the same logic of my code, the program crashes.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
839 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.