2017-07-31 167 views
0

我开发了一个webservice的方法(GetDServices),它调用返回字符串结果的函数(GetResource)。c#调用函数参数

在函数的第一次调用中,Culture参数的值是'ca-ES',并且完美地检索它。

第二次我调用方法时,函数接收到值为'en-US'的参数,但第一个调用'ca-ES'的值仍然存在。

我不明白为什么会发生这种情况。 我已调试并且方法调用的值正确到达。

如果我重新启动,第一次运行良好。

我编辑此代码,现在发布完整的代码。 (测试和测试2),我看到,当我调用方法,因为分配(测试&测试2)运行良好,但是当我从数组分配调用方法,在第二次调用保持第一价值。

我的方法:

[WebMethod] 
    #region GetServices 
    public ResultDTO GetServices(
     LoginDTO login, 
     string enterprise, 
     string culture, 
     out List<Services> Services 
     ) 
    { 
     Bootstrapper.TryInit(); 
     LogHelper.DumpParams("WS.GetServices", login, enterprise); 

     // Inicialización de salida. 
     Services = new List<Services>(); 
     try 
     { 
      // LOGIN VALIDATION 
      User user; 
      var result = ValidationHelper.ValidateLogin(login, out user); 

      if (result != null) 
      { 
       return LogHelper.DumpResult("WS.GetServices", result); 
      } 
      // LOGIN VALIDATION END 

      // QUERY SERVICES 
      var queryservices = Bootstrapper.Context.GetRepository<Service, long>() 
       .Query().Where(x => x.EnterpriseServiceRelationVigence.Any(y => y.Enterprise.NIF == enterprise)); 

      if (queryservices == null) 
      { 
       return LogHelper.DumpResult("WS.GetServices", new ResultDTO(ErrorCodes.SERVICES_NOT_FOUND)); 
      } 

      // this vars has correct value after assignation 
      string test = culture; // <- here First time has "ca-ES" and second time has "en-US" 
      string test2 = GetResource(culture, "sample"); // <- here First time has "ca-Es" and second time has "en-US" 


      Services = queryservices.Select(x => new Services 
      { 
       IdService = x.Id, 
       Name = x.Name, 
       ShortDescription = GetResource(test, x.ResourceKey + "Description"), 
       Description = GetResource(test, x.ResourceKey + "Comment"), 
       ImageButton = x.ImageButton, 
       ImageButtonDisabled = x.ImageButtonDisabled, 
       ImageButtonHome = x.ImageButtonHome, 
       ColorTextoHome = x.ColorTextoHome, 
      } 
      ).ToList(); 

      return LogHelper.DumpResult("WS.GetServices", new ResultDTO()); 
     } 
     catch (Exception ex) 
     { 
      Logger.Log.Error(() => ex.ToString()); 
      return LogHelper.DumpResult("WS.GetServices", new ResultDTO(ErrorCodes.GENERIC_ERROR)); 
     } 
    } 
    #endregion 

我的功能:

static string GetResource(string Culture, string ResourceKey) 
    { 

    // When this function its called, from test2 assignation running well. 
    // but when this function its called from 
    //  ShortDescription = GetResource(test, x.ResourceKey + "Description"), 
    // or 
    //  Description = GetResource(test, x.ResourceKey + "Comment"),  
    // then remained the value of first call. 


     if (Culture.IsNullOrEmpty()) { Culture = "es-ES"; } 

     var queryservices = Bootstrapper.Context.GetRepository<Resource, string>() 
      .Query().Where(x => x.ResourceKey == ResourceKey && x.CultureKey == Culture); 

     if (!queryservices.IsNullOrEmpty()) // Usuari Generic 
     { 
      return queryservices.Select(x => x.ResourceValue).FirstOrDefault(); 
     } 

     return string.Empty; 
    } 
+0

您是否尝试过调试? '文化'在(重新)分配'ES-ES'之前有什么价值? – Aphelion

+1

'Culture.IsNullOrEmpty()'?这是一个扩展? – Rahul

+1

@Fredou,那是'string.IsNullOrEmpty(文化)'我相信? – Rahul

回答

0

除非你可以张贴GetDServices方法体的完整代码,(假设culture的原始值来该方法是正确的)检查是否有任何重新分配culture变量之前拨打GetResource,即以下代码

// Inicialization 
... 
// Login validation 
... 
// End Login Validation 
... 
+0

标有......的内容无关紧要,所以我没有写出来。 调试我已经看到变量“culture”在方法调用中作为参数接收,值为“ca-ES”。 只需输入函数值为“ca-ES”,但在第二次调用函数时,参数的值不会随调用的值更新。 我会继续测试。 我怀疑这个函数是否定义得很好。 – LordRick3dfx

+0

其中一种检查方法是将Fiddler配置为您的代理,并在有效负载中查看“culture”的值。 –