WCF: Retry when service is in fault state.
Service Host:
using System;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web.Configuration;
using log4net;
using System.Threading; namespace Test.Custom
{
public class CustomServiceHostFactory : System.ServiceModel.Activation.WebServiceHostFactory
{
private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
log4net.Config.XmlConfigurator.Configure(); CustomServiceHost customServiceHost = new CustomServiceHost (serviceType, baseAddresses);
log.Info("Create Service Host called"); customServiceHost.incomingServiceType = serviceType;
customServiceHost.incomingBaseAddresses = baseAddresses;
customServiceHost.Faulted += customServiceHost.OnServiceHostFaulted; return customServiceHost;
}
} public class CustomServiceHost : ServiceHost
{
public Type incomingServiceType { get; set; }
public Uri[] incomingBaseAddresses { get; set; } private int Attempts = ;
private bool isSuccess = false; private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public CustomServiceHost (Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
log.Info("CustomServiceHost constructor called");
} protected override void ApplyConfiguration()
{
base.ApplyConfiguration();
} public void OnServiceHostFaulted(object sender, EventArgs e)
{
log.Info("Host state is fault.");
ICommunicationObject faultedHost = (ICommunicationObject)sender;
faultedHost.Abort(); faultedHost = new ServiceHost(this.incomingServiceType, this.incomingBaseAddresses);
//faultedHost.Faulted += this.OnServiceHostFaulted; log.Info("Host state is " + faultedHost.State.ToString()); if (Attempts < && isSuccess == false)
{
Attempts++;
log.Info("Retry to create server host for the" + Attempts + "time");
try
{
if (isSuccess == false)
{
Thread.Sleep( * );
}
faultedHost.Open();
isSuccess = true;
}
catch (Exception ex)
{
faultedHost.Abort();
log.Info("Host state is " + faultedHost.State.ToString());
log.Info("Exception: " + ex.Message);
}
}
} //protected override void OnFaulted()
//{
// base.OnFaulted();
//} }
}
CustomServiceHostFactory.cs
1. 创建一个自定义的HostFactory,继承自WebServiceHostFactory;
2. 重载CreateServiceHost方法,并在方法内添加ServiceHost对象的Faulted的EventHandler处理;
3. 对自定义Faulted EventHandler方法根据需要加入Retry。
Service Client:
using Microsoft.ServiceBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace Test.WCF.ServiceBus.Client
{
class Program
{
public static ChannelFactory<IMediaServicesManagementServiceChannel> testCF=null;
static void Main(string[] args)
{ Retry(
() =>
{
bool flag = false;
var cf = new ChannelFactory<IMediaServicesManagementServiceChannel>(
new NetTcpRelayBinding(),
new EndpointAddress(ServiceBusEnvironment.CreateServiceUri("sb", "***Service Bus Name***", "***Relay Name***"))); cf.Endpoint.Behaviors.Add(new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedSecretTokenProvider("owner", "***Service Bus Token***") }); var ch1 = cf.CreateChannel(); try
{
Console.WriteLine(ch1.AddNumbers(, ));
flag = true;
}
catch
{
ch1.Abort();
flag = false;
}
finally
{
if (ch1 != null)
{
ch1.Close();
}
}
return flag;
},
); Console.WriteLine("Press ENTER to close");
Console.ReadLine();
} public static void Retry(Func<bool> task, int times)
{
bool flag = false; for (int i = ; i < times; i++ )
{
flag = task(); if(flag==true)
{
break;
}
}
} }
}
Program.cs
Referece Links:
http://msdn.microsoft.com/en-us/library/windowsazure/hh966775.aspx
WCF: Retry when service is in fault state.的更多相关文章
- wcf和web service的区别
1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单 ...
- WCF和Web Service的 区(guan)别(xi)
参考文献:http://social.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice 之前 ...
- 扩展Wcf call security service, 手动添加 Soap Security Head.
有次我们有个项目需要Call 一个 Java 的 web service, Soap包中需要一个 Security Head <soapenv:Header> <wsse:Secur ...
- Detecting Client Connection in WCF Long Running Service (Heartbeat Implementation) z
Download source - 45.3 KB Introduction Hello everyone! This is my first blog on WCF and I hope that ...
- 使用WCF 创建 Rest service
REST SERVICE 允许客户端修改url路径,并且web端功过url 请求数据. 他使用http协议进行通讯,想必大家都知道 . 并且我们可以通过设置进行数据类型转换, 支持XML,JSON 格 ...
- 在IIS8.5的环境下配置WCF的Restful Service
今天在客户的环境中(Windows Server 2012 R2 + IIS 8.5)搭建Call WCF Restful Service的功能,发现了几个环境配置的问题,记录如下: 1):此环境先安 ...
- Entity Framework + WCF REST JSON Service
利用EF 和WCF 建立一个REST JSON Service. 首先我们要下载一个Visual Studio 的Template 叫 "ADO.NET C# POCO Entity Gen ...
- WCF - Versus Web Service
There are some major differences that exist between WCF and a Web service which are listed below. 这里 ...
- Web API、WCF和Web Service的区别
[转载] Web Service 1.它是基于SOAP协议的,数据格式是XML 2.只支持HTTP协议 3.它不是开源的,但可以被任意一个了解XML的人使用 4.它只能部署在IIS上 WCF 1.这个 ...
随机推荐
- C++编译与链接(1)-编译与链接过程
大家知道计算机使用的一系列的1和0 那个一个C++语言程序又是如何从一个个.h和.cpp文件变成包含1和0的可执行文件呢? 可以认为有以下的几个环节 源程序->预处理->编译和优化-> ...
- C++编译与链接(2)-浅谈内部链接与外部链接
发现每次写技术博客时,都会在文章开头处花费一番功夫 ...从前,有一个程序员....他的名字叫magicsoar 为什么有时会出现aaa已在bbb中重定义的错误? 为什么有时会出现无法解析的外部符号? ...
- 定时任务中的备份不同的数据库中的所有的表,每个表使用单独的sql备份文件
#! /bin/bash # 指定用户 USER=root # 指定密码 PASS=123456 # 指定主机地址 HOST=localhost # 指定备份的目录 BACKUP=/backup/sq ...
- Idea安装Python插件并配置Python SDK
第一步:在help/about中查看IDEA版本,作者IDEA 15.0.2 第二步:在http://plugins.jetbrains.com/plugin/631中下载python-143.116 ...
- date format 参数表
format 必需.规定输出日期字符串的格式.可使用下列字符: d - 一个月中的第几天(从 01 到 31) D - 星期几的文本表示(用三个字母表示) j - 一个月中的第几天,不带前导零(1 到 ...
- Linux_Nginx 安装
官网:http://nginx.org/ 1.下载http://nginx.org/download/nginx-1.14.0.tar.gz 2.查看详情 [zwesy@localhost ~]$ l ...
- iOS 数据库sqlite完整增删改查操作
1: 创建数据库表格 1.1 — 表格创建使用一个数据库软件快速创建:软件大小14.3M; 下载地址:http://pan.baidu.com/s/1qWOgGoc; 表格创建-> 打开软件,点 ...
- springmvc+mybatis 处理时间
项目结构: 一.数据库中time的字段为datetime1. 数据库设计如图 2. addNews.jsp <%@ page language="java" contentT ...
- 【大数据】MapTask并行度和切片机制
一. MapTask并行度决定机制 maptask的并行度决定map阶段的任务处理并发度,进而影响到整个job的处理速度 那么,mapTask并行实例是否越多越好呢?其并行度又是如何决定呢? 1.1 ...
- 简明Python教程自学笔记——命令行通讯录
[前言]学习Python已经有一段时间了,相关的书籍资料也下载了不少,但是没有一本完整的看完,也没有编出一个完整的程序.今天下午比较清闲就把<简明Python教程>看了一遍,然后根据书里面 ...