构造服务器响应客户端超时,服务器在ASP.NET Web API中花费太多时间发送响应 - 超时错误...

我创建了一个在服务器上工作的Web API应用程序。在客户端,为了测试我有android应用程序。 这很简单。用户输入用户名和密码,并使用两个字符串DeviceId和DeviceName发送它们。 用户通过已定义的服务得到验证,如果一切顺利3件事情发生:服务器在ASP.NET Web API中花费太多时间发送响应 - 超时错误

用户被创建并保存在数据库中。

信息电子邮件发送给用户。

访问令牌返回给客户端。

但是,在对代码进行了一些更改之后,来自服务器的响应开始花费很长时间,并且对于移动用户超时发生得太频繁。

我已经注意到,超时开始我添加的代码一个特定部分之后发生的情况:

Regex rgx = new Regex("[^a-zA-Z0-9 -]");

string deviceId = rgx.Replace(model.DeviceId, "");

这部分是用来调整从字符串的DeviceID所有非字母数字字符。这很重要,因为在添加此代码之前,如果用户在此变量中使用斜杠或反斜杠,则会出现与JSON相关的错误。

所以我的问题是:Regex类和它的方法是否可能在服务器上造成一些混乱,并使服务器的响应时间过长?

如果是任何帮助,这是一个问题调用的代码:

[Route("registration/request")]

public async Task RegistrationRequest(Registration model)

{

try

{

MatrixLogManager.Info("Starting token creating.");

var request = HttpContext.Current.Request;

var tokenServiceUrl = request.Url.GetLeftPart(UriPartial.Authority) + request.ApplicationPath + "/Token";

MatrixLogManager.Info("Checking if model is valid.");

if (!ModelState.IsValid)

{

return Request.CreateResponse(BadRequest(ModelState));

}

using (MatrixServiceLayerLogin login = new MatrixServiceLayerLogin())

{

if (login.LoginUser(model.UserName, model.Password, true, true))

{

var personId = login.GetPersonId();

MatrixLogManager.Debug("User " + model.UserName + " successfully logged in on MatrixSTS.");

try

{

using (var authRepo = new AuthRepository())

{

MatrixLogManager.Info("Changing deviceID format.");

Regex rgx = new Regex("[^a-zA-Z0-9 -]");

model.DeviceId = rgx.Replace(model.DeviceId, "");

MatrixLogManager.Debug(model.DeviceId);

MatrixLogManager.Debug("Saving user: " + model.DeviceId);

ApplicationUser appUser = new UserFactory().CreateApplicationUser(model, personId);

IdentityResult result = await authRepo.RegisterUser(appUser);

EMailService.SendEmail(appUser);

IHttpActionResult errorResult = GetErrorResult(result);

MatrixLogManager.Debug("Saved user: " + model.DeviceId);

if (errorResult != null)

{

return Request.CreateResponse(errorResult);

}

using (var client = new HttpClient())

{

var requestParams = new List>

{

new KeyValuePair("grant_type", "password"),

new KeyValuePair("username", appUser.UserName),

new KeyValuePair("password", "0000")

};

var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams);

var tokenServiceResponse = await client.PostAsync(tokenServiceUrl, requestParamsFormUrlEncoded);

var responseString = await tokenServiceResponse.Content.ReadAsStringAsync();

var responseCode = tokenServiceResponse.StatusCode;

var responseMsg = new HttpResponseMessage(responseCode)

{

Content = new StringContent(responseString, Encoding.UTF8, "application/json")

};

responseMsg.Headers.Add("PSK", appUser.PSK);

return responseMsg;

}

}

}

catch (Exception ex)

{

MatrixLogManager.Error("Error: ", ex);

throw ex;

}

}

else

{

return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid username or password.");

}

}

}

catch (Exception ex)

{

MatrixLogManager.Error(string.Format("Error while trying registring user: Exception = {0} InnerException {1}", ex.Message, ex.InnerException.Message));

throw;

}

}

附:还有一件事。当我在我的机器上测试这些代码时,本地响应时间并不长,并且一切顺利。只有当这个代码发布到服务器时,问题才会发生,并且时间会发生。

2015-04-23

nemo_87

+0

我认为正则表达式是不应该责怪,除非你有每秒1000坪。最好的做法(嗯,我的最佳做法)是使用'RegexOptions.Compiled'选项集作为私有/公共静态只读字段。尝试,如果问题仍然存在,这肯定不是正则表达式问题。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值