Friday, June 20, 2008

IsNumber IsNaN in C# .NET

Many times we get the requirement to find out where the input is a valid integer or not. In C#, there is no straight forward method for this. Although many basic languages like JavaScript provide IsNaN() function for the same purpose. Anyway, I will show how to suffice this requirement writing minimum amount of code possible

The magic code to do this is

int dummyInt;
if(Int32.TryParse(months,out dummyInt))
return "Yes, it is integer";
else
return "No, it is not an integer";


Some people use Int32.Parse and use try-catch block. But that option is not as quick as this.

Technorati Tags: ,


IceRocket Tags: ,

Monday, June 9, 2008

Creating REST web service using WCF

During last two weeks, I was working on a niche area when I was given a task to create a REST service in .NET. Upon my initial exploration, I found that REST support is only provided by Framework 3.5. I found out that finding REST in WCF articles over the net was not easy as very few and obsure links are available.

So here I'm publishing few links which will give you quick start if you want to create a REST service using WCF



Everything About REST Web Services - What and How - Part 1

Everything About REST Web Services - What and How - Part 2

Creating RESTful Web Services with Windows Communication Foundation

How to Build a REST app in .NET (with WCF)

How to create REST/POX web services with WCF

Essential XAML Simplified 1

Today we'll touch the three most lauded concepts introduced by the XAML (eXtensible Application Markup Language). XAML to me seems like XML + .NET. By that I mean XAML provides you the best of both worlds by leveraging descriptive nature of XML and object-oriented nature of .NET. It allows you to instantiate a .NET object like WatermarkedTextbox and describe it properties using pure XML driven syntax, and all that in the Markup. So lets go ahead and explore these three majestic Xaml concepts :

  1. Property Element Syntax
  2. Attached Properties
  3. Markup Extensions

Property Element Syntax

Basically in XAML markup we have three types of element syntaxes

  • Object Element Syntax
  • Attribute Element Syntax
  • Property Element Syntax

To understand them one by one, lets take an example of the a simple WPF control, the TextBlock

<TextBlock Text="Title:" Style="{StaticResource TextBlockStyle}"

Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right"></TextBlock>

Object element syntax refers to the way we have instantiated a .NET object ie TextBlock. Attribute element syntax means to how have used XML attributes (like HorizontalAlignment above) to define that object's properties and given it a value (ie Right). Now assume for a while that we have a TextBlock that displays a custom drawing instead of just plain text. Such TextBlock definition will look like this

<TextBlock Style="{StaticResource TextBlockStyle}" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right">

<TextBlock.Text>

<Canvas>..Drawing here...</Canvas>

</TextBlock.Text>

</TextBlock>
Property Element syntax pertains the way have defined the TextBlock's property comprehensively (a detailed drawing object) instead of just a word "Title:". So this ability of XAML to support myriad of property definitions is branded by the magic word "Property Element Syntax". Just for the sake of readability we'll cover the next 2 concepts in the subsequent post which can be found here

Monday, June 2, 2008

Differences between SOAP and REST

This article compares SOAP style web services with REST style web services. It highlights key business areas where REST scores over SOAP. The idea here is not to belittle SOAP which has been a strong pillar of distributed computing over the past decade but to bring its dark side to light.
SOAPREST
SOAP clients call every SOAP operation on the server using only Http GETREST clients leverage true functionality of HTTP methods like GET,POST,PUT and DELETE and use them to call the REST service
SOAP is operation centricREST is resource centric
SOAP mandates the use of WSDL and proxies on all the clients which need to call SOAP web serviceREST call be called from any client on any platform with using any proxy or WSDL
If SOAP WS Interface changes, you need to update proxies on the all clients which are hooked to that SOAP WSREST services doesn't have this limitation
SOAP puts overhead of and on the XML dataREST doesn't put any such overhead
Stateful SOAP services are not scalableREST services are stateless by design so they are scalable
Stateful SOAP services create much headache in Load-balanced environmentBecause REST is fundamentally stateless, this headache doesn't arises
SOAP objects are not cache-ableREST Uris are cache-able
SOAP puts additional overhead of SoapBody and SoapEnvelope which is considerable for low bandwidth clients like PDAs, Smartphones, etcREST doesn't have this limitation
SOAP is slower because it consumes time parsing XML and serializing objectsREST is faster because its simpler and doesn't have such schema dependencies
SOAP has a bigger learning curve as developers need to fully understand WSDL, UDDI, WS-*REST has very shorter learning curve as it extends directly from HTML

Despite these limitations SOAP is still the first choice when it comes to Reliable, Secure and Transactional Web Services. So each side has its pros and cons but REST is certainly more than it is thought to be.

Add Service Reference in VS 2008

Add Service Reference is a new feature in VS 2008 which primarily makes creating WCF proxy easy. You no longer need to goto Command Prompt and generate proxy files using svcutil.exe. Behind the scenes, Add Service Reference (ASR) calls svcutil.exe, which invokes a service’s MEX endpoint to query for its interfaces and to generate a proxy class and configuration file.

To Add Service Refernce to you WCF do the following steps in Client Visual Studio Project
1. Right Click on the project name and select “Add Service Reference”
2. Make sure your service host is running and the service is up.
3. Enter the service’s Uri into the Add Service Reference Dialog. Upon successful discovery, it will automatically retrieve the metadata (Service Names, Contracts Names, etc) from your service and show them in the dialog
4. Finally provide the Reference name
You dialog should look like this

The next figure shows how Proxy and Configuration files are automatically created
You can understand the differeces between Add Web Reference and Add Service Reference in my next post here