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..

Saturday, September 10, 2011

Convert DateTime values to W3C DateTime format in C#

ConvertDateToW3CTime() function takes a C# DateTime value and converts it to a W3C formatted date/time value.
The function works by first converting the date/time parameter to a UTC (Coordinated Universal Time) value and formatting it. It then appends the UTC offset time to the previously formatted string.

The T placed between the date and time simply indicates that the numbers following it are Time values. The UTC offset can be one 3 states, zero e.g. there is not difference in time between the UTC value and the local value. A zero value is identified by simply appending the letter Z to the end of the formatted datetime value. If the offset time is greater than 0 then it is preceded with a + sign, e.g. 2 hours and 30 minutes over the UTC time would be written as +02:30. If the offset is less than the UTC value then it is preceded with a sign e.g. -01:00.

/// <summary>
/// Converts a datetime value to w3c format
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string ConvertDateToW3CTime(DateTime date)
{
//Get the utc offset from the date value
var utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(date);
string w3CTime = date.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss");
//append the offset e.g. z=0, add 1 hour is +01:00
w3CTime += utcOffset == TimeSpan.Zero ? "Z" :
String.Format("{0}{1:00}:{2:00}", (utcOffset > TimeSpan.Zero ? "+" : "-")
, utcOffset.Hours, utcOffset.Minutes);

return w3CTime;
}



Here is an example, how to use it


ConvertDateToW3CTime(DateTime.Now);
//Output 2011-10-17T19:10:48+01:00

Thursday, August 25, 2011

How to Close the Browser Window from a Silverlight Application

All secured sites ask you to close your browser window after you sign out from a web application. This is a security measure which actually removes all session details from the browser cache.

If you are developing a secured site and want to close the browser window just after the user logs out from the application, this small tip will help you. If you want to develop the same behavior in your Silverlight application, this is how we can do the trick.

Use  the "System.Windows.Browser.HtmlPage.Window.Invoke()" method to call the Close() method of the browser window, as shown in the below code snippet:

private void OnWindowCloseClick(object sender, RoutedEventArgs e)
{
    System.Windows.Browser.HtmlPage.Window.Invoke("close");
}

The above code when called will close the browser window where your Silverlight application is hosted. If it is a tab, it will close the Window tab instead. If you are using it inside Internet Explorer, it will ask you whether you really want to close the browser. If you press "No", it will remain in that page, and clicking "Yes" will close the browser tab/window

Monday, August 01, 2011

Tips for Cross Browser Compatibility

Cross-browser compatibility is one of the most time consuming tasks for any web designer. We’ve seen many different articles over the net describing common problems and fixes. There are some things you should consider for Safari and Firefox also, and IE isn’t always the culprit for your CSS woes. Here is a quick summary of How to get Cross Browser Compatibility Every Time:

  • Always use strict doctype and standards-compliant HTML/CSS
  • Always use a reset at the start of your css
  • Use -moz-opacity:0.99 on text elements to clean up rendering in Firefox, and text-shadow: #000 0 0 0 in Safari
  • Never resize images in the CSS or HTML
  • Check font rendering in every browser. Don’t use Lucida
  • Size text as a % in the body, and as em’s throughout
  • All layout divs that are floated should include display:inline and overflow:hidden
  • Containers should have overflow:auto and trigger hasLayout via a width or height
  • Don’t use any fancy CSS3 selectors
  • Don’t use transparent PNG’s unless you have loaded the alpha

Thursday, July 21, 2011

Keyboard Shortcuts: SQL Server Management Studio

SQL Server Management Studio offers users two keyboard schemes. By default, it uses the Standard scheme, with keyboard shortcuts based on Microsoft Visual Studio. A second scheme, called SQL Server 2000, closely resembles the tools from SQL Server 2000, in particular the keyboard shortcuts from the Query Analyzer. In a few cases, SQL Server Management Studio cannot offer the keyboard shortcuts from Query Analyzer. To change the keyboard scheme or add additional keyboard shortcuts, on the Tools menu, click Options. Select the desired keyboard scheme on the Environment, Keyboard page.

For Shortcuts page. Click here to navigate.