Friday, August 14, 2009

WCF first Programming

Windows Communiction Foundation Service(Indigo):• Indigo combines and extends the capabilities of existing Microsoft distributed systems technologies, including Enterprise Services, System.Messaging, .NET Remoting, ASMX, and Web Service Enhancements to deliver a unified development experience.
• Windows communication foundation service is primarily based on the System.ServiceModel namespace.
The endpoint is the basic unit of communication in Windows Communication Foundation (WCF). Each endpoint is made up of three elements: an address, a binding, and a contract.

Address: Address specifies the info about where can I find my service.
Binding: Binding specifies the info that how can I interact with my service.
Contracts: contracts specifies the info that how my service is implemented.
Address: Every WCF endpoint must have an address so that other end points can initialize the communication. Each Address consists of an Uri and Address properties. The address properties consist of an Identity and optional Headers.
The endpoints can be added programmatically or in configuration files. The configuration files provide the flexibility of changing the endpoints in the future without a need to recompile the application and deploying it. Addresses will be of the format http://localhost:8000/HelloWorld.

Binding: Bindings describe how the endpoints communicate with the external world.
The bindings specify the protocol stack, transport protocol, Encoding.
BasicHttpBinding For communication with ASMX based clients
wsHttpBinding Secure WS interoperable binding for non duplex contracts
wsDualHttpBinding Secure WS interoperable binding for duplex contracts
netTcpBinding Secure reliable binding for Cross machine communication
NetNamedPipeBinding For on machine communication
NetMsmqBinding Queued binding for cross machine communication.
MsmqIntegrationBinding maps MSMQ messages to Windows Communication Foundation (WCF) messages.

Contract : A contract specifies what the endpoint communicates. What the service can offer. It is a collection of messages. Called message exchange.(MEX).

how we can configure an endpoint using the configuration based programming approach.


Three methods of programming are supported in the Indigo programming model:
Declarative programming: the usage of attributes for the types
Imperative programming: OO based programming in code.
Configuration based programming: specifying the information required in the .config files(web.config/App.config)
Step 1: Creating a new service.
IN VS2005 create a new C# console application project.
Add System.service model as a reference from the .Net components tab.
Add the statement using System.ServiceModel; in the class file.
Copy the following code in the EmployeeService.cs

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IndigoEmployeeService
{
// Defining the contract.

[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
DataSet GetInfo();
}
[ServiceBehavior]
class EmployeeService:IEmployeeService
{
public DataSet GetInfo()
{
SqlConnection _conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;User Id=sa;Password=yukon;");

_conn.Open();

try

{
SqlDataAdapter _sqlAdapter = new SqlDataAdapter("select EmployeeID,LastName, FirstName, Address from employees", _conn);

DataSet _employeeDs = new DataSet();
_sqlAdapter.Fill(_employeeDs);
return _employeeDs;

}
catch (Exception ex)
{
return null;
}
finally
{
_conn.Close();
_conn.Dispose();
}

}

public static void Main()

{

// Create a ServiceHost.

Uri uri = new Uri("http://localhost:8000/EmployeeService1");

using (ServiceHost serviceHost = new ServiceHost(typeof(EmployeeService), uri))

{

serviceHost.Open();

Console.WriteLine();

Console.WriteLine("The service is ready");

Console.WriteLine();

Console.WriteLine("Press ENTER to shut down service.");

Console.WriteLine();

Console.ReadLine();

// Close the service.

serviceHost.Close();

}

}

}

}





}

public static void Main()

{

// Create a ServiceHost.

Uri uri = new Uri("http://localhost:8000/EmployeeService1");

using (ServiceHost serviceHost = new ServiceHost(typeof(EmployeeService), uri))

{

serviceHost.Open();

Console.WriteLine();

Console.WriteLine("The service is ready");

Console.WriteLine();

Console.WriteLine("Press ENTER to shut down service.");

Console.WriteLine();

Console.ReadLine();

// Close the service.

serviceHost.Close();

}

}

}

}

Step 2: Add the configuration file to the service.

1. rightclick on the service project and click add new item.

2. add the Application configuration file.

