Search This Blog

Thursday, 24 April 2014

WebAPI service and WCF service calling in client application

WCF service details

class TestService : ITestService
{
    public Stream TestGet()
    {
        return new MemoryStream(Encoding.UTF8.GetBytes("Hello World. Time is: " + DateTime.Now));
    }
 
    public async Task<Stream> TestStream(Stream requestStream)
    {
        using (var reader = new StreamReader(requestStream))
        {
            var body = await reader.ReadToEndAsync();
 
            return new MemoryStream(Encoding.UTF8.GetBytes(body));
        }
 
    }
 
}



WebAPI service details

 
public class TestController : ApiController
{
    public HttpResponseMessage Get()
    {
        return new HttpResponseMessage() { Content = new StringContent("Hello World. Time is: " + DateTime.Now, Encoding.UTF8, "text/plain") };
    }
 
    public async Task<HttpResponseMessage> Post(HttpRequestMessage inputMessage)
    {
        var content = await inputMessage.Content.ReadAsByteArrayAsync();
        var response = new HttpResponseMessage { Content = new ByteArrayContent(content) };
        return response;
    }
 
    public async Task<HttpResponseMessage> Put(HttpRequestMessage inputMessage)
    {
        var content = await inputMessage.Content.ReadAsStreamAsync();
        var response = new HttpResponseMessage { Content = new StreamContent(content) };
        return response;
    }
 
 
}

No comments:

Post a Comment