我创建了一个在服务器上运行的 Web API 应用程序。在客户端,为了测试我有 android 应用程序。这很简单。用户输入用户名和密码,然后将它们与另外两个字符串 DeviceId 和 DeviceName 一起发送。用户通过已定义的服务进行验证,如果一切顺利,会发生 3 件事:
- 用户被创建并保存在数据库中。
- 信息电子邮件被发送给用户。
- 访问令牌返回给客户端。
但是在对我的代码进行了一些更改之后,来自服务器的响应开始花费太长时间,并且对于移动用户来说,超时发生得太频繁了。
我注意到在我添加了一个特定的代码部分后超时开始发生:
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
string deviceId = rgx.Replace(model.DeviceId, "");
此部分用于从 DeviceID 字符串中修剪所有非字母数字字符。这很重要,因为在添加此代码之前,如果用户在此变量中有斜杠或反斜杠,我会收到与 JSON 相关的错误。
所以我的问题是:Regex 类及其方法是否有可能在服务器上造成一些混乱并使服务器的响应时间过长?
如果有任何帮助,这是有问题的调用的代码:
[Route("registration/request")]
public async Task<HttpResponseMessage> 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<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", appUser.UserName),
new KeyValuePair<string, string>("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;
}
}
PS还有一件事。当我在我的机器上测试这段代码时,本地响应时间并没有那么长,一切都很顺利。问题只是当此代码发布到服务器时,它会不时发生。