3. In the App.config, specify the ‘endpoint’ information and binding information.

4. The end point can be configured as below:




type="IndigoEmployeeService.EmployeeService"

behaviorConfiguration="EmployeeServiceBehavior">


binding="wsHttpBinding"

bindingConfiguration="Binding1"

contract="IndigoEmployeeService.IEmployeeService" />





Lets deal with the elements in the config file above.

< service > tag:
type: specifies the type of our service. This is the class name of the Service.

behaviorConfiguration: specifies the behavior to be used to instantiate the service. This behavior is defined as below.




name="EmployeeServiceBehavior"

returnUnknownExceptionsAsFaults="True" >





Now we have to define the properties of the end point. As we discussed earlier the endpoint has three elements address, binding and contract.

Address is the Uri object that we have specified in the EmployeeService.cs code . in this case it is http://localhost:8000/EmployeeService1 if this is left as blank this will take the base address.


binding:- we use the predefined wsHttpBinding that ships with WCF.

returnUnknownExceptionsAsFaults enables the unhandled exceptions to be returned as fault messages. This will be very much useful for debugging purposes.


Code Analysis- Employee service

First we define an interface IEmployeeService and mark it with [ServiceContract] Attribute in System.ServiceModel

Then we declare the operation to be performed by the service. In this case GetInfo() is a method that returns a data set. We mark it with [OperationContract] attribute.

Then comes the implementation of our servicecontract.

We implement the IEmployeeService contract i

First Programming WCF

Windows communication foundation previously code named Indigo is the Microsofts next generation programming platform for building Distributed systems.In this article we will deal with the basics of Windows communicaiton foundation service and a simple walkthrough of creating a simple WCF service with VS2005..

Windows Communiction Foundation Service(Indigo):

Indigo combines and extends the capabilities of existing Microsoft distributed systems technologies, including Enterprise Services, System.Messaging, .NET Remoting, ASMX, and WSE to deliver a unified development experience.

Windows communication foundation service is primarily based on the System.ServiceModel namespace. This is the Programming interface to the developers. The System.ServiceModel namespace is very rich in its design so that it allows much easier programming interface.

Before getting into first programming sample of a WCF service, lets look into the programming model of the WCF service.

Tuesday, August 11, 2009

Sites for .net architecture

LINQ to SQL

http://msdn.microsoft.com/en-us/library/bb386976.aspx


http://www.davidhayden.com/default.aspx


How can I create a Data Access Layer using LINQ??

http://social.msdn.microsoft.com/Forums/en-US/architecturegeneral/thread/63f45ea8-d431-457d-8f1b-5144df5eaa76

Friday, July 24, 2009

WPF Basics

What is WPF?

WPF is the engine that is responsible for creating, displaying, and manipulating user-interfaces, documents, images, movies, and media in Windows Vista.

Physically, WPF is a set of libraries that have all functionalty you need to build, run, execute, and manage Windows Vista applications.

What is XAML?

XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next generation managed applications.

How XAML is related to WPF?

XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next generation managed applications. XAML is used in WPF to represent the controls and code with the help of C#, Visual Basic, and other .NET Framework languages.



XAML can be think as ASP.NET and/or Windows Forms in Windows Vista. For example, to write a Web application in .NET 1.0, 1.1, or 2.0, you use ASP.NET and to write Windows Applications, you use Windows Forms. Now in Windows Vista and .NET 3.0, you will use XAML instead of Windows Forms and ASP.NET.



Does that mean XAML will replace ASP.NET and Windows Forms? YES and NO. Both ASP.NET and Windows Forms will also be supported on .NET 3.0 but you don't have to use them if you don't want.

What Operating Systems does WPF support?

Windows Vista, Windows XP, and Windows 2003 Server.

How do I build WPF Applicaitons?

To build WPF application, you must install .NET 3.0 SDK. It can be found on MSDN downloads sites.

What do I need to run WPF Applications?

To run WPF applications, you must install .NET 3.0 SDK redistributable. It can be found on MSDN downloads sites.

Get Started with WPF

