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

Wednesday, October 12, 2011

Understanding SOAP and REST

Which is better SOAP or REST? One of the most common discussions. Both REST and SOAP are different approaches in writing the service oriented applications. REST is an architectural style for building client-server applications. SOAP is a protocol for exchanging data between two endpoints.

It is more appropriate if you compare REST with RPC(remote procedure call). RPC is a style of building client-server applications. Compared to RPC there won’t be generated proxy for client which is less coupled to the service.

REST relies on HTTP, requests for data[Get requests] can be cached. RPC systems not having such infrastructure even when using SOAP over HTTP.

Both REST and SOAP can used to implement similar functionality but SOAP should be used when a particular feature of SOAP is needed.

Security

Is SOAP is more secured than REST? answer is no. It is easy to make REST based service secure as it is to make SOAP service.The security in REST  is in the form of HTTP-based authentication and Secure Sockets Layer(SSL).  Because of WS-* specifications SOAP supports the end-to-end message security.

Transactions

This is another feature that SOAP and WS-*  supports where REST has none.

WS-Atomic transactions supports distributed, two-phase commit transactional semantics over SOAP-based services. REST has no support for distributed transactions. To create something like transactions in REST you create a  resource called Transaction. When client wants to do some transaction and then he creates a resource that specifies all the correct resources.

Interoperability

SOAP services are less interoperable than REST Services. In terms of platforms, For REST all you need is HTTP stack. REST has widest interoperability like mobile devices,household devices, POS devices etc. The problem in SOAP and WS-* is the large number of standards to choose from.

Metadata

There is no direct way in REST to generating client from server-side-generated metadata. In SOAP with WSDL we can generate the client proxies. In REST we can achieve the same using WADL (Web Application Description Language). WSDL makes easier in generating the proxy than writing some code fro generating for REST service.

Protocol Support

Though REST is currently tied with HTTP but you still can implement the REST features on other protocols until vendors add support for this.

IS REST is for Internet-facing apps and SOAP for enterprise apps?

Answer is no. This question comes due to lack of distributed transaction support in REST vs explicit WS-atomic transactions in SOAP.

ASP.NET doesn’t have support for distributed transactions, but does that mean ASP.NET isn’t useful for enterprises?

Enterprise applications need scalability and speed. SOAP services are much harder to scale than RESTful services. Most of the scaling features can not be used with SOAP because SOAP uses POST only over HTTP.

Conclusion

“Which is better, REST or SOAP?” is “It depends”. Both REST and SOAP has advantages and disadvantages when it comes to building the services. When you need the features that are easy to implement using REST or SOAP choose it..

Monday, June 06, 2011

Difference Between Add Service Reference And Web Reference?

Service References or WCF comes from VS 2008 and above with an extension as .svc and Web References are their from the beginning as .asmx.

The main difference between WCF service and Webservice while consuming, we need to add them in solution as reference to access them.

Add Web Reference is a wrapper over wsdl.exe and can be used to create proxies for .NET 1.1 or 2.0 clients. This means we are pointing to a WCF service you have to be pointing to an endpoint that uses basicHttpBinding.

Add Service Reference is a wrapper over svcutil.exe and also creates clients proxies (and additionally web.config entries). These proxies, however, can only be consumed by .NET 3.0+ clients.

Below image will show you how to add web reference from Service Reference. Right click theService References from solution explorer, then select advance button on Add Services windowyou will get to the form as in below image and then will be able to add Web Reference via Addweb Reference window..

image

But there is some things which changed in VS2010 about adding web references to a class library. So in VS 2010 you cannot add Web Reference directly.  Here is how you need to do it.

From Project -> Add Service Reference ..., (From Solution Explorer, Right Click on Project, From the drop down Menu, Select Add Service Reference)

image

This window will appear, Click advanced,

image

On Add Service Settings window, Now click on Add Web Reference

image

This will open add Web reference window from where we can add web service. Hope this helps. Good luck.

Wednesday, June 09, 2010

How to: call/Invoke a web service without adding web reference

Why would anybody want to do this since there is a simple way given by Visual studio by adding a Web Reference from Solution Explorer. But recently wants to build an application which should install in different client locations with different names etc. As in the installer we can give different custom names to virtual directory this will not work for me.  That means “Virtual Directory” can be different for each client in that case i need modify the web reference as per the “Virtual Directory” name of the hosted solution.

So for that i end up adding proxy class of the web service in my solution by using WSDL tool. I need to generated Proxy class of the web service that i have created and add the proxy class in my Web solution.  For example here is a sample web service code for simplicity

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace SampleWebSerivce
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}


Once your done with your custom web methods, you add the web reference where you actually consume the web service methods. When you add a web reference - based on the WSDL specifications IDE actually creates proxy classes in your application which represents the web service classes and its methods on the remote server



Visual Studio provides Intellisense with Web services, which will not be accurate till you update your web reference each time you change your web service, it is because the proxy classes which are local to your solution are being used to offer you the Intellisense. So, ultimately when you invoke a Web method you are invoking a method in one of these proxy classes. These classes, in turn, are capable of invoking the remote methods on the Web Service URL.



So, every time the web service is updated, these proxy classes need to be regenerated by clicking Update Web Reference. However, the proxy classes do not always have to be generated from the IDE, .NET also gives us the WSDL.exe utility to manually generate these proxy classes and include them in your project.



Here is the syntax to create Proxy class from WSDL tool.wsdl



Just grab the class, and add in the App_Code folder if its a web site or you can add in root folder if it is a web solution.



If you open the proxy class you will notice  the constructor of the proxy class contains "url" member property which is by default assigned the url of the website in which the web service exists. You can store the web service url in web.config or in database or as per your logic.



So, finally to call the "Hello World" web method - you need to create object of this proxy class and assign the url property correctly as below:



public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Service1 objService = new Service1();
        objService.Url =
            System.Configuration.ConfigurationManager.AppSettings["WebServiceUrl"];
        Response.Write("Output of Web method call: " + objService.HelloWorld());
    }
}


Finally, You can call web service method without adding Web Reference. But make sure you update the proxy class through WSDL utility as and when you have any changes in the Web Service.