Windows Azure AppFabric 入门教学系列 (二):一个简单的Service Bus例子
2012-03-22 11:58:06 来源:WEB开发网右击Service项目,Add->new Item
选择Application Configuration File。将App.config改为如下:
<configuration>
<system.serviceModel>
<services>
<service name="Service.EchoService">
<endpoint contract="Service.IEchoContract"
binding="netTcpRelayBinding" />
</service>
</services>
</system.serviceModel>
</configuration>
客户端:
7.与创建Service项目一样,创建一个新项目,命名为Client。
8.参考步骤2,为Client项目加入System.ServiceModel以及Microsoft.ServiceBus.dll程序集。
9.参考步骤3,添加IEchoContract服务契约,代码如下:
using System;
//应用WCF程序集
using System.ServiceModel;
//将接口定义为WCF的服务契约
namespace Client
{
[ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IEchoContract
{
[OperationContract]
string Echo(string text);
}
}
10.使用通过AppFabric Service Bus托管的WCF服务
打开Client项目的Program.cs
加入引用:
using System.ServiceModel; using Microsoft.ServiceBus;
在Main函数中:
static void Main(string[] args)
{
Console.Title = "Client";
Console.Write("Your Service Namespace Domain (e.g. sb://<YOUR-NAMESPACE>.servicebus.windows.net/): ");
string serviceNamespaceDomain = Console.ReadLine();
Console.Write("Your Issuer Name: ");
string issuerName = Console.ReadLine();
Console.Write("Your Issuer Secret: ");
string issuerSecret = Console.ReadLine();
//基于服务命名空间来创建服务URI
Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespaceDomain, "EchoService");
//为端点(endpoint)创建凭据对象( credential object)
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;
sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;
// 创建读取配置文件的信道工厂( channel factory)
ChannelFactory<IEchoContract> channelFactory = new ChannelFactory<IEchoContract>("RelayEndpoint", new EndpointAddress(serviceUri));
// 应用Service Bus 凭证
channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
// 创建并打开客户端信道
IEchoContract channel = channelFactory.CreateChannel();
((ICommunicationObject)channel).Open();
Console.WriteLine("Enter text to echo (or [Enter] to exit):");
string input = Console.ReadLine();
while (input != String.Empty)
{
try
{
Console.WriteLine("Server echoed: {0}", channel.Echo(input));
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
input = Console.ReadLine();
}
((ICommunicationObject)channel).Close();
channelFactory.Close();
}
11.参考步骤6,为Client项目新建App.config,修改如下:
<configuration>
<system.serviceModel>
<client>
<!-- Application Endpoint -->
<endpoint name="RelayEndpoint"
contract="Client.IEchoContract"
binding="netTcpRelayBinding"/>
</client>
</system.serviceModel>
</configuration>
验证:
至此,代码已经创建完毕,我们已经可以进行调试了。
1.右击Service项目,Debug->Start new Instance。(按F5也可)
按照Console提示输入您在Windows Azure platform AppFabric portal处所注册得到的信息。
Service Namespace Domain,Issuer Name 和Issuer Key的具体位置请参照下图。
2.右击Client项目,Debug->Start new Instance启动另一调试实例,同样按照Console提示输入信息。
最终效果如图:
探析:
本示例简单的展示了如何通过Service Bus暴露服务以便让客户端访问。假设我们在公司内部运行服务器端程序,我们能够在任何可以访问Internet的机器上运行客户端程序。无论客户端身处何种网络结构,都能通过该URI访问到服务。在本例中该URI会成为如下形式:sb://<serviceNamespace>.servicebus.windows.net/EchoService/
Service Bus为我们应用程序之间的通信提供了桥梁,我们无需编写和维护复杂的代码。
本示例程序中还有另外一个重要概念便是程序通信时的绑定(binding)方式。这原本是WCF中的概念,但是ServiceBus对其进行了扩展,本例使用的NetTcpRelayBinding便是拓展的binding之一。NetTcpRelayBinding能够使你暴露出通过URI便能访问的端点。当使用NetTcpRelayBinding 的端点打开时,其URI便被注册于ServiceBus,任何使用相同类似配置的授权客户便能向该URI发送信息。这样,Service Bus便成为了那2个端点之间的中转。
目前为止,我们的教程都是基于Azure付费账户的。在下一篇教程中我们会介绍LABS环境。LABS提供了免费的使用环境。这对于国内用户和开发者来说会带来极大的便利。敬请期待我们的下一篇教程。好了,大家赶快动手试一下,进入奇妙的AppFabric世界吧!
更多精彩
赞助商链接
