Service Host:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel; namespace Test.WCF.ServiceBus.Host.Service
{
[ServiceContract(Namespace = "urn:ps")]
interface IProblemSolver
{
[OperationContract]
int AddNumbers(int a, int b);
} interface IProblemSolverChannel : IProblemSolver, IClientChannel { }
}

IProblemSolver.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test.WCF.ServiceBus.Host.Service
{
class ProblemSolver : IProblemSolver
{
public int AddNumbers(int a, int b)
{
return a + b;
}
}
}

ProblemSolver.cs

using Microsoft.ServiceBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using Test.WCF.ServiceBus.Host.Service; namespace Test.WCF.ServiceBus.Host
{
class Program
{
static void Main(string[] args)
{
ServiceHost sh = new ServiceHost(typeof(ProblemSolver)); sh.AddServiceEndpoint(
typeof(IProblemSolver), new NetTcpBinding(),
"net.tcp://localhost:9358/***Relay Name***"); sh.AddServiceEndpoint(
typeof(IProblemSolver), new NetTcpRelayBinding(),
ServiceBusEnvironment.CreateServiceUri("sb", "***Service Bus Name***", "***Relay Name***"))
.Behaviors.Add(new TransportClientEndpointBehavior
{
TokenProvider = TokenProvider.CreateSharedSecretTokenProvider("owner", "***Service Bus Token***")
}); sh.Open(); Console.WriteLine("Press ENTER to close");
Console.ReadLine(); sh.Close();
}
}
}

Program.cs

Service Client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel; namespace Test.WCF.ServiceBus.Client
{
[ServiceContract(Namespace = "urn:ps")]
interface IProblemSolver
{
[OperationContract]
int AddNumbers(int a, int b);
} interface IProblemSolverChannel : IProblemSolver, IClientChannel { }
}

IProblemSolver.cs

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

这里在client端加入了Retry的逻辑以用于由于网络不稳定或其他原因造成的Host Service Faulted State的情形。

同时要注意,WCF创建的Channel是Fault State时,在调用.Close()之前, 要先调用.Abort().

Reference Links:

http://jeffbarnes.net/blog/post/2007/04/24/wcf-your-proxy-can-only-fault-once.aspx

http://blog.tallan.com/2009/05/06/recover-from-a-wcf-service-fault/

Windows Azure: Service Bus Relay的更多相关文章

  1. Windows Azure Service Bus (6) 中继(Relay On) 使用VS2013开发Service Bus Relay On

    <Windows Azure Platform 系列文章目录> 注意:本文介绍的是国内由世纪互联运维的Windows Azure服务. 项目文件请在这里下载. 我们在使用Azure平台的时 ...

  2. Windows Azure Service Bus Topics实现系统松散耦合

    前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...

  3. Windows Azure Service Bus Notification Hub推送通知

    前言 随着Windows Azure 在中国的正式落地,相信越来越多的人会体验到Windows Azure带来的强大和便利.在上一篇文章中, 我们介绍了如何利用Windows Azure中的Servi ...

  4. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  5. Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue

    <Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...

  6. Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别

    <Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...

  7. Windows Azure Service Bus (5) 主题(Topic) 使用VS2013开发Service Bus Topic

    <Windows Azure Platform 系列文章目录> 项目文件,请在这里下载 在笔者之前的文章中Windows Azure Service Bus (1) 基础 介绍了Servi ...

  8. Windows Azure Service Bus (1) 基础

    <Windows Azure Platform 系列文章目录> 我们在基于Windows Azure进行云端开发的时候,云端的软件通常都需要与其他软件进行交互.这些其他软件可能包括其他In ...

  9. Windows Azure Service Bus 推动财务服务门户的高可用性和可伸缩性

    抵押贷款公司和评估管理公司面临着快速.复杂且数据量极大的业务流程.他们需要可快速.轻松设置且容量几乎无限的可伸缩的企业级服务,来对处理评估订单以及自动化流程本身所产生的所有文档和数据进行管理. 这听起 ...

随机推荐

  1. A网站访问B网站,跨域问题

    跨域异常:XMLHttpRequest cannot load  ''. No 'Access-Control-Allow-Origin' header is present on the reque ...

  2. mongodb的命令介绍

    db.help() 查看库级别的命令 db.stats() 查看数据库状态 db.version() 查看数据库版本 db.serverStatus() 查看数据库服务器状态 db.mycoll.he ...

  3. excel表格如何限制单元格输入内容

    一天一天实在太快,周六了~~~ 测试任务不太紧的时候就可以着手开始整理本月的测试项目,选择的是excel,清晰明了. 原来excel的功能远比我们想象的强大与好用,今天先介绍如何限制单元格内容: 如下 ...

  4. excel的常用技巧

    如何将EXCEL表中SHEET的名字导出 (一)office的操作方法   按下ATL+F11 菜单:插入-模块 复制下面代码,然后按F5运行.会在最前面加张总表,显示工作表名称. Sub mulu( ...

  5. python读取文件解码失败

    python2.7 urllib2 抓取新浪乱码 中的: 报错的异常是 UnicodeDecodeError: 'gbk' codec can't decode bytes in position 2 ...

  6. input accept 属性

    *.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video *.ac3 audio/ac3 AC3 Audio *.asf allpication/vnd.ms-as ...

  7. delphi的ActionToolBar控件的使用 [问题点数:50分]

    delphi那些默认图标在哪里就是那些Text,Label,Checkbox,显示在palette上面的那些图标. 上面的按钮是在ActionManager中添加的,让后拖到ActionToolBar ...

  8. JAVAScript对象及初始面向对象

              javaScript对象及初始面向对象 1:内置对象 例:Date String Array 类等... 2:自定义对象 方法1:var newObj=new Object(); ...

  9. 自定义样式,使用浏览器阅读epub格式的电子书

    epub格式的电子式一般用专门的阅读器打开,但是如果可以使用浏览器打开,就可以随意更改css了,获得极致的体验效果. 比如可以自定义字体.行间距.背景色.字体大小.缩进等等... 当然,如果您不需要添 ...

  10. 为什么有时候访问某些加密https网站是不需要证书的? https? ssl?

    根证书是CA颁发给自己的证书, 是信任链的起点 1.所有访问https的网站都是需要证书的. 2.对于某些网站,尤其是证书颁发机构的网站,操作系统自动添加了这些网站访问需要的证书到证书管理器中,所以就 ...