Monday, December 20, 2010

Implicitly Typed Local Variables Vs Dynamic Type

Implicitly Typed local variables are declared using “var” keyword. You must need to initialize value at the time of declaring variable. For an example
var counter = 5;
From the above example, Compiler will determine “counter” variable as an int type variable. If you forgot to initialize variable value then compiler will give error (“Implicitly-typed local variables must be initialized”).

Dynamic type is declared using “dynamic” keyword. You don’t require to assign value at the time of declaring variable. Like
dynamic counter;
Compiler doesn’t know about the value but when you set any value to the variable, the variable will hold that type and you can use it. It is similar to Object type. Like
dynamic counter;
counter = 5;
counter = new Employee();

  • Developer will get intellisense support with “var” keyword. But you will not get intellisense support with “dynamic” keyword.
  • Implicit local variable give compile type error if any. Dynamic variable gives only runtime error if any.
  • Implicit local variable assign value at the time of declaration only. Developer can change the value but can’t change the type. Dynamic variable gives facility to assign any type of value at anytime. For example if you assign value 5 (numeric) to implicit typed variable then you can’t assign any other type (string, Boolean etc) to that variable. For dynamic type, you can assign any type of value anytime.

Wednesday, November 24, 2010

What is EnableEventValidation

EnableEventValidation attribute is comes with Page directives. EnableEventValidation attribute accept Boolean value i.e. True or False.

When EnableEventValidation is set to True, ASP.Net application will validate the control event generated from the client UI. All ASP.Net control supports the EnableEventValidation features. For an example, If you have one dropdown list which fires page post back and it contains value 1,2 and 3. Now when user select any option then page will get post back and ASP.Net application will validate the request. If ASP.Net gets value 4 in your dropdown box (which is not exists initially) then ASP.Net application will raise the error.

EnableEventValidation have default value “True”. If you required to add some options at runtime using Javascript then you have to set EnableEventValidation=False.

You can also use “RegisterForEventValidation” when adding items at client side.

What is AutoEventWireup

AutoEventWireup is an attribute attached with the Page directives. AutoEventWireup accept Boolean value i.e. True or False.

AutoEventWireup attribute specify that ASP.Net wire up the code behind events with the page automatically or not. I.e. If you set AutoEventWireup = True then ASP.Net will execute code behind events automatically according to the event name. Event names must be predefined name like Page_Init, Page_Load etc… You need not required to attach handler with events. Disadvantage is that you can’t specify event name.

If you set AutoEventWireup = false, then you need to specify handler of event or you have to bind the event with the control.

Default value of AutoEventWireup is true.

Sometime you got a problem of executing any event two times. It is due to the AutoEventWireup attribute.

Sunday, November 21, 2010

WCF Contracts

WCF supports following contracts:
1. Service Contract
2. Operation Contract
3. Message Contract
4. Fault Contract

Service Contract: We can use this service contract attribute on interface which will be exposed to client to consume the WCF service.

public interface IStudentContract
List GetStudents();

Student GetStudent(int studentId);


Here we added “ServiceContract” attribute to IStudentContract interface. So WCF will expose this interface as a WCF service.

Operation Contract: We need to use “OperationContract” attribute to the methods which needs to be exposed as a service method. In above example, We have added “OperationContract” attribute to “GetStudents” and “GetStudent” methods.

Message Contract: Sometime we need to change SOAP message format, then we can use Message contract. We can place some variables in body and some in header part as per requirement.

public class Student
public int StudentId;

public string StudentName;

public int Standard;

Here, student class is decorated as a MessageContract attribute. When we transfer object of student class between client and service then WCF will include “StudentId” attribute to Message Header and “StudentName” and “Standard” attribute in Message Body.

Fault Contract: Fault contract is used to managing fault (exception) in WCF service. You can decorate any class as a Fault contract and send it as an Exception from service to client.


ABC of WCF is “Address, Binding and Contract”. Address part specify where the service is available. Binding part specify which protocol and encoding method, service is using. And Contract specify which message format, service is expected from the client.

WCF supports many types of addresses to host/consume WCF service like HTTP, HTTPS, Net TCP, Net MSMQ etc... Address gives information where client needs to send message.


WCF supports following in-build binding:

1. basicHttpBinding
2. wsHttpBinding
3. wsDualHttpBinding
4. wsFederationHttpBinding
5. netTcpBinding
6. netNamedPipeBinding
7. netMSMQBinding
8. netPeerTcpBinding
9. webHttpBinding
10. msmqIntegrationBinding

Binding gives information about the protocol and encoding method to send the message.

Contract expose functionality to client. Contract have message signature so client can send the message to service via consuming service contract.

WCF Hosting Options

WCF gives facility to developer to host their WCF service on following hosting options:

1. IIS
2. Console Application
3. Windows Application
4. Windows Service
5. Self Hosting

Developer can host WCF service on IIS. We need to create virtual directory in IIS and create SVC file for exposing WCF service. After doing that, IIS will take care of availability of service. Whenever you will start your machine, WCF service will be available to your customer. You don't required to start any program.

IIS also gives some in-built functionality like web gardening, web farming, recycling etc.

Console application:
Developer can host WCF service on Console application. This option mainly used for testing purpose. You must need to start Console application for service availability. If anybody will close console application then service will not be available to any customer for consumption.

Windows Application:
Developer can host WCF service on Windows application. This option also mainly used for testing purpose. You must need to start Windows application for service availability. If anybody will close windows application then service will not be available to any customer for consumption.

Windows Service:
Developer can also host WCF service in Windows Service. Windows service runs in background. This option is best option to host WCF service if IIS is not available on the server. You can set windows service startup property to Automatic. Windows service will start as soon as machine start so you don't need to start any application for service availability.

Self hosting:
WCF also provides facility to host itself. This used for only testing purpose on developer machine. You can select WCF service project and mark it as startup project. After running the project, Visual Studio IDE will host the WCF service in itself.

WCF Tenets

There are four tenets of WCF:

1. Service boundaries are explicit
In WCF, we must need to expose the service contract to client so client can consume the service and call service method. If we will expose the implemented class then it will mess the boundaries of your service.
2. Service are autonomous
Service must be give full information about itself to client who wants to consume it like address where service is available, which message format service is expected, what transport service is using to receive messages etc.
3. Share Schema not the implementation
Expose only service contract (interface) to client. Never share implemented code to client. It will be very difficult to manage the changes if we will share code with the clients. We need to change code to every place if any change needs into service code.
4. Service Compatibility is Policy based
Service compatibility must be policy based so whenever Admin user will change any policy developer does not required to change accordingly.

DotNet Code Guru