【问题标题】:c# calling function parametersc#调用函数参数
【发布时间】:2017-07-31 13:55:02
【问题描述】:

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

第一次调用该函数时,Culture参数的值为'ca-ES',完美检索。

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

我不明白为什么会这样。 我已经调试过了,方法调用的值正确到达了。

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

我编辑了这段代码,现在发布完整的代码。

测试后,我尝试分配新的变量(test 和 test2),我看到当我调用该方法时,因为分配(test 和 test2)运行良好,但是当我从数组分配中调用方法时,在第二次调用中仍然存在第一个值。

我的方法:

  [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;
  }

【问题讨论】:

  • 你试过调试吗?在(重新)分配 es-ES 之前,Culture 的值是多少?
  • Culture.IsNullOrEmpty()??那是扩展名吗?
  • @Fredou,我相信是string.IsNullOrEmpty(culture)
  • @Fredou,我可能是?哦,真的……见msdn.microsoft.com/en-us/library/…
  • The second time I call the method, the function receives the parameter with the value 'en-US', but the value of the first call 'ca-ES' remains.我不清楚这句话是什么意思。

标签: c# function web-services parameter-passing


【解决方案1】:

除非您可以发布GetDServices 方法体的完整代码,(假设传入该方法的culture 的原始值是正确的)检查您是否有任何culture 的重新分配变量 在调用GetResource 之前在它里面,即下面的代码

// Inicialization
 ...
// Login validation
 ...
// End Login Validation
 ...

【讨论】:

  • 标有...的内容无关紧要,所以没有写。调试我看到变量“culture”在方法调用中作为参数接收,值为“ca-ES”。刚输入的函数值是“ca-ES”,但是在第二次调用函数时,参数的值,并没有随着调用的值更新。我会继续测试。我对函数是否定义明确表示怀疑。
  • 其中一种检查方法是将 Fiddler 配置为您的代理,并查看负载中 culture 的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 2012-06-02
相关资源
最近更新 更多