Hey, .NET 3.5 will come soon, and I haven’t got know anything about WCF. So I learn how to build service in WCF so it could be consume by clients. So this application contains 2 parts, service as server side and client that consumes the service. I will host the service into IIS.
At glance, WCF is a framework that provides communication mechanisms that involve client from windows and non windows world. Of course there are some aspects that should be taken into account for this service run reliable, scalable and secure to be accessed. I leave additional theory behind this WCF to leading leading MVP C# to explain more about this kind of “animal”.
So you install first your WCF, and Visual Studio extension for WCF and WPF, there is database sample used, AdventureWorks. I use enterprise library May 2007 build.
Make new project in VS2005, there is option NET Framework 3.0 in Visual C#’s or Visual Basic tree node of Project types list box, choose WCF Service Library. Name it ProductServiceProject. Configure project property Output Path from bin\debug to bin\ in Build section, make note to your assembly name and default namespace.
I also add reference Microsoft enterprise library for data.dll, common.dll, and object builder.dll
There is one class created by default, you just add corresponding Microsoft enterprise library namespace in “using (for C#)/Imports (for VB.NET)” section.
There must be 2 namespace that included in every class in WCF projects, “using System.ServiceModel” and “using System.Runtime.Serialization”. “using System.ServiceModel” contains all WCF related classes and “using System.Runtime.Serialization” contains all classes required to serialize object over wire and deserialize into object again.
Now at sample section you will see 2 attributes, ServiceContract and DataContract. DataContract identifies a class/enumeration/structure that can be serialized/deserialized as XML stream by WCF. ServiceContract identifies interface as a contract, so clients application would be able to generate metadata to consume the WCF service.
A class contains members, you can mark each members that you would like to serialize/deserialize with DataMember attribute. And for each member of interface that is marked as OperationContract, could be exposed with OperationContract interface. And you create a class to implement the interface. Let’s see below sample script to explain about this:
[DataContract]
public class Product
{
[DataMember]
public string ProductNumber;
}
[ServiceContract]
public interface IProductService
{
[OperationContract]
List
}
public class ProductServiceImpl : IProductService
{
public List
{
Database dbAdventureWorks = DatabaseFactory.CreateDatabase("AdventureWorksConnection");
string queryString = @"SELECT ProductNumber
FROM Production.Product";
IDataReader productReader = dbAdventureWorks.ExecuteReader(CommandType.Text, queryString);
List
while (productReader.Read())
{
string productNumber = productReader.GetString(0);
productsList.Add(productNumber);
}
return productsList;
}
}
If you want to deploy it to IIS, you need to specify the class that implement the interface and assembly that contains the class in separate service file (SVC file) as below script.
<%@ServiceHost Service="Products.ProductServiceImpl" %>
<%@Assembly Name="ProductsService" %>
In web config add below scripts in
contract="Products.IProductsService" />
Service Model element contains configuration information about WCF web service.
Services element contains detail implementations for each service.
Endpoint element contains detail of each services that client application will require in order to communicate with the corresponding service.
Address attribute is left blank because IIS will use SVC file.
Binding element provides transport mechanism to access the WCF web service.
Next step is to make virtual directory point to your WCF project. After deployment you can see your service by typing http://localhost/
And add new attribute inside service element with behaviorConfiguration="ProductsBehavior".
I have done for WCF service part. I try for WCF client part that consume the service.
No comments:
Post a Comment