2015-04-23 89 views
0

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

  1. 用户被创建并保存在数据库中。
  2. 信息电子邮件发送给用户。
  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; 
    } 
} 

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

+0

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

回答

0

我在这里看到了两个问题:

Regex rgx = new Regex("[^a-zA-Z0-9 -]"); 
model.DeviceId = rgx.Replace(model.DeviceId, ""); 

首先,通过使用正则表达式构造你强制系统创建一个新的正则表达式对象每次申请它的时间。如果您使用其中一种静态方法,系统会在您第一次使用时自动缓存Regex对象。或者你可以提前构造Regex并将其存储在一个静态变量中。

其次,你一次只替换一个不需要的字符,这是非常低效的。将+添加到您的正则表达式的末尾,以便一次匹配不需要的字符的整个序列。

model.DeviceId = Regex.Replace(model.DeviceId, "[^a-zA-Z0-9 -]+", ""); 
相关问题