我有一个函数,检查并验证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;
        }
    }

最佳答案

您可以创建一个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;
}

关于c# - 网站无法正常运作如果功能无法正常运作,则会发生错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46047618/

10-09 13:51