I am getting error CS0119: 'ServiceClient' is a type, which is not valid in the given context in my project . In my project I have a web reference and in that I'm getting this error . The details are below. Please help guys as I don't understand how to solve this .
CODE
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" };
}
In Referrence.cs file
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;
}
}
I am getting error in this particular line ServiceClient _client = new ServiceClient (); . Any solution for this error or any clue ? In method AuthenticatedUser ResolveUser(string Id) line var temp = ServiceClient .getProfileUser(Id, Config.ProfileAppName) ServiceClient throws error like threw an exception of type 'System.TypeInitializationException' . InnerException {"Object reference not set to an instance of an object."}
SOLUTION Okay I found my solution . I have to change this line
var temp = ServiceClient .getProfileUser(Id, Config.ProfileAppName);
to this
_client = new ServiceClient();
var temp = _client.getProfileUser(Id, Config.ProfileAppName);