HttpClient is not getting null outside using block [duplicate]












0
















This question already has an answer here:




  • Setting an object to null vs Dispose()

    3 answers




i have a class in which there is a method wich returns http client like below.



private HttpClient client;
private HttpClient GetClient()
{
if (this.client == null)
{
this.client = new HttpClient
{
BaseAddress = new Uri("api")
};
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
return this.client;
}


i am using this GetClient method in other methods like



                using (var client = this.GetClient())
{
var response = await client.GetAsync($"/abc").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}


this client is not getting null outside the using block.next time when this method hits then client is not null.whats the problem?










share|improve this question













marked as duplicate by Camilo Terevinto c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 10:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Disposing doesn't mean that the object which will be disposed is getting null. It just frees the used ressources on the object.

    – Christoph K
    Nov 15 '18 at 9:39











  • Note that using (var client ...) creates a different clientvariable than the one declared at class scope. The client variable from the using declaration is not even defined outside the declaration.

    – Clemens
    Nov 15 '18 at 10:39


















0
















This question already has an answer here:




  • Setting an object to null vs Dispose()

    3 answers




i have a class in which there is a method wich returns http client like below.



private HttpClient client;
private HttpClient GetClient()
{
if (this.client == null)
{
this.client = new HttpClient
{
BaseAddress = new Uri("api")
};
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
return this.client;
}


i am using this GetClient method in other methods like



                using (var client = this.GetClient())
{
var response = await client.GetAsync($"/abc").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}


this client is not getting null outside the using block.next time when this method hits then client is not null.whats the problem?










share|improve this question













marked as duplicate by Camilo Terevinto c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 10:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Disposing doesn't mean that the object which will be disposed is getting null. It just frees the used ressources on the object.

    – Christoph K
    Nov 15 '18 at 9:39











  • Note that using (var client ...) creates a different clientvariable than the one declared at class scope. The client variable from the using declaration is not even defined outside the declaration.

    – Clemens
    Nov 15 '18 at 10:39
















0












0








0









This question already has an answer here:




  • Setting an object to null vs Dispose()

    3 answers




i have a class in which there is a method wich returns http client like below.



private HttpClient client;
private HttpClient GetClient()
{
if (this.client == null)
{
this.client = new HttpClient
{
BaseAddress = new Uri("api")
};
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
return this.client;
}


i am using this GetClient method in other methods like



                using (var client = this.GetClient())
{
var response = await client.GetAsync($"/abc").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}


this client is not getting null outside the using block.next time when this method hits then client is not null.whats the problem?










share|improve this question















This question already has an answer here:




  • Setting an object to null vs Dispose()

    3 answers




i have a class in which there is a method wich returns http client like below.



private HttpClient client;
private HttpClient GetClient()
{
if (this.client == null)
{
this.client = new HttpClient
{
BaseAddress = new Uri("api")
};
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
return this.client;
}


i am using this GetClient method in other methods like



                using (var client = this.GetClient())
{
var response = await client.GetAsync($"/abc").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}


this client is not getting null outside the using block.next time when this method hits then client is not null.whats the problem?





This question already has an answer here:




  • Setting an object to null vs Dispose()

    3 answers








c# asp.net .net wpf http






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 9:37









sum1sum1

235




235




marked as duplicate by Camilo Terevinto c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 10:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Camilo Terevinto c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 10:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Disposing doesn't mean that the object which will be disposed is getting null. It just frees the used ressources on the object.

    – Christoph K
    Nov 15 '18 at 9:39











  • Note that using (var client ...) creates a different clientvariable than the one declared at class scope. The client variable from the using declaration is not even defined outside the declaration.

    – Clemens
    Nov 15 '18 at 10:39





















  • Disposing doesn't mean that the object which will be disposed is getting null. It just frees the used ressources on the object.

    – Christoph K
    Nov 15 '18 at 9:39











  • Note that using (var client ...) creates a different clientvariable than the one declared at class scope. The client variable from the using declaration is not even defined outside the declaration.

    – Clemens
    Nov 15 '18 at 10:39



















Disposing doesn't mean that the object which will be disposed is getting null. It just frees the used ressources on the object.

– Christoph K
Nov 15 '18 at 9:39





Disposing doesn't mean that the object which will be disposed is getting null. It just frees the used ressources on the object.

– Christoph K
Nov 15 '18 at 9:39













Note that using (var client ...) creates a different clientvariable than the one declared at class scope. The client variable from the using declaration is not even defined outside the declaration.

– Clemens
Nov 15 '18 at 10:39







Note that using (var client ...) creates a different clientvariable than the one declared at class scope. The client variable from the using declaration is not even defined outside the declaration.

– Clemens
Nov 15 '18 at 10:39














1 Answer
1






active

oldest

votes


















1














Why would you expect it to be null? Disposing an object doesn't set references to it to null, it simply calls Dispose(), rendering the instance to an unusable state (when properly implemented).



See also Setting an object to null vs Dispose().



Also, don't dispose your HttpClient, read You're using HttpClient wrong and it is destabilizing your software.






share|improve this answer
























  • but if i use using (var client =new httpclient()) then it gives null outside scope, why so?

    – sum1
    Nov 15 '18 at 9:52













  • and i do not have frequent calls to this method, no performance issues thats why disposing it.

    – sum1
    Nov 15 '18 at 9:54


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Why would you expect it to be null? Disposing an object doesn't set references to it to null, it simply calls Dispose(), rendering the instance to an unusable state (when properly implemented).



See also Setting an object to null vs Dispose().



Also, don't dispose your HttpClient, read You're using HttpClient wrong and it is destabilizing your software.






share|improve this answer
























  • but if i use using (var client =new httpclient()) then it gives null outside scope, why so?

    – sum1
    Nov 15 '18 at 9:52













  • and i do not have frequent calls to this method, no performance issues thats why disposing it.

    – sum1
    Nov 15 '18 at 9:54
















1














Why would you expect it to be null? Disposing an object doesn't set references to it to null, it simply calls Dispose(), rendering the instance to an unusable state (when properly implemented).



See also Setting an object to null vs Dispose().



Also, don't dispose your HttpClient, read You're using HttpClient wrong and it is destabilizing your software.






share|improve this answer
























  • but if i use using (var client =new httpclient()) then it gives null outside scope, why so?

    – sum1
    Nov 15 '18 at 9:52













  • and i do not have frequent calls to this method, no performance issues thats why disposing it.

    – sum1
    Nov 15 '18 at 9:54














1












1








1







Why would you expect it to be null? Disposing an object doesn't set references to it to null, it simply calls Dispose(), rendering the instance to an unusable state (when properly implemented).



See also Setting an object to null vs Dispose().



Also, don't dispose your HttpClient, read You're using HttpClient wrong and it is destabilizing your software.






share|improve this answer













Why would you expect it to be null? Disposing an object doesn't set references to it to null, it simply calls Dispose(), rendering the instance to an unusable state (when properly implemented).



See also Setting an object to null vs Dispose().



Also, don't dispose your HttpClient, read You're using HttpClient wrong and it is destabilizing your software.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 9:40









CodeCasterCodeCaster

109k17145196




109k17145196













  • but if i use using (var client =new httpclient()) then it gives null outside scope, why so?

    – sum1
    Nov 15 '18 at 9:52













  • and i do not have frequent calls to this method, no performance issues thats why disposing it.

    – sum1
    Nov 15 '18 at 9:54



















  • but if i use using (var client =new httpclient()) then it gives null outside scope, why so?

    – sum1
    Nov 15 '18 at 9:52













  • and i do not have frequent calls to this method, no performance issues thats why disposing it.

    – sum1
    Nov 15 '18 at 9:54

















but if i use using (var client =new httpclient()) then it gives null outside scope, why so?

– sum1
Nov 15 '18 at 9:52







but if i use using (var client =new httpclient()) then it gives null outside scope, why so?

– sum1
Nov 15 '18 at 9:52















and i do not have frequent calls to this method, no performance issues thats why disposing it.

– sum1
Nov 15 '18 at 9:54





and i do not have frequent calls to this method, no performance issues thats why disposing it.

– sum1
Nov 15 '18 at 9:54





Popular posts from this blog

Bressuire

Vorschmack

Quarantine