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. express框架实现承载静态页面的能力

    我们知道nodejs本身不具有一个web容器的作用,不像tomcat或者IIS这样的服务器一样天然具有web容器承载静态动态页面的能力,如果要原生实现的话需要自己通过路由配置,比较麻烦,而expres ...

  2. Enterprise Library 6.0 参考源码索引

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

  3. IT行业的个人见解

    IT这个行业是近代历史上的新新行业,它的就业前景是非常的好的,就业率高,但是这个行业的需求人才精英不是那些半桶水的所谓IT男.我现在学习的是计算机专业中的软件工程目标是成为一名合格的软件工程师,软件工 ...

  4. 每日站立会议——敏捷流程scrum实践

    每日站立会议是敏捷流程scrum中的很重要的一个制度之一. 功能:     1.快速同步进展,让项目组内部的员工互相了解彼此的进展,从而了解本项目的整体进展.      2.给每个人一种精神压力,信守 ...

  5. Robot Framework 教程 (5) - 连接Oracel数据库

    Robot Framework 提供了多种Library.其中Database Library可用来连接操作数据库. 1.安装Database Library 打开Robot Framework官网, ...

  6. CMD命令去导出文件下的文件名称到EXCEL

      dir C:\Users\caire\Pictures\壁纸/b>E:\temp.xls

  7. pycharm 修改新建文件时的头部模板

    pycharm 修改新建文件时的头部模板 默认为__author__='...' [省略号是默认你的计算机名] 修改这个作者名的步骤: 依次点击:File->Settings->Edito ...

  8. Js获取上一月份

    new Date(new Date().setMonth(new Date().getMonth() - 1))

  9. yaf windows安装

    1.需要先下载 php_yaf模块.地址(http://pecl.php.net/package/yaf/2.3.2/windows) 看清你的php版本,然后在phpinfo中看Achitectur ...

  10. PHP时间格式化参数表笔记

    date_create_from_format() 函数返回一个根据指定格式进行格式化的新的 DateTime 对象.通常需要配合date_format()函数使用 语法: date_create_f ...