.net 如何使用C#在引发BadRequestException后从Catch块中断

nlejzf6q  于 2023-02-17  发布在  .NET
关注(0)|答案(1)|浏览(214)

我有一个方法,需要打破后,BadRequestException()抛出。目前后,BadRequestException()抛出,前端显示加载栏缓冲无限。
请在下面找到我的代码

public FeSite GetSiteBySiteId(string envCode, string siteId)
        {
            try
            {
                envCode.ThrowIfNull();
                siteId.ThrowIfNull();

                var watch = new Stopwatch();
                watch.Start();

                FeSite result = this.ExecuteAndParseWebRequestForEnv<FeSite>(envCode, $"sites/{siteId}", HttpMethod.Get);
                this.LogService.Info($"FeeDataAcccess - GetSitebySiteId - {watch.ElapsedMilliseconds}ms");

                return result;
            }
            catch (Exception e)
            {
                this.LogService.Error($"GetSiteBySiteId - {e.Message}");
                throw new BadRequestException("Invalid siteId!");
            }
        }

以下方法调用GetSiteBySiteId()方法:

public string GetDevicesSerialNumberBySiteId(string envCode, string siteId)
        {
            var siteSerialNumber = "";
            if (siteId != null)
            {
                var result = this.feeDataAccess.GetSiteBySiteId(envCode, siteId);
                List<string> gatewaySiteSNlist = result.Gateways.Select(x => x.SerialNumber).ToList();
                foreach (var item in gatewaySiteSNlist)
                {
                    var siteSN = item;
                    siteSerialNumber += $";{siteSN};";
                }
            }

            return siteSerialNumber;
        }

我已经尝试了下面的代码:

public FeSite GetSiteBySiteId(string envCode, string siteId)
        {
            do
            {
                try
                {
                    envCode.ThrowIfNull();
                    siteId.ThrowIfNull();

                    var watch = new Stopwatch();
                    watch.Start();

                    FeSite result = this.ExecuteAndParseWebRequestForEnv<FeSite>(envCode, $"sites/{siteId}", HttpMethod.Get);
                    this.LogService.Info($"FeeDataAcccess - GetSitebySiteId - {watch.ElapsedMilliseconds}ms");
                    return result;
                }
                catch (Exception e)
                {
                    this.LogService.Error($"GetSiteBySiteId - {e.Message}");
                    throw new BadRequestException("Invalid siteId!");
                }
            }
            while (false);
            {
                break;
            }

        }

但是我得到错误"没有可以中断或继续的封闭循环"
怎么解决这个问题?先谢谢你。

kyvafyod

kyvafyod1#

public string GetDevicesSerialNumberBySiteId(string envCode, string siteId)
    {
        var siteSerialNumber = "";
        if (siteId != null)
        {
            try{
            var result = this.feeDataAccess.GetSiteBySiteId(envCode, siteId);
            List<string> gatewaySiteSNlist = result.Gateways.Select(x => x.SerialNumber).ToList();
            foreach (var item in gatewaySiteSNlist)
            {
                var siteSN = item;
                siteSerialNumber += $";{siteSN};";
            }
            }catch{
               //BadRequestException catch and error handling goes here
            }
        }

        return siteSerialNumber;
    }

相关问题