Download a free version of Visual Studio 2008, if you do not have Visual Studio 2008 or express version here: www.c-sharpcorner.com/Downloads

WCF Basics

WCF is fundamentally different from previous communication technologies. Another common "jargon-phrase" you will hear thrown around a lot is "SOA" or Service Oriented Architecture. Extremely simplified, think of SOA has different pieces of code/services running around the world and clients can make use of those services. These service hosts take care of internal complexities, and the communication infrastructure allows for things such as versioning and the communication infrastructure.

Thus, a big tenet of SOA is isolation. Let me explain with an example.

Say, you are consuming a web service, that accepts a zipcode, and returns you a weather forecast. All you, the client, ever cares about is .. the zipcode (input), and the forecast (output). Beyond the input/output .. or request/response .. the service host .. never talks to the client. There is an isolation between the client and server, and they interoperate on only what they had agreed to interoperate upon, i.e. the contract. So, this brings me to the first important piece in WCF, "The Contract".

Contract: This is the heart of your service - what does your service do!

In WCF, a contract is usually implemented as an interface decorated by the [ServiceContractAttribute]. For instance, in this particular case, your contract would look like this -

1: [ServiceContract]
2: public interface IWeatherService
3: {
4: [OperationContract]
5: public WeatherForecast GetForeCast(int zipCode);
6: }

But is a contract all I need? Well no .. because I need to know atleast 3 peices of information before I can use such a service -

Where can I find this service - The Address.
What protocol will I use in using this service - The Binding.
And of course the contract - when I know where the service is (address), and how I will technologically speaking use it (binding) - what the heck will I do with it .. the Contract.
So, in WCF, you have ABC's -

The Basics

Address: Address is where your service can be found. Usually in the format of scheme://domaon[:port]/path. Example http://localhost:8080/MyService

Binding: A binding refers to specific protocols used by a particular endpoint. Example, http, tcp, msmq etc.

Contract: This is the heart of your service - what does your service do!

These 3 together, form an "EndPoint".

EndPoint: EndPoint is how a ServiceHost, exposes a service, so clients can invoke it's operations defined in the contract. (Read that again!). So, how is an EndPoint different from an Address? An EndPoint have 3 peices - Address, Binding, and Contract.

Now, note that in the contract I showed above, I am using a return type called "WeatherForecast". The "WeatherForecast" is a class I wrote, that represents a business object which lets me conveniently hold the details of the data I am accepting or sending. This along with all details of the endpoint constitute, the "MetaData" for the service.

Also, for "WeatherForecast" to go over the wire, it must be Serializable. Being Serializable means, the object can be hydrated, dehydradted from a stream/bunch of bytes, into a living class instance. Thus, if the human body was serializable, teleportation like Star Wars shows us all the time, would be possible. The business object "Human Body" would be dehydrated into "information" - sent over a communication channel(s), and then rehydrated at the other side - to produce a human being on the other side. Sure beats my commute.

And given the MetaData for a service, a consumer of the service, can now create a Proxy - which is an empty shell, or a type, that exposes all details of the service.

Proxies: A client communicates with a host using a proxy. A proxy is an empty type that exposes all operations in a service contract, and hides serialization/sending over wire details. A single proxy, uses a single endpoint.

Finally, the client, and the host, talk to each other through various channels. And around these channels, you may have various behaviors associated with the service.

Channels: Channels, is what a message goes through when it goes between a client and a host. Usually you have multiple stacked channels.

Behavior: Behaviors modify the message as they flow through a channel stack. A good example of a behavior is authorization.

So .. overall ..

You have a host, and a client, both agree on a contract. The host exposes EndPoints which are a combination of Address, Binding and Contract. The client uses a proxy, that is tied to a particular endpoint - thus tied to a particular contract. And the actual communication related details are abstracted in Channels and Behaviors.

That one line above, summarizes WCF.

So, what makes WCF different?

You first think of the Contract - What am I trying to get done!?
Then you think of where will I host it, and what technology will I use to host it.
Then you worry about the nitty gritties such as authentication etc.
For the very first time, you have a communication technology, that lets you first focus on what you're trying to get done, and worry about communication later.