Create a LIB Base Class Model
1) Add a new project --> select a console application-->name it as "NorthWindLibrary"
2) add a class Product.cs and write code as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data.Linq.Mapping;
namespace NWLib
{
[Table(Name="Products")]
public class Product
{
[Column(Name="ProductId" ,IsPrimaryKey=true)]
public int Id { get; set; }
[Column]
public string productName { get; set; }
[Column]
public decimal UnitPrice { get; set; }
}
[CollectionDataContract]
public class Products : List<Product>
{
/// <summary>
/// Contructure to get list of product
/// </summary>
public Products() { }
/// <summary>
/// Contructure to get list of product with parameter
/// </summary>
public Products(List<Product> pList):base(pList) { }
}
}
3) Add New Interface and write code as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace NWLib
{
[ServiceContract]
public interface IProductService
{
[WebGet(UriTemplate="ShowProducts")]
[OperationContract]
Products GetAllProduct();
[WebGet(UriTemplate = "ShowProductsJson",ResponseFormat=WebMessageFormat.Json)]
[OperationContract]
Products GetAllProductjson();
[WebGet(UriTemplate = "SingleProduct/{pid}")]
[OperationContract]
Product GetProduct(string pid);
}
}
4)Add class ProductSvc and write code as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
namespace NWLib
{
public class ProductSvc : IProductService
{
DataContext context = new DataContext("Data Source=.;Initial Catalog=Northwind;Integrated Security=true");
public Products GetAllProduct()
{
var result = from p in context.GetTable<Product>()
select p;
return new Products(result.ToList());
}
public Products GetAllProductjson()
{
var result = from p in context.GetTable<Product>()
select p;
return new Products(result.ToList());
}
public Product GetProduct(string pid)
{
var result = (from p in context.GetTable<Product>()
where p.Id == Convert.ToInt32(pid)
select p).FirstOrDefault();
return result as Product;
}
}
}
3) build and run
Create a Host Application
1) Add a new project --> select a console application-->name it as "HostApp"
2) in Program.cs write code as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using NWLib;
namespace HostApp
{
class Program
{
static void Main(string[] args)
{
WebServiceHost host = new WebServiceHost(typeof(ProductSvc));
host.Open();
Console.WriteLine("Service started.");
Console.ReadLine();
host.Close();
}
}
}
3)Add App.Config and Do the Configuration by GUI or write code as follows in app.config
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ProductBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="NWBehaviour">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NWBehaviour" name="NWLib.ProductSvc">
<endpoint address="http://localhost:9560/PS" behaviorConfiguration="ProductBehavior"
binding="webHttpBinding" bindingConfiguration="" contract="NWLib.IProductService" />
</service>
</services>
</system.serviceModel>
</configuration>
4) build and Run
Create a Client Application
1) Add a new project --> select a console application-->name it as "ClientApp"
2) In Program.cs write code as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace ClientApp
{
class Program
{
static void Main(string[] args)
{
UsingWebRequest();
}
private static void UsingWebRequest()
{
///To Get all list of Products in Default XMl format
//string url = "http://localhost:9560/PS/ShowProducts";
///To Get all list of Products in Json format
//string url = "http://localhost:9560/PS/ShowProductsJson";
///To Get a single product when we pass a productid=2
string url = "http://localhost:9560/PS/SingleProduct/2";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
string data = reader.ReadToEnd();
Console.WriteLine(data);
Console.ReadLine();
}
}
}
3)Build and Run
No comments:
Post a Comment