Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Task based WCF Services in .NET 4.5

Task based asynchronous programming is now simplified and streamlined in .NET 4.5 through the use of keywords ‘await’ and ‘async’. These keywords help makes asynchronous code look similar to synchronous coding, making it easier to write and understand Asynchronous code. Those who have used WCF service might have used the Asynchronous contract generation while adding WCF service reference in the Client application. The Asynchronous contracts are required so that client can make an Asynchronous call to WCF service performing time consuming operations.

In Visual Studio 2012 and WCF 4.5, there is a new option available to generate Task based operations so that the code from the client side can be then less complex. In the following example we will see how to implement it.

Sample Application

Step 1: Open VS2012 and create a blank solution, name it as ‘WCF_Task_Based_WCFService’. In this solution add a new WCF Service Application project targeting .NET 4.5 Framework. Name this project as ‘WCF_TaskBasedService’. Rename IService1.cs to IService.cs and Service1.svc to Service.svc.

Perform CRUD Operations using OData Services in .NET

In this article, we will see how to perform CRUD Operations using OData Services in .NET applications.

OData (Open Data Protocol) is a web protocol for performing CRUD operations which is built upon web technologies like HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to the data to various applications, services and stores.

We can use WCF Data Service to expose OData. For this demonstration, we will create a table with the Name Customers under database “CallCenter” in SQL Server 2008 R2. The table script is given below –

CREATE TABLE Customers
(
    CustomerID INT PRIMARY KEY,
    ContactName VARCHAR(50),
    Address VARCHAR(50),
    City VARCHAR(50),
    ContactNo VARCHAR(50)
)


Now let’s start by creating a WCF Data Service which will expose our CallCenter database entities using ADO.NET Entity Data Model. To create a WCF Data Service, let’s open Visual Studio 2012 and create a New Web Site. Choose “WCF Service” and name it as “ODataService_CRUDOperations”.

WCF RIA Services Toolkit Update

There is an update to the WCF RIA Services Toolkit and new features have been added including:
  • Linq to SQL support
  • EntityFramework 4.1 (EF CodeFirst support)
  • T4 Template based Code Generation
  • ViewModel (MVVM) support for CollectionViews
  • Windows Azure Table Storage support
  • SOAP and JSON endpoints
  • ASP.NET DomainDataSource support
  • Preview of the jQuery client for RIA Services (RIA/JS)

Microsoft WCF RIA Services simplifies the traditional n-tier application pattern by bringing together the ASP.NET and Silverlight platforms. The WCF RIA Services Toolkit is a collection of forward looking features made available outside the normal RIA Services release cycle

WCF 4.0 : Discoverable Service

Continuing my WCF 4.0 Tutorials series, in this small article, I have explained the WCF 4.0 Discovery feature. This feature is used when the location of the service gets changed e.g. changing in the hosting environment or changing the physical address of the server etc. With this feature, we can very well say that hosting is not controlled only by the service creator or publisher.

For implementing the WCF discovery feature, the client needs to specify a criteria using which the service address can be looked up for communication. This is a mechanism of probing in which the client defines the criteria and looks for the endpoint. Generally client specifies the Contract for probing. When the client knows about the address of the WCF service, then a call to the service can be made, since the contract information is now available with the client. Let’s see some code.

Step 1: Open VS2010 and create a blank solution, name it as ‘WCF40_Runtime_Discovering_WCF_Services’. In this service, add a WCF Service application, name it as ‘WCF_SampleService’. Rename IService1.cs to IService.cs and Service1.Svc to Service.svc.

Step 2: Add a ServiceContract, OperationContract and DataContract in the IService.cs as below:

WCF Service Contract

Step 3: Implement the IService service contract interface in the Service class as shown below:

WCF GetEmployees

Step 4: Write the following configuration in the web.config file. Add an endpoint with name udpDiscovery in the configuration. This endpoint is used to make the service discoverable. The UdpDiscoveyEndpoint class is used to make the service discoverable. This is a standard preconfigured endpoint for WCF Discoverable services.

WCF Discoverable Config

Step 5: Publish the service on IIS.

Step 6: In the same solution, add a new WPF windows application, name it as ‘WPF40_DynamicClient’. In this client application, add the WCF Service reference. Name the namespace as ‘MyRef’.

Step 7: Open the app.config in the client application, and add the following endpoint in it.

<endpoint name="udpDiscoveryEndpoint" kind="udpDiscoveryEndpoint"/>

Step 8: In the MainPage.xaml, add a button and a DataGrid with AutoGeneratedColumns property set to ‘false’. Also define DataGridColumns as shown below:

image


image

Step 9: In the client project, add a reference to ‘System.ServiceModel.Discovery’. This is required to access the Probing classes. Also use this class in the MainWindow.Xaml.cs as below:

using System.ServiceModel;
using System.ServiceModel.Discovery;

Step 10: In the button click event, now write the following code:
WCF Proxy call

The above code makes use of the following classes:

WCF classes
In the above code, we read the address in the for-each loop, create a proxy object and then make a call to WCF service.

Step 11: Run the application and click on the ‘Get Employees’ button, the DataGrid will show all employees. You will find that the time to display data in the DataGrid is more because since this is probing, it may take time to complete the process for request and response.

image

Note: In a production scenario you can think of making an Async call to the WCF service so that the UI is not blocked till the call completes.

Download the source code

Windows Communication Foundation WCF 4.0 Tutorials

Author and Trainer Mahesh Sabnis has been writing some in-depth WCF 4.0 tutorials that I thought of sharing with all my blog readers. This tutorial series covers some basics and advanced concepts of WCF 4.0 programming

Hope you find this list useful! Please retweet and spread the word. Thanks!

