Yoda
Yoda

Reputation: 339

Return a StringContent from async

How do I return a StringContent from async? Do I still need to return it as a task? Below is the sample code that I am using:

public async Task<HttpResponseMessage> GetOrder(string url)
{
    xml = "<result><success> True </success><message></result>";

    responseMessage = await httpContent;
    return new  HttpResponseMessage()
    {
        Content = new StringContent(xml, Encoding.UTF8, "application/xml")
    };    
}

Upvotes: 0

Views: 1259

Answers (2)

Nkosi
Nkosi

Reputation: 247451

If you are not actually using async/await then the method would look like this.

public Task<HttpResponseMessage> GetOrder(string url) {
    xml = "<result><success> True </success><message></result>";

    var responseMessage = new  HttpResponseMessage() {
        Content = new StringContent(xml, Encoding.UTF8, "application/xml")
    }; 

    return Task.FromResult(responseMessage);   
}

Upvotes: 2

Choco Smith
Choco Smith

Reputation: 1668

Is this what you are trying to do?

   public async Task<HttpResponseMessage> GetOrder(string url)
    {
        string xml = "<result><success> True </success><message></result>";

        return await Task<HttpResponseMessage>.Factory.StartNew(() => new HttpResponseMessage()
                                                    {
                                                        Content = new StringContent(xml, Encoding.UTF8, "application/xml")
                                                    });

    }

if so your return xml is also invalid. To quote one of the many online xml validators "The element type "message" must be terminated by the matching end-tag " (message)

Upvotes: 1

Related Questions