简体   繁体   中英

c# calling function parameters

I have developed a method (GetDServices) of a webservice , which calls a function (GetResource) that returns a string result.

In the first call to the function, the value of the Culture parameter is 'ca-ES', and retrieves it perfectly.

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.

I do not understand why this happens. I have debugged and the value of the method call arrives correctly.

If i restart, first time run well.

I edit this code, and now post full code.

After test, i tryed to assign new vars (test and test2) and i saw that when i call the method since assignation (test & test2) running well, but when i call method from array assignation, in the second call remain first value.

My method:

  [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

My function:

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

Unless you can post complete code of the GetDServices method body, (assuming the original value of the culture coming to the method is correct) check if you have any re-assignments of the culture variable inside it before calling GetResource ie the code of the following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM