HttpClient is not getting null outside using block [duplicate]
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?
c# asp.net .net wpf http
marked as duplicate by Camilo Terevinto
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.
add a comment |
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?
c# asp.net .net wpf http
marked as duplicate by Camilo Terevinto
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 thatusing (var client ...)
creates a differentclient
variable than the one declared at class scope. Theclient
variable from the using declaration is not even defined outside the declaration.
– Clemens
Nov 15 '18 at 10:39
add a comment |
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?
c# asp.net .net wpf http
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
c# asp.net .net wpf http
asked Nov 15 '18 at 9:37
sum1sum1
235
235
marked as duplicate by Camilo Terevinto
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
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 thatusing (var client ...)
creates a differentclient
variable than the one declared at class scope. Theclient
variable from the using declaration is not even defined outside the declaration.
– Clemens
Nov 15 '18 at 10:39
add a comment |
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 thatusing (var client ...)
creates a differentclient
variable than the one declared at class scope. Theclient
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 client
variable 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 client
variable 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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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 differentclient
variable than the one declared at class scope. Theclient
variable from the using declaration is not even defined outside the declaration.– Clemens
Nov 15 '18 at 10:39