Showing posts with label Web API. Show all posts
Showing posts with label Web API. Show all posts

Monday, December 5, 2016

Web API VS WCF SOAP

Consume data on HTTP from clients like mobile, JavaScript, Windows applications, etc.
WebAPI is the technology by which you can expose data over HTTP following REST principles.


WCF SOAP
WEB API
Size
Heavyweight because of complicated WSDL structure.
Light weight, only the necessary information is transferred.
Protocol
Independent of protocols.
Only for HTTP protocol
Formats
To parse SOAP message, client needs to understand WSDL format.
Output of WebAPI is simple string messages, JSON, simple XML format, etc. So writing parsing logic for that is very easy.
Principles
SOAP follows WS-* specification.
WebAPI follows REST principles. (Please refer to REST in WCF chapter.)

WCF and WebAPI also can implement REST, why WebAPI
WCF was brought into implement SOA, the intention was never to implement REST. WebAPI is built from scratch and the only goal is to create HTTP services using REST. Due to the one point focus for creating REST service, WebAPI is more preferred.

Sunday, December 13, 2015

Write REST Web API

Attribute routing - More Control the Web API URL.

URL : /customers/1/orders
[Route("customers/{customerId}/orders")]
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }
(1)Enabling Attribute Routing 
(2)Convention-based
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();  (1)

        // Convention-based routing.
        config.Routes.MapHttpRoute(        (2)
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}



Tuesday, December 8, 2015

Web Request

string url = "PenyuApi.aspx?user=" + user + "&pass=" + pasword;

WebRequest request = WebRequest.Create(url);
request.Timeout = 30000; //5 sec call
WebResponse response = request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string Contents = stream.ReadToEnd();