Resources

WCF Services in ASP.NET websites

WCF Introduction

WCF is unifying programming model. Single approach to writing services and unifying technologies such as web services (ASMX), .NET remoting, message queues (MSMQ), Enterprise Services (COM+) and Web Services Enhancements (WSE).

WCF is message based communications between endpoints.

Write service and then attach one (or more) endpoints.

Endpoint defines location to which messages are sent or received.

Location includes address, binding and contract:

After defined and configured WCF service it must be hosted. Several options for this, but concentrate on IIS and ASP.NET here.

WCF Architectural Layers

WCF Service with ASP.NET

Follow these steps to create WCF service:

  1. Define service contract
  2. Implement service contract
  3. Configure service endpoints
  4. Host service
  5. Reference and call service from client

Define service contract by defining interface and decorating it with attributes - typically:

[ServiceContract] 
public interface IShipperService
{
[OperationContract]
Shipper GetShipper(int shipperId);

[OperationContract]
Shipper SaveShipper(Shipper shipper);
}

WCF Service Templates

Visual Studio provides WCF Service Application project template.

The IService1.cs file defines an interface to be used as the service contract.

The Service1.svc is a webpage that provides URL for service. Uses @ ServiceHost directive to indicate page is a service. Code behind file implements the service.

Automatically configured to be hosted by IIS - see <system.serviceModel> section of web.config.

Can also add WVF service to existing website via the Add New Item dialogue.

Consuming WCF Service

Right click website and select "Add Service Reference" and navigate to .svc URL for web service.

Namespace in dialogue defines name for generated proxy class.

Reference.cs contains the actual proxy code, rest of files used when working with webservice.

Will also generate appropriate endpoint information and write to web.config file, e.g.:

<client>

<endpoint address="http://localhost/ShipperService.svc" binding="basicHttPBinding" bindingConfiguration="BasicHttpBinding_IShipperService", contract="ShipperServiceRef.IShipperService" name="BasicHttpBinding_IShipperService" /)

</client>

Calling WCF Service via AJAX (REST and JSON)

WCF is mechanism to define endpoints, binding and contracts so supports many message types and protocols, e.g. representational state transfer (REST) and JavaScript Object Notation (JSON).

Programming AJAX became easier with a simple service (REST) based on simple message format (JSON).

REST web service responds to basic HTTP requests, e.g. GET, POST, PUT, DELETE. Clients call in same way they would a web page. REST service does not require knowledge of XML schema used to call service, instead simply uses text-based responses (usually JSON formatted data).

REST does not use SOAP. Many service based security models are based on SOAP. If data security is important use HTTPS between client and server.

Example of JSON formatted message:

{ 
"productName": "Computer Monitor",
"price": "229.00"
}

Creating WCF Service using REST and JSON

AJAX-WCF template available via "Add New Item" | AJAX-Enabled WCF Service template.

[ServiceContract(Namespace="PricingServices")]  
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirements.Allowed)]
public class PricingService
{
[OperationContract]

[WebInvoke]
public double CalculatePrice("string itemId, string shipToPostalCode)
{
}
}

The WebInvoke attribute indicates method is called by HTTP request.

WebInvoke methods are called by HTTP POST. Use when sending data to server or don't want request to be cached.

WebGet methods are called by HTTP GET. Used to return static data that can be cached.

The web.config will be modified so that the binding is set to webHttpBinding - indicates service called via HTTP (not SOAP).

<endpoint address="" behaviorConfiguration="PricingServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="PricingService" />

The endpointBehavior has an enableWebScript element appended to it indicating that the end point is a RESTful using JSON data format.

<endpointBehaviors>

<behavior name="PricingServiceAspNetAjaxBehavior">

<enableWebScript />

</behavior>

</endpointBehaviors>

Calling JSON based WCF Service from AJAX

ScriptManager control allows a service reference to be set to RESTful WCF service.

<asp:ScriptManager ID="ScriptManager1" runat="server">

<Services>

<asp:ServiceReference Path="PricingService.svc"/>

</Services>

</asp:ScriptManager>

Then use script block on page to call proxy generated by service reference, passing in callback method if required.

<script language="javascript" type="text/javascript">

function ButtonCalculate_onclick() {

var service = PricingServices,PricingService();

service.CalculatePrice(document.forms[0].TxtBoxProduct.value, document.forms[0].TextBoxPostCode.value, onSuccess, null, null);

}

onSuccess(result) {

LabelPrice.innerText = result;

}

</script>

WCF Data Services

Can expose and work with data via Open Data Protocol (OData).

Exposed over REST-based services.

Follow these steps:

  1. Define data model using Entity Data Model (EDM) via ADO.NET Entity Data Model template.
  2. Create data service using WCF Data Services template.
  3. Add code to data service to access the EDM, e.g. to expose the "Customer" table: public static void InitializeService(DataServiceConfiguration config) { config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.2; config.SetEntityAccessRule("Customer", EntitySetRights.AllRead | EntitySetRights.AllWrite);
  4. Create client to access data service.
  5. Add service reference to client - generates WCF proxy for calling service.
  6. Write code to work with exposed data - often down via LINQ. Uri svcUri = new Uri("http://localhost/WcfDataSrv/NwdDataService.svc"); NwdEntitiesSrv.NewEntities nwd = new NwdEntitiesSrv.NewEntities(svcUri); DataServiceQuery<NwdEntitiesSrv.Customer> q = nwd.Customers; this.GridViewCustomers.DataSource = q; this.GridViewCustomers.DataBind();

Downloads