• 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
No comments:
Post a Comment