站点不工作如果该功能不起作用,则会出错

时间:2017-09-05 05:34:32

标签: c# asp.net web-services error-handling

我有一个检查并验证 User的功能,并在此基础上将数据显示给相应的用户。函数名称为Get_AuthenticateUser_Ums(strUserName);

我在Page_load上调用此函数。此函数包含web service。现在我想要的是每当服务不工作或有问题时,我希望不应该向用户显示该网站,并且消息应该提示为The service is down, so couldnt load the site.

以下是我的代码

if (!IsPostBack)
            {
                Get_AuthenticateUser_Ums(strUserName); }

功能

private void Get_AuthenticateUser_Ums(string strUserName)
    {
        try
        {
            strReturnMessage = string.Empty;

            Boolean bolReturn = ObjUMS.AuthenticateApplicationAccess(strUserName, strAppUrl, out strReturnMessage);

            if (bolReturn)
            {
                DataSet dsUserGroups = new DataSet();
                dsUserGroups = ObjUMS.GetUserAppDetailsbyUserNameApplicationUrl(strUserName, strAppUrl, out strReturnMessage);

                if (dsUserGroups.Tables[1] != null && dsUserGroups.Tables[1].Rows.Count > 0)
                {
                    string strSubGroupName = dsUserGroups.Tables[1].Rows[0]["SUBGROUP_NAME"].ToString();

                    if (strSubGroupName == "UBR Requester")
                    {
                        if (dsUserGroups.Tables[2] != null && dsUserGroups.Tables[2].Rows.Count > 0)
                        {
                            string[] allStates = dsUserGroups.Tables[2].AsEnumerable().Select(r => r.Field<string>("BOUNDARY_VALUE")).ToArray();
                            ViewState["States"] = string.Join(",", allStates);
                        }
                    }
                    else
                    {
                        Response.Redirect("~/NotAuthorize.aspx", false);
                    }
                }
                else
                {
                    Response.Redirect("~/NotAuthorize.aspx", false);
                }
            }
            else
            {
                Response.Redirect("~/NotAuthorize.aspx", false);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用Method创建url to svc来检查连接,并根据您可以查看服务是否已启动的boolean

public bool checkConnection(){ 
var url = "http://nvmbd1bkh150v02/UMSService/UserProvider.svc"; 
bool tosend = false; 
try 
{ 
var myRequest = (HttpWebRequest)WebRequest.Create(url); 

var response = (HttpWebResponse)myRequest.GetResponse(); 

if (response.StatusCode == HttpStatusCode.OK) 
{ 
tosend = true ; 
// it's at least in some way responsive 
// but may be internally broken 
// as you could find out if you called one of the methods for real 
Debug.Write(string.Format("{0} Available", url)); 
} 
else 
{ 
tosend = false; 
// well, at least it returned... 
Debug.Write(string.Format("{0} Returned, but with status: {1}", 
url, response.StatusDescription)); 
} 
} 
catch (Exception ex) 
{ 
// not available at all, for some reason 
Debug.Write(string.Format("{0} unavailable: {1}", url, ex.Message)); 
} 

return tosend; 
}