What's New in Windows Communication Foundation (WCF) 4.0 Part- I - VS 2010 and .NET 4.0 has come out with many new features. Typically there are a lot of changes in Windows Workflow 4.0 (WF 4.0), Windows Presentation Foundation (WPF 4.0) and Windows Communication Foundation (WCF 4.0). In this article we will see some new features of WCF 4.0.

What's New in Windows Communication Foundation (WCF) 4.0 Part- II - Developing Routing Service - In part 1 of this article, we went through a new feature of WCF - ‘Simple Configuration’. In this second part of the article series, we will see how WCF 4.0 performs routing.

What's New in Windows Communication Foundation (WCF) 4.0 Part III - Service-Discovery Feature - In the previous two articles, we have gone through important features of WCF 4.0. In this article we will see WCF service discovery features.

Windows Communication Foundation 4.0 - New REST Features - By now most of you might have started exploring the new features of .NET 4.0 and have probably read my previous articles on WCF 4.0.

JSON Enabled WCF 4 Service - 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.

WCF 4.0 REST POX Service with Help Page - Representational State Transfer (REST) support was included in WCF 3.5. In WCF 4.0, this feature has been enhanced with Help page support. This facility was added as WCF REST allows direct call to OperationContracts, using HTTP protocol by the client application. Now the problem is if the WCF service has several OperationContracts, then how does the client application know and distinguish between the urls for method calls?

WCF 4.0: Protocol Bridging using Routing to Protect Internal Business Services - In this article, we are going to discuss protocol bridging mechanism using WCF 4.0 routing features.

Reading Data from WCF Data Service in Silverlight 4 - WCF data Services provides integration with Data Model built using ADO.NET EF. Since the data received is in the form of XML, any client application built on Open standards can easily consume it. In this article, I will demonstrate how it is done using Silverlight 4.

Push Data to the Client using WCF CallBack Service - In WCF, callback is used to implement PUSH mechanism, so that delayed or long running operation results can be automatically pushed back to the client application. WCF actively supports callback to its client, over the instance context established. In this article, we will explore the same

WCF 4: Download Files using WCF REST EndPoints - This article demonstrates how to make use of WCF REST service for downloading files.

Expose WCF 4.0 Service as SOAP and REST - By exposing a WCF service on REST and SOAP, you can make it more accessible to all types of clients. This is especially useful for the clients who can consume the proxy as well as can understand only HTTP communication with XML data processing. This article shows how to do so.

Creating WCF Service using Workflow 4.0 - By now most of you might have started using VS 2010 Beta 2. In this article we will see how to create a WCF service using Workflow 4.0 Beta 2. This article is based on a Beta version and could possibly change in the future. Assuming you have installed VS 2010 Beta 2, Open VS 2010 and create a Workflow project. Following are the activities provided

Creating Routing Service using WCF 4.0, .NET Framework 4.0 and Visual Studio 2010 - The Routing mechanism WCF 4.0 provides an excellent feature for developing SOA based application. Now the client application is free to send request to any WCF service based upon filters.

Implementing Transport Message Security with WCF 4.0 and VS 2010 - This article is Part 1 of a two part series where I will demonstrate how to create a certificate, create a WCF 4.0 service that uses this certificate and finally host a WCF service on IIS 7.5 with SSL. In this article, we will see how to create a certificate and create a WCF service that uses this certificate.

Hosting WCF 4.0 Service on IIS 7.5 with SSL - This article is Part 2 of the two part series where I demonstrate how to create a certificate, create a WCF service that uses this certificate and finally host a WCF service on IIS 7.5 with SSL. In this article, we will see how to host a WCF service we created in Part 1, on IIS 7.5 with SSL.

JSON Enabled WCF 4 Service

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.

Step 1: Open VS2010 and create a new WCF service application, name it as ‘WCF_JSONService’. Open Iservice1.cs and add the following ServiceContract, OperationContract, DataContract. Apply WebGetAttribute on the OperationContract as shown below:

wcf json service

Step 2: Implement the above IService1.cs in the service class:

wcf iservice class

Step 3: Write the Web.Config file as shown below with ‘WebHttpBinding’ and the ‘EndpointBehavior’:

wcf webconfig json

Step 4: Right click on the WCF service and select properties, select ‘Web’ tab and write the following Url in the ‘Use Local IIS Web Server’:

Use Local IIS Server

By using this approach, the WCF service is published on the web Server

Step 5: Open IIS and navigate to the Virtual Directory ‘WCF_JSONService’ created above. Right click on the Service1.Svc and the following result will be displayed:

WCF JSON Service

Now change the url as shown below and hit Enter, the JSON file data will be downloaded as shown below:

JSON File Data

Save the file on disc and view it. The data in the response will be similar to the following:

clip_image001

For demo purposes, I am displaying the Data in a notepad. You can always consume it in any client application like Silverlight, as demonstrated here . The advantage of using JSON over REST is, since REST is XML messaging format, it requires parsing. However this is not required in JSON.

WCF REST Starter Kit Preview 2‏

As on the site
"The WCF REST Starter Kit is a set of features, Visual Studio templates, samples and guidance that enable users to create REST style services using WCF. While the October release of the WCF REST Starter Kit focused on building server-side REST services, the Preview 2 release adds features to make client-side REST development easier. Chief among the new functionality included in Preview 2 is a new class that provides a staged pipeline model for requesting resources over the web. Using this new HTTP client class allows the developer to plug into the various stages of communication to handle custom authentication, caching, and fault handling outside of the client’s application logic."


The goal is to provide a toolset that simplifies building RESTful services today, and to take feedback from the developer community on the features provided in the WCF REST Starter Kit that will shape future REST capabilities in WCF in .NET 4.

You can download the kit from here