Introduction

Hello everyone! This is my first blog on WCF and I hope that you like it.

Today, I will talk about how to implement heart beat mechanism in WCF, so that whenever client is not alive, our WCF service detects it and kills its long running process and saves the server resources for other clients.

Background

Basic knowledge of WCF is required.

Using the Code

IMHO, I think there is no easy way to detect client has been closed or client is abruptly killed somehow.

Now, I am presenting one scenario and the problem in it and at last, the solution for it.

Scenario

Suppose there is one service which takes lots of time to complete, say 1 hour. And in-between, the client of WCF service gets killed.

Problem

Then the WCF service will continue to run till the whole method completes its execution, it means full 1 hour. WCF service does not have mechanism to stop its execution when client is no longer active or alive. It will waste huge server resource and will keep server busy in case resource is scarce on server side.

Solution

Suppose we have a method called “DoWork” that will take 1 hour to complete its execution.

Hide   Copy Code
[ServiceContract]
public interface ILongRunningService
{
[OperationContract]
Output DoWork();
}

Now to resolve the problem statement, we have to refactor this method into the below code snippet.

Hide   Copy Code
[ServiceContract]
public interface ILongRunningServiceV2
{
[OperationContract]
void StartDoWork(); [OperationContract]
Status ContinueWorkAndGetStatusFromServer(); [OperationContract]
Output GetFinalDoWork();
}

Now the method “DoWork” is divided into two parts, one is “StartDoWork” and the second is “GetFinalDoWork”.

The client will now call the first method “StartDoWork” and return immediately, “StartDoWork” will spawn a thread for its execution.

Then client will call “ContinueWorkAndGetStatusFromServer” continuously in while loop with wait (Thread.Sleep) of 1 second. This is required because it will keep the server informed that client is still connected to WCF Service and “DoWork” method execution should not stop. In case client does not call this method “ContinueWorkAndGetStatusFromServer”, then service after few elapsed seconds will stop the execution of “DoWork”.

In case client keeps calling this method and method “DoWork” completes its execution, then the client will call “GetFinalDoWork” to fetch the result from the service.

This implementation is also called heart beat implementation.

Now you can see the code snippet below and download the source code.

And this is the original implementation of method “DoWork”.

Hide   Copy Code
public class LongRunningService : ILongRunningService
{
public Output DoWork()
{
var builder = new StringBuilder();
for (int count = 0; count < 10; count++)
{
builder.Append("Count: " + count);
Thread.Sleep(1000);
}
return new Output(builder.ToString());
}
}

Now, we have created one SessionManger class for managing the session of active client.

Whenever any client connects to our service, it will go through the SessionManager class which will maintain the object of ServiceProvider.

ServiceProvider is our new refactored implementation of method “DoWork”.

Hide   Shrink   Copy Code
public class ServiceProvider
{
private Output _resourceOutput;
private readonly Status _status;
private volatile bool _shouldDisconnect = false;
private Timer _timer;
private DateTime _clientDateTime; public ServiceProvider()
{
this._status = new Status()
{
Message = "Initialized",
OperationStatus = OperationStatus.None
};
} public void StartDoWork()
{
TimerCallback timerCallback = StopActivity;
_clientDateTime = DateTime.Now;
_timer = new Timer(timerCallback, null, 2000, 2000);
Task.Factory.StartNew(this.RunActivity);
} public Status ContinueProcessingAndGetStatus()
{
_clientDateTime = DateTime.Now;
return this._status;
} public Output GetFinalDoWork()
{
if (this._status.OperationStatus == OperationStatus.Ready)
{
return this._resourceOutput;
}
else
{
throw new Exception("Exception");
}
} private void RunActivity()
{
this._status.OperationStatus = OperationStatus.Working; var builder = new StringBuilder(); for (int count = 0; count < 10; count++)
{
if (_shouldDisconnect)
break;
builder.Append("Count: " + count);
Thread.Sleep(1000);
Debug.WriteLine(count);
this._status.Message = count.ToString();
} this._status.OperationStatus = OperationStatus.Ready;
_resourceOutput = new Output(builder.ToString());
} private void StopActivity(object state)
{
TimeSpan span = DateTime.Now - _clientDateTime;
if (span > new TimeSpan(0, 0, 7))
{
this._timer.Dispose();
_shouldDisconnect = true;
}
}
}

This is a SessionManager class.

Hide   Shrink   Copy Code
public static class SessionManager<TSessionHandler> where TSessionHandler
: new()
{
private static readonly ConcurrentDictionary<string, TSessionHandler>
SessionHandlers = new ConcurrentDictionary<string, TSessionHandler>(); private static readonly object SyncObject = new object(); public static TSessionHandler GetSession()
{
var sessionId = OperationContext.Current.SessionId;
if (SessionHandlers.ContainsKey(sessionId))
{
return SessionHandlers[sessionId];
}
else
{
lock (SyncObject)
{
TSessionHandler handler = new TSessionHandler();
SessionHandlers.TryAdd(sessionId, handler);
return handler;
}
}
}
}

This is client code, the method “WithNormalService” is an old way of calling and method “WithVersion2Service” is a new way of calling service.

