为什么我无法从使用asp.net应用程序托管的SVC服务中抛出任何异常?

时间:2014-09-11 14:20:34

标签: c# asp.net .net webforms svc

我在使用IIS 7.5托管的Asp.net中运行SVC服务,当发生异常时,它不会抛出异常并继续执行下一行!

我特别在未满足条件或例如未找到数据时抛出如下错误。

throw new Exception("Data Not Found");

我的代码如下

  using System;

  public class DataProvider : IDataProvider
  {
    private AccountData _accountData;
    private OwnerData _ownerData;

  public DataProvider()
  {
    var dataAccessor = new DataAccessor();

    _accountData = dataAccessor.AccountDetails();

    //throw the error in constructor 
    if (_accountData == null)
        throw new Exception("Data Not Found");

    if (_accountData.AccountInfo != null)
    {
        _ownerData = _accountData.AccountInfo.OwnerData;
        //continue....
    }
  }

  public string GetAccountOwner()
  {
    if (_ownerData != null)
    {
        return _ownerData.Name;
    }

    return null;
  }
}

主要方法:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class AccountService : IAccountService
{
  private readonly IDataProvider _dataProvider;

 public AccountService()
    : this(new DataProvider())
 {
 }

 public AccountService(IDataProvider dataProvider)
    {
        _dataProvider;= dataProvider;
    }

 public AccountResponse GetOwner()
 {
    try
    {
        var owner = _dataProvider.GetAccountOwner();

        return new AccountResponse
        {
            Owner = owner
        };
    }
    catch (Exception ex)
    {
        if (ex.Message == "Data Not Found")
        {
            //do something here...
        }

        return null;
    }
}

}

0 个答案:

没有答案