【问题标题】:error CS0119: 'ServiceClient' is a type, which is not valid in the given context [closed]错误 CS0119:“ServiceClient”是一种类型,在给定的上下文中无效 [关闭]
【发布时间】:2020-08-05 07:20:07
【问题描述】:

我收到错误 CS0119:'ServiceClient' 是一种类型,在我的项目中的给定上下文中无效。在我的项目中,我有一个网络参考,并且我收到了这个错误。详情如下。请帮助大家,因为我不明白如何解决这个问题。

代码

    private ServiceClient _client;
    private ServiceClient ProfileServiceClient
    {
        get
        {
            if (_client == null)
            {
                //Log.Trace("Initiating Profile client");

                var watch = Stopwatch.StartNew();
                ServiceClient _client = new ServiceClient ();
                watch.Stop();

                Log.Debug("Initiating Profile took " + watch.ElapsedMilliseconds + " miliseconds");
            }
            return _client;
         }
      }


      public AuthenticatedUser ResolveUser(string Id)
    {
        try
        {
            //Log.BusinessTask(string.Format("Trying to resolve '{0}' via Profile", vcnId));
            var temp = ServiceClient .getProfileUser(Id, Config.ProfileAppName);

            if (temp == null)
            {
                Log.BusinessTask(string.Format("No user found in profile with '{0}' as  ID", Id));
                var dummyUser = new AuthenticatedUser()
                {
                    Id = Id,
                    Email = string.Empty,
                    FirstName = Id,
                    LastName = string.Empty
                };

                dummyUser.Roles.Add("Viewer");

                Log.BusinessTask("Closing Profile client connection");
                ServiceClient .Dispose();

                return dummyUser;
            }

            Log.BusinessTask("Profile user found, mapping properties");
            var result = new AuthenticatedUser
            {
                Id = temp.userNr,
                Email = temp.email,
                FirstName = temp.firstName,
                LastName = temp.lastName
            };

            result.Roles.Add("Viewer");

            Log.BusinessTask("Profile user found, mapping roles and applications");
            var animatrix = ServiceClient .getTheAnimatrix(Id, Config.ProfileAppName, 0, null, null);

            if (animatrix != null)
            {
                foreach (var matrix in animatrix)
                {
                    if (matrix.authorisations != null)
                    {
                        foreach (var m in matrix.authorisations)
                        {
                            if (m.value.strValue == "Developer Documentation")
                            {
                                if (!result.Roles.Any(x => x.Equals("Developer", StringComparison.InvariantCultureIgnoreCase)))
                                    result.Roles.Add("Developer");
                            }

                            if (m.value.strValue == "IsIt Documentation")
                            {
                                if (!result.Roles.Any(x => x.Equals("IsIt", StringComparison.InvariantCultureIgnoreCase)))
                                    result.Roles.Add("IsIt");
                            }

                            if (m.characteristic == "Managed Application")
                            {
                                if (matrix.role.roleName.Contains("DEVELOPER"))
                                    result.Applications.Add("Developer" + "/" + m.value.strValue);
                                else
                                    result.Applications.Add("IsIt" + "/" + m.value.strValue);
                            }
                        }
                    }
                }

            }

            Log.BusinessTask("Closing Profile client connection");
            ServiceClient .Dispose();

            Log.BusinessTask("Returning User object");
            return result;
        }
        catch (Exception e)
        {
            Log.Error(string.Format("Couldn't reach the PROFILE auth webservice : {0}", e));
        }

        return new AuthenticatedUser { FirstName = "Viewer", Id = "none" };
    }

在 Reference.cs 文件中

         public ServiceClient () {
          this.Url = 
          global::ProxyComponent.Properties.Settings.Default.ProxyComponent_Services_ServiceClient _ServiceClient ;
          if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
          }
           else {
            this.useDefaultCredentialsSetExplicitly = true;
          }
        }

我在这一行 ServiceClient _client = new ServiceClient (); 中遇到错误。这个错误的任何解决方案或任何线索?在方法 AuthenticatedUser ResolveUser(string Id) line var temp = ServiceClient .getProfileUser(Id, Config.ProfileAppName) ServiceClient throws error like throws an exception of type 'System.TypeInitializationException' 。 InnerException {"对象引用未设置为对象的实例。"}

解决方案 好的,我找到了我的解决方案。我必须改变这一行

   var temp = ServiceClient .getProfileUser(Id, Config.ProfileAppName);

到这里

     _client = new ServiceClient();
     var temp = _client.getProfileUser(Id, Config.ProfileAppName);

【问题讨论】:

  • 您是否缺少 using 语句?
  • 你只想要_client = new ServiceClient();,你不想要一个新的局部变量。
  • 试过这个但同样的错误
  • 这段代码没有给出编译器错误。阅读How to Ask 并创建一个minimal reproducible example

标签: c# asp.net .net web-reference


【解决方案1】:

看起来编译器将服务客户端与另一种类型混淆了。也许尝试使用带有完整命名空间的 ServiceClient?类似 FullProjectNamespace.ServiceClient _serviceClient = new...

【讨论】:

  • 这会产生不同的编译器错误:CS0029:无法将 FooNamespace.ServiceClient 类型隐式转换为 BarNamespace.ServiceClient。
  • @CodeCaster 有什么线索可以解决这个问题吗??
  • 为什么投反对票?通过您的编辑,您已经证明了我的观点并且使用了错误的类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 2020-04-16
  • 2020-07-10
  • 2018-08-09
  • 2016-03-30
  • 2017-12-06
  • 2021-01-19
相关资源
最近更新 更多