foundation (wcf): durable service - preserving state white

13
Big Data Analytics In M2M WHITE PAPER Windows Communication Foundation (WCF): Durable Service - Preserving State

Upload: others

Post on 09-Jun-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Foundation (WCF): Durable Service - Preserving State WHITE

Big Data Analytics In M2M

WHITE PAPER

Windows Communication Foundation (WCF): Durable Service - Preserving State

Big Data Analytics In M2M

WHITE PAPER

Page 2: Foundation (WCF): Durable Service - Preserving State WHITE

2 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Table of Contents

Objective ............................................ Error! Bookmark not defined.

Overview ........................................................................................... 3

Prerequisites ...................................... Error! Bookmark not defined.

Durable Service in Action ................... Error! Bookmark not defined.

Decorating the Service ...................................................................... 6

Configuration Requirements ............................................................. 7

Hosting the Service ........................................................................... 8

Invoking the Service .......................................................................... 8

Reference ........................................................................................ 11

Author Info ......................................... Error! Bookmark not defined.

Page 3: Foundation (WCF): Durable Service - Preserving State WHITE

3 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Objective

In Service Oriented Architecture (SOA), the data provided by the service as well as the maintenance of

the service’s instance at a persistent state is very important. This is especially in cases where the

connection with the client may be lost due to reasons like the client closing the proxy or the server being

shut down, or restarted, etc.

The word ‘stateless’ means that the state of the object in the Windows Communication Foundation

(WCF) service will not be persisted between multiple requests for the service from clients.

HTTP is a stateless protocol, hence any application running on this protocol is not persistent in nature.

NET 3.5 version onwards paves the way to maintain the state in between multiple requests from the

client, or if client and server applications are restarted for any reason.

Another possibility is using the “per session mode” of WCF, but if the client/server application is

recycled or restarted, then the session information will be lost.

Overview Microsoft introduced durable service in .NET 3.5 onwards, in which the state of an object is maintained

by storing the object (after serialization) in a database and by maintaining a unique record by giving it a

unique GUID. This GUID can be shared with the client so that the client can restore the object’s state.

Page 4: Foundation (WCF): Durable Service - Preserving State WHITE

4 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Prerequisites

• Install SQL Server database engine

• Go to C:\WINDOWS\Microsoft.NET\Framework\v3.5\SQL\EN path on your file system. There are

four .sql files

o DropSqlPersistenceProviderLogic.sql

o DropSqlPersistenceProviderSchema.sql

o SqlPersistenceProviderLogic.sql

Contains the stored procedure script for creating the serialized instance in the database, retrieving

the instance, and deleting the instance

o SqlPersistenceProviderSchema.sql

Contains the script for creating the tables and the required schema for storing the object instance.

The name of the table is InstanceData

These scripts get installed by default to the above location.

Durable Service in Action

Let’s create a service from visual studio ’DurableService’ and add reference to the

System.WorkflowServices from the .NET tab as shown in the snapshot below.

Pic 1: Adding System.WorkflowServices to the service application

Now System.ServiceModel.Description namespace is available at the service application.

Page 5: Foundation (WCF): Durable Service - Preserving State WHITE

5 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Let’s define a service contract ICustomerService and one DataContract Employee which consists of

three Datamembers, FirstName, LastName and address in our service.

This Service contract has four OperationContracts - CreateEmployeeObject, AddEmployee,

SaveEmployee and EndPersistence.

Page 6: Foundation (WCF): Durable Service - Preserving State WHITE

6 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Decorating the Service

To make it a durable service in the service file, we need to set two attributes.

Serializable – It indicates that the class CustomerService is serializable.

DurableService - this specifies that the service instance state will be saved after the operation is

completed.

Note: DurableService and DurableOperation attribute will be available only after adding the reference

to System.WorkflowServices is added to the WCF service application

As shown in the above snapshot, we have a generic list object of Employee and which is intialized with

null. There are implementations of all four operation contracts, but each data contract is decorated with

Durable Operation attribute. Here when CreateEmployee object will be called it will initialize an object

“myempobject”.

AddEmployee method will add an employee object into “myempobject”. Let’s consider that

SaveEmployee method will save employee object from myempobject into database. Finally

Page 7: Foundation (WCF): Durable Service - Preserving State WHITE

7 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Endpersistence method will be called to kill persistency in the service.

Here, in the method CreateEmployeeObject, with the Durableoperation attribute, there is a property

CanCreateInstance which is set to true. It specifies that when this method is called from the client, it will

create and store the instance in the persistence medium. (Sqlserver database table).

For CanCreateInsatance to act properly, the method-operation on which it is specified must be a

