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.的更多相关文章

  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. 扩展Wcf call security service, 手动添加 Soap Security Head.

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

  4. 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 ...

  5. 使用WCF 创建 Rest service

    REST SERVICE 允许客户端修改url路径,并且web端功过url 请求数据. 他使用http协议进行通讯,想必大家都知道 . 并且我们可以通过设置进行数据类型转换, 支持XML,JSON 格 ...

  6. 在IIS8.5的环境下配置WCF的Restful Service

    今天在客户的环境中(Windows Server 2012 R2 + IIS 8.5)搭建Call WCF Restful Service的功能,发现了几个环境配置的问题,记录如下: 1):此环境先安 ...

  7. Entity Framework + WCF REST JSON Service

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

  8. WCF - Versus Web Service

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

  9. Web API、WCF和Web Service的区别

    [转载] Web Service 1.它是基于SOAP协议的,数据格式是XML 2.只支持HTTP协议 3.它不是开源的,但可以被任意一个了解XML的人使用 4.它只能部署在IIS上 WCF 1.这个 ...

随机推荐

  1. Java 线程间通讯

    /* 线程间通讯: 多个线程在处理同一资源,但是任务却不同. */ package com.cwcec.test; class Input implements Runnable { Resource ...

  2. 字符串拆分函数 func_splitstr

    create type str_split is table of varchar2(4000) ; 1 CREATE OR REPLACE FUNCTION splitstr(p_string IN ...

  3. PHP 常用函数总结(一)

    重点: 看函数的 ‘参数’ 和 ‘返回值’ 文档:http://www.w3school.com.cn/php/php_ref_array.asp http://www.w3school.com.cn ...

  4. [转帖] 固定硬盘接口 U.2和M.2

    U.2接口 U.2接口别称SFF-8639,是由固态硬盘形态工作组织(SSD Form Factor Work Group)推出的接口规范.U.2不但能支持SATA-Express规范,还能兼容SAS ...

  5. linux 负载均衡配置 keepalive lvs 使用nginx转发 CentOS7 搭建LVS+keepalived负载均衡

    最近希望能够配置一下负载均衡,在虚拟机上面,但是网上找了很多资料很零散,对于不了解的人,很多不够详细,最近终于做好了,把具体的步骤写下来,方便各位网友查阅学习 这个实验需要安装nginx如果没有安装过 ...

  6. Python动态规划求解最长递增子序列(LIS)

    原始代码错误,移步博客查看O(N^2)及优化的O(N*logN)的实现:每天一道编程题--最长递增子序列

  7. HDU4745——Two Rabbits——2013 ACM/ICPC Asia Regional Hangzhou Online

    这个题目虽然在比赛的时候苦思无果,但是赛后再做就真的是个水题,赤果果的水题. 题目的意思是给n个数构成的环,两只兔子从任一点开始分别顺逆时针跳,每次可以调到任意一个数(最多不会跳过一圈). 求最多能跳 ...

  8. 80X86计算机组织

    计算机主要由运算器.控制器.存储器.和输入输出设备构成. 主频: 主频是指芯片所用的主时钟频率,它直接影响计算机的运行速度,由于处理器体系结构的差别,同样的主频可能产生不同的计算速度,但主频仍然是反映 ...

  9. oracle 慢查询

    一.查询执行最慢的sql select * from (select sa.SQL_TEXT, sa.SQL_FULLTEXT, sa.EXECUTIONS "执行次数", , ) ...

  10. C++模板学习笔记

    一个有趣的东西:实现一个函数print, 输入一个数组, 输出数组的各个维度长度. eg. ], b[][], c[][][]; print(a); //(2, 4) print(b); //(3, ...