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. EF 小数位的保留

    问题描述:当采用EF的DbContext保存decimal类型数据到数据库,默认只会保存小数点后的前2位小数,其余均置0:例如保存101.182352152322,实际存到数据库里的数据为101.18 ...

  2. 微信小程序wx:for和wx:for-item的正确用法

    wx:for="{{list}}"用来循环数组,而list即为数组名wx:for-item="items" 即用来定义一个循环过程中每个元素的变量的 如果是一维 ...

  3. java中的Serializable接口的作用

    实现java.io.Serializable 接口的类是可序列化的.没有实现此接口的类将不能使它们的任一状态被序列化或逆序列化. 序列化类的所有子类本身都是可序列化的.这个序列化接口没有任何方法和域, ...

  4. windows多线程(一) 创建线程 CreateThread

    一 线程创建函数 CreateThread 修改说明: 这里 说了另一种创建线程方法,使用_beginthreadex()更安全的创建线程,在实际使用中尽量使用_beginthreadex()来创建线 ...

  5. [转贴]Linux的SUID SGID 等知识内容

    作者:sparkdev 出处:http://www.cnblogs.com/sparkdev/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, ...

  6. SGU326_Perspective

    NBA打比赛.所有的比赛被分为多个团队.有的比赛是团内的,有的是与团外的队伍打的. 给出团内每个队伍已得分,以及总共有多少场比赛,还有团内所有队伍之间有多少场比赛? 问1队是否可能是分数最高的一个队伍 ...

  7. python自动化之读写

    ############################################################################# #############在Windows上 ...

  8. bzoj2095-Bridge

    题意 一个 \(n\) 个点 \(m\) 条边的图,每条边双向都有权值(可能不一样).求从 1 开始,经过所有点,经过所有边一次且仅一次(即一定要经过这条边的某个方向)回到 1 的路径上权值最大的最小 ...

  9. AtCoder Grand Contest 029 翻车记

    A:对于每个B,会和其右边的每个W交换一次. #include<iostream> #include<cstdio> #include<cmath> #includ ...

  10. P4254 [JSOI2008]Blue Mary开公司

    题面 这道题的意思就是给出若干个一次函数,当\(x=x_0\)时,最大的\(y\)为多少 这种题可以用李超线段树来处理 什么是李超线段树呢? 李超线段树存储的是在区间上方暴露最多的直线标号,为了便于描 ...