request-response operation. In this example, the context is “goes” to the client and the durability is

achieved by the client's ability to communicate with the service/server. If the operation is one-way, the

context is “not goes” to the client. Therefore, there must be another way to send the context to the

client. In the same manner, CompletesInstance attribute which is decorated with Endpersistence

method will delete the object instance from the persistence store, i.e. from our database.

Configuration Requirements

Durable service works with the ContextBinding only because a DurableOperationContext is used by the

client to identify a specific service instance. After invoking a request-response operation and receiving a

response from the service, the client will use the InstanceId value(GUID) on each subsequent operation

/method invocation, so that the client maps itself with the correct service instance. If this tracking

mechanism were not used, then, when a service instance is either restarted, shut down or disconnected

from the client, the client would not be able to re-establish a connection to a specific service instance.

1. First define a connection string to the SQL server database in the web.config file as shown in

snapshot below

2. Add persistenceProvider tag as shown in the below snapshot where we need to mention connection

string. In our exmpale it is givn the name “DurableServiceStore”.

Page 8: Foundation (WCF): Durable Service - Preserving State WHITE

8 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

3. Add end points with ContextBinding

Host the Service After configuration has been done, host the service in IIS.

Invoking the Service

Let’s create a console application “HCL_WCFDurableServiceClient” and add a reference to

System.ServiceModel.

Page 9: Foundation (WCF): Durable Service - Preserving State WHITE

9 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Pic 2: Adding System.ServiceModel

Create the proxy class of the service and add to the client application.

In the main method, we are creating an object of the proxy class and initializing an object of

“myempobject” in the server. After that, when “AddEmployee” will be called then instance of the

service created and a new entry will be added to the “InstanceData” table.

Here, the instance value is null, as the Instance state is persisted as binary data. It’s null if the instance is

persisted as text, as we have serializeAsText="true" in the web.config in the PersistenceProvider tag.

Page 10: Foundation (WCF): Durable Service - Preserving State WHITE

10 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

And we have the content of instancexml as below.

In case of non durable service; when the server is restarted, the data with the “myempobject” will be

lost in the server. To see this in durable service, let’s again create an employee object in the client and

add a new employee.

After the execution of ‘AddEmployee’, let’s have a look in the database snapshot. The ‘instancesml’

contains both employee, one added before restarting server, and another after it has been restarted.

Page 11: Foundation (WCF): Durable Service - Preserving State WHITE

11 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

When the client application will call SaveEmployee() , it will return 2 because the server saves both

employee records though the server is restarted. Now, when the client application will call

EndPersistence() , the record will be deleted from the instance table.

Reference http://msdn.microsoft.com/en-us/library/vstudio/bb628514(v=vs.90).aspx

Author Info

Gourik Kumar Bora Gourik is a Technical Lead with more than 7 years experience on Microsoft technologies. He is

instrumental in developing and deploying various Web applications with ASP.net (C#.net & VB.net,

jquery, etc.) with SQL Server and Oracle as database engines. SOA is another area of his expertise. He

works with multiple window services, web services, console applications and form based applications.

Recently, he began developing Sharepoint custom applications using C# and ASP.net and is involved in

‘Simplify’, a knowledge management system from ERS.

Page 12: Foundation (WCF): Durable Service - Preserving State WHITE

12 | © 2015, HCL Technologies. Reproduction Prohibited. This document is protected under Copyright by the Author, all rights reserved.

Navin Saini Navin Saini is a Project Manager at HCL. In the past couple of years, his specific focus area has been

Sharepoint development with both, OOTB (Out of the Box) and custom implementation (using C# and

ASP.net). Using Sharepoint 2007 and 2010, he has been instrumental in developing and deploying

various Sharepoint Portals which include Dashboards and Knowledge Management systems.

Page 13: Foundation (WCF): Durable Service - Preserving State WHITE

Hello, I'm from HCL's Engineering and R&D Services. We enable technology led organizations to go to market with innovative products and solutions. We partner with our customers in building world class products and creating associated solution delivery ecosystems to help bring market leadership. We develop engineering products, solutions and platforms across Aerospace and Defense, Automotive, Consumer Electronics, Software, Online, Industrial Manufacturing, Medical Devices, Networking & Telecom, Office Automation, Semiconductor and Servers & Storage for our customers. For more details contact: [email protected] Follow us on Twitter: http://twitter.com/hclers & LinkedIn: http://lnkd.in/bt8hDXM View our blog-site: http://www.hcltech.com/blogs/engineering-and-rd-services Visit our website: http://www.hcltech.com/engineering-rd-services