Drop the Soap: WCF, REST, URIs in .NET 4
This post will cover REST in .NET 4 and Visual Web Developer 2010.
Getting Started
To get started we’ll create a basic WCF Rest Service Application
using the new on-line templates option in VVD 2010:
using the new on-line templates option in VVD 2010:
Dude Where’s my .Svc File?
The WCF REST template shows us the new way we can simply build services. Before we talk about what’s there, let’s look at what is not there:
- The .Svc File
- An Interface Contract
- Dozens of lines of configuration that you have to change to make your service work
REST in .NET 4 is greatly simplified and leverages the Web Routing capabilities used in ASP.NET MVC and other parts of the web frameworks. With REST in .NET 4 you use a global.asax to set the route to your service using the new ServiceRoute class. From there, the WCF runtime handles dispatching service calls to the methods based on the Uri Templates.
~ ~ ~
wcf 4 linq :
IService1.cs :
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WCF_JSONService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
// [WebGet(UriTemplate = "/Products",ResponseFormat=WebMessageFormat.Json)]
[WebGet(UriTemplate = "/Products", ResponseFormat = WebMessageFormat.Xml)]
List GetAllProducts();
}
[DataContract]
public class Product
{
[DataMember]
public int ProdId { get; set; }
[DataMember]
public string PropName { get; set; }
[DataMember]
public int Quantity { get; set; }
[DataMember]
public int Price { get; set; }
}
}
WCF and LINQ to SQL
WCF and LINQ to SQL (Part 2 (Creates))
WCF and LINQ to SQL (Part 3 (Deletes))
WCF and LINQ to SQL (Part 4 – Updates)
Simple 5 steps to expose WCF services using REST style
http://bitcast-g.bitgravity.com/iplayerhd/data/1/55936c11.mp4
The advantages of using JSON enabled WCF service is that the data is communicated in plain text like stream, it does not require any data/message parsing. For exposing a WCF service as JSON enabled WCF service, we need to apply the [WebGet] attribute on the method of the ServiceContract with ResponseFormat as JSON. The binding used here is WebHttpBinding which is also used for REST enable WCF services. While exposing WCF as JSON enable service, the EndPointBehavior for the WCF service is WebHttp which generates JSON response from the WCF service.
IService1.cs :
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WCF_JSONService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
// [WebGet(UriTemplate = "/Products",ResponseFormat=WebMessageFormat.Json)]
[WebGet(UriTemplate = "/Products", ResponseFormat = WebMessageFormat.Xml)]
List
}
[DataContract]
public class Product
{
[DataMember]
public int ProdId { get; set; }
[DataMember]
public string PropName { get; set; }
[DataMember]
public int Quantity { get; set; }
[DataMember]
public int Price { get; set; }
}
}
Service1.svc :
using System.Collections.Generic;
namespace WCF_JSONService
{
public class Service1 : IService1
{
public List GetAllProducts()
{
return new List
{
new Product() {ProdId=101,PropName="Laptop",Quantity=200,Price=45000},
new Product() {ProdId=102,PropName="Desktop",Quantity=300,Price=35000},
};
}
}
}
No comments:
Post a Comment