Hide   Shrink   Copy Code
static void WithNormalService()
{
LongRunningServiceClient client = new LongRunningServiceClient();
var result = client.DoWork();
Console.WriteLine(result.FinalValue);
} static void WithVersion2Service()
{
LongRunningServiceV2Client client = new LongRunningServiceV2Client();
client.StartDoWork();
string serviceMsg = string.Empty;
Status status = client.ContinueWorkAndGetStatusFromServer(); Console.WriteLine(status.OperationStatus);
do
{
if (!string.IsNullOrEmpty(status.Message))
{
if (!serviceMsg.Equals(status.Message))
{
serviceMsg = status.Message;
Console.Out.WriteLine(status.Message);
}
} Thread.Sleep(500); status = client.ContinueWorkAndGetStatusFromServer(); } while (status.OperationStatus != OperationStatus.Ready); ServiceReferenceV2.Output image = client.GetFinalDoWork(); Console.WriteLine(image.FinalValue);
}

Earlier, this was the flow of calling between client and service.

Client ---> DoWork

Now after the implementation of Heartbeat in WCF Service, this is the new way of calling WCF Service.

Client ---> StartDoWork

Client ---> ContinueWorkAndGetStatusFromServer in a while loop till it get Operation Status Ready, then it will call GetFinalDoWork.

Client ---> GetFinalDoWork

Point of Interest

One improvement point in this code is to remove the dead client references from the dictionary in SessionManager class.

Detecting Client Connection in WCF Long Running Service (Heartbeat Implementation) z的更多相关文章

  1. wcf和web service的区别

    1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单 ...

  2. WCF和Web Service的 区(guan)别(xi)

    参考文献:http://social.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice 之前 ...

  3. ORA-12518,TNS:listener could not hand off client connection

    前几天在启动应用的时候,在控制台抛出了此异常信息!很明显是数据库方面的问题,不过具体是什么问题哪?百度了一下,网上关于此问题的信息还是有比较多,从异常的提示中我们也能看到是具体是和客户端的连接相关的问 ...

  4. 本地运行项目成功 ,但在服务器运行程序就会报Failed to establish a new connection: [Errno -2] Name or service not known

    equests.exceptions.ConnectionError: HTTPSConnectionPool(host=): Max retries exceeded with url: /appa ...

  5. 扩展Wcf call security service, 手动添加 Soap Security Head.

    有次我们有个项目需要Call 一个 Java 的 web service, Soap包中需要一个 Security Head <soapenv:Header> <wsse:Secur ...

  6. Entity Framework + WCF REST JSON Service

    利用EF 和WCF 建立一个REST JSON Service. 首先我们要下载一个Visual Studio 的Template 叫 "ADO.NET C# POCO Entity Gen ...

  7. WCF - Versus Web Service

    There are some major differences that exist between WCF and a Web service which are listed below. 这里 ...

  8. WCF: Retry when service is in fault state.

    Service Host: using System; using System.Configuration; using System.ServiceModel; using System.Serv ...

  9. WCF宿主Window Service Demo

    尝试了下将服务寄宿在window 服务上.具体步骤如下 整个解决方案截图 一.创建window 服务 Wcf.WinService namespace Wcf.WinService { public ...

随机推荐

  1. HTTPS和SSL详解

    这是转载别人的(转:崔永秀) 把这几天学习到的关于ssl和https协议的内容在这里分享一下,适合一些像我一样的网络协议初学者. ssl协议的起源和历史我就不再多说了,就是那个Netscape 网景公 ...

  2. 开启Unity项目中VS工程的属性面板

    https://www.cnblogs.com/CodeGize/p/7859656.html Unity生成的VS工程一般是无法打开属性面板的.但是事实上,如果使用VS for unity,通过配置 ...

  3. 【LESS系列】一些常用的Mixins

    在我们平时的开发中,对于一些使用频率很高的方法函数,我们一般都会将其归纳到一起,整理出一个核心库来. 其实这个思想,借助 LESS 也可以在 CSS 中得以实现. 下面是几个在 W3CPLUS 中偷过 ...

  4. 通过反射获取及调用方法(Method)

    1.获取方法使用反射获取某一个类中的方法,步骤:①找到获取方法所在类的字节码对象②找到需要被获取的方法 Class类中常用方法: public Method[] getMethods():获取包括自身 ...

  5. freemarker实现通用布局的模板拆分与复用

    原文:http://www.hawu.me/coding/733 一.基础页面布局 假设我们项目页面的通用布局如下图所示: 实现这样的布局的基本html代码如下:           XHTML   ...

  6. Scrum 冲刺博客第二篇

    一.当天站立式会议照片一张 二.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中 昨天已完成的工作 配置和连接微信小程序服务器 个人界面设计 部主页界面设计 答题界面设计 今 ...

  7. android添加系统(服务、应用)

    1. 添加系统服务 1.1 添加方式1:(不加入servicemanager统一管理的) 看Android6.0.1 init.rc解析中的第2章和第3章 方式1: 1). 写一个测试脚本test.s ...

  8. Golang教程:方法

    什么是方法 一个方法只是一个函数,它有一个特殊的接收者(receiver)类型,该接收者放在 func 关键字和函数名之间.接收者可以是结构体类型或非结构体类型.可以在方法内部访问接收者. 通过下面的 ...

  9. php中array_walk() 和 array_map()两个函数区别

    两个函数的共性和区别: 1.传入这两个函数的 $value,就是数组中的单一个元素. 2.array_walk() 仅返回true或者false,array_map() 返回处理后的数组: 3.要得到 ...

  10. C#,动态加载DLL,通过反射,调用参数,方法,窗体

    .net中常会用到动态加载DLL,而DLL中可能包含各种参数.方法.窗体,如何来调用动态加载这些参数.方法.窗体呢? 在C#中,我们要使用反射,首先要搞清楚以下命名空间中几个类的关系: System. ...