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. week3b:个人博客作业

    vs2013的初步使用 由于自己在写这篇博客的时候已经把vs2013安装完毕,就我写了. 这是2013的使用步骤 第一步 注释 箭头指的方向是“点击” 第二步 第三步 第四步 第5步 第六步 最后点击 ...

  2. [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)

    1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...

  3. Enterprise Library 6.0 参考源码索引

    http://www.projky.com/entlib/6.0/Diagnostics/Tracing/DiaLib.cs.htmlhttp://www.projky.com/entlib/6.0/ ...

  4. form表单转化json对象

    利用 $.fn 可以让每一个jquery 对象都能直接使用这个方法. //form表单转化json对象$.fn.serializeObject = function () { var o = {}; ...

  5. Git查看与修改用户名、邮箱(转载)

    用户名和邮箱的作用: 用户名和邮箱地址相当于你的身份标识,是本地Git客户端的一个变量,不会随着Git库而改变. 每次commit都会用用户名和邮箱纪录. github的contributions跟你 ...

  6. Beta阶段DAY2

    一.提供当天站立式会议照片一张 二.每个人的工作 1.讨论项目每个成员的昨天进展 刘阳航:删除多余按钮,调整界面. 林庭亦:删除麻烦的颜色设置. 郑子熙:添加新增按钮. 陈文俊:重新规划面板及功能. ...

  7. vue开发完成后打包后图片路径不对

    用vue做了一个小的移动端项目,从头到尾做下来,感觉自己好多东西都没弄清楚过.也学到了很多,已整理笔记在自己电脑上,但是比较零散,空了再来仔细整理整理. 于是,上周五模拟好数据(接口还未写),准备打包 ...

  8. Robot Framework 教程 (2) - 页面元素定位

    上一篇文章中,简单模拟了一个baidu搜索并截图的过程,在搜索过程中,我们需要对搜索框.搜索按钮进行定位.本篇文章主要介绍一下具体的定位方法. 我们的脚本如下: *** Settings *** Li ...

  9. UVAlive4287_Proving Equivalences

    题意是告诉你有n个命题,m条递推关系,表示某个命题可以推出另外一个命题. 现在问你至少在增加多少个递推关系可以保证所有命题两两互推. 命题为点,关系为有向边,题目转化成为至少增加多少条有向边使得整个图 ...

  10. 跳转不同包时候 需要先指定该包的namespace 注意 先跳转 即加上/