Reputation: 11344
I want to return true/false for Task<HttpResponseMessage>
, how to do this.
Currently I'm getting below error here,
cannot convert from 'bool' to 'System.Func< System.Net.Http.HttpResponseMessage>'
private Task<HttpResponseMessage> PersistData()
{
Trace.TraceError("test");
return new Task<HttpResponseMessage>(true);
}
update:
I want to return Task<HttpResponseMessage>
with some empty or whatever, how to do this.
private Task<HttpResponseMessage> PersistData()
{
Trace.TraceError("test");
//what to return here?
}
Upvotes: 0
Views: 2806
Reputation: 11
You can not return True/False exactly, but you can handle this issue by bottom code.
Private Task<HttpResponseMessage> PersistData()
{
Trace.TraceError("test");
return new Task<HttpResponseMessage>(() => new HttpResponseMessage(HttpStatusCode.Accepted));
}
And after that you can check if HttpStatuseCode was "HttpStatuseCode.Accepted" its mean true OR another things. same this,
Private bool Validation()
{
var temp = PersistData();
if (temp.Result.StatusCode == HttpStatusCode.Accepted)
return true;
else
return false;
}
Upvotes: 1
Reputation: 1101
You can't return true or false for HttpResponseMessage, but you can change your type of return value.
private Task<bool> PersistData()
{
Trace.TraceError("test");
return Task.FromResult(true)
}
updated not async
private HttpResponseMessage PersistData()
{
Trace.TraceError("test");
return new HttpResponseMessage(HttpStatusCode.OK);
}
async
private Task<HttpResponseMessage> PersistData()
{
Trace.TraceError("test");
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
}
Upvotes: 1
Reputation: 10849
You can return the empty string using below code:
return Task.FromResult(Request.CreateResponse(HttpStatusCode.OK, string.Empty));
Here Request.CreateResponse
is extension method which creates HttpResponseMessage
. You can read the documentation at here.
You can read about Task.FromResult
at here.
Upvotes: 0