《微软云计算Windows Azure开发与部署权威指南》——6.7 AppFabric服务总线REST的服务开发

简介:

本节书摘来自异步社区《微软云计算Windows Azure开发与部署权威指南》一书中的第6章,第6.7节,作者: 尹成 , 郝庭毅 , 张俊强 , 孙奉刚 , 寇睿明 更多章节内容可以访问云栖社区“异步社区”公众号查看。

6.7 AppFabric服务总线REST的服务开发

微软云计算Windows Azure开发与部署权威指南
本节介绍如何建立一个简单的服务总线主应用程序,使该程序公开一个基于REST的访问接口。任一台Web客户端,比如浏览器,都可以使用HTTP请求访问服务总线API。本示例使用的是WCF REST编程模型在服务总线上构建REST服务。

1.步骤一:注册账户
① 在Windows Azure门户创建一个服务命名空间。可参考本章6.2小节的内容。

② 在Windows Azure Management门户主窗口中单击选中创建的命名空间。

③ 在左侧的“Properties”面板中找到“Default Key”入口。

④ 在“Default Key”中单击“View”,记下或复制密钥,以在之后的操作中使用。

2.步骤二:定义一个基于REST的服务契约
① 以管理员身份运行Visual Studio 2010,选择新建工程,选择Visual C#,创建一个控制台应用程序,命名为“ImageListener”,如图6-68所示。

② 添加对System.ServiceModel.dll的引用到该工程。在解决方案资源管理器中,右键单击“引用”,选择“添加引用”,在弹出的对话框中选择“.NET”选项卡,找到System.ServiceModel.dll,选中,单击“确定”按钮。

③ 用同样的方法添加System.ServiceModel.Web.dll的引用。


7e02e9c65281a711606989ffb4618030e2399eb4

④ 在Program.cs中添加如下命名空间的引用。

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.IO;

通过System.ServiceModel命名空间可以使用WCF的基本功能,服务总线使用了很多WCF的对象和属性定义契约,所以在大多数服务总线应用程序中都用到该命名空间。System.ServiceModel.Channels命名空间帮助定义通道,通过通道与服务总线和客户的Web浏览器通信。System.ServiceModel.Web包含了建立基于Web应用程序的数据类型。

⑤ 将Visual Studio默认的命名空间改为Microsoft.ServiceBus.Samples。

⑥ 在命名空间里定义一个名为“IImageContract”的接口,在接口中声明一个名为“GetImage”的方法,作为最后要公开的接口方法。代码如下:

[ServiceContract(Name = "ImageContract", 
    Namespace = "http://samples.microsoft.com/ServiceModel/Relay/RESTTutorial1")]
publicinterfaceIImageContract
{
    [OperationContract, WebGet]
Stream GetImage();
}

这样就允许服务总线将HTTP GET请求路由到GetImage方法上,并将GetImage方法的返回值转换成HTTP GETRESPONSE回复。

⑦ 在“IImageContract”接口定义的下面,声明一个继承了IImageContract和IClientChannel的通道,具体代码如下。

publicinterfaceIImageChannel : IImageContract, IClientChannel { }

channel是一个WCF对象,是服务端和客户端互相传递信息的通道。之后,主机应用程序会创建一个通道,服务总线就会通过这个通道将来自浏览器的HTTP GET请求传递给GetImage方法。服务总线也通过这个通道得到GetImage方法返回的值,并转换成要返回给浏览器的HTTP GETRESPONSE。

⑧ 按F7键生成解决方案,以确保正确。最后的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.IO;

namespace Microsoft.ServiceBus.Samples
{

    [ServiceContract(Name = "IImageContract", 
        Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
publicinterfaceIImageContract
    {
        [OperationContract, WebGet]
Stream GetImage();
    }
publicinterfaceIImageChannel : IImageContract, IClientChannel { }

classProgram
    {
staticvoid (string[] args)
        {
        }
    }
}

3.步骤三:实现基于REST的WCF服务契约
① 在“IImageContract”接口下面创建一个名为“ImageService”的类,该类实现“IImageContract”接口。具体代码如下:

[ServiceBehavior(Name = "ImageService", 
    Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
classImageService : IImageContract
{
}

② 向工程里添加一个JPG格式的图片。右键单击工程,选择“添加”→“现有项”,在弹出的文件选择对话框中选择本地的一张JPG图片,注意文件类型过滤条件要选择ALL Files(.)以方便选择JPG文件。本例添加图片名称为“Penguins.jpg”。

③ 为确保运行时的服务能找到图像文件,在解决方案资源管理器中右键单击添加的图片,在属性中设置“复制到输出目录”的值为“如果较新则复制”,如图6-69所示。


7670412ee78726b4b5a9432fb45965986a384ad7

④ 在工程中添加对System.Drawing.dll、System. Runtime.Serialization.dll、Microsoft.ServiceBus.dll的引用,可参考步骤二的序号②。Microsoft. ServiceBus.dll可以在Windows Azure SDK目录下找到,然后在Program.cs中添加对如下命名空间的引用。

using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Web;

⑤ 在ImageService中添加构造方法来加载位图,以准备将图片发送至客户端浏览器。

classImageService : IImageContract
{
conststring imageFileName = "Penguins.jpg";

Image bitmap;

public ImageService()
    {
this.bitmap = Image.FromFile(imageFileName);
    }
}

⑥ 在ImageService类中添加GetImage()方法,返回值为包含了返回图片的HTTP消息。

publicStream GetImage()
{
MemoryStream stream = newMemoryStream();
this.bitmap.Save(stream, ImageFormat.Jpeg);

    stream.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

return stream;
}

GetImage方法体中使用MemoryStream检索图像和准备流。

⑦ 右键单击ImageListener工程,选择“添加”→“新建项”,在添加新建项对话框中选择应用程序配置文件,单击“添加”,如图6-70所示。


f12d34f144915f065ad6c0b289ea25025ab9217c

⑧ 打开App.config文件,具体内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

⑨ 在根节点中添加名为“system.serviceModel”的子节点,这是WCF的元素,用来定义一个或者多个服务。在这里用来定义服务名称和端点。

⑩ 在“system.serviceModel”中添加名为“binding”的子节点,它用来定义程序中使用的绑定,可以定义多个,但在本例中只需要一个,具体代码如下。

<bindings>
<!-- Application Binding -->
<webHttpRelayBinding>
<bindingname="default">
<securityrelayClientAuthenticationType="None" />
</binding>
</webHttpRelayBinding>
</bindings>

该代码定义了一个relayClientAuthenticationType为None的WebHttpRelayBinding服务总线绑定,这表明使用此绑定的端点不要求客户端提供证书。

11 在“binding”元素后面再添加“services”元素,同样可以定义多个,但本例只需要一个,具体代码如下。

<services>
<!-- Application Service -->
<servicename="Microsoft.ServiceBus.Samples.ImageService"
behaviorConfiguration="default">
<endpointname="RelayEndpoint"
contract="Microsoft.ServiceBus.Samples.IImageContract"
binding="webHttpRelayBinding"
bindingConfiguration="default"
behaviorConfiguration="sharedSecretClientCredentials"
address="" />
</service>
</services>

这段代码配置了使用之前定义的webHttpRelayBinding的一个服务。它使用默认的sharedSecretCredentials,这将在下一步中定义。

12 在“services”元素后面再添加一个“behaviors”元素,代码如下,将“ISSURE_NAME”和“ISSURE_SECRET”替换为发行名字和密钥。

<behaviors>
<endpointBehaviors>
<behaviorname="sharedSecretClientCredentials">
<transportClientEndpointBehaviorcredentialType="SharedSecret">
<clientCredentials>
<sharedSecretissuerName="owner"issuerSecret="fYSDEsI2GhgaCGE6msnul9Ze2DAYvsNGcTnslROykpE=" />
</clientCredentials>
</transportClientEndpointBehavior>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behaviorname="default">
<serviceDebughttpHelpPageEnabled="false"httpsHelpPageEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>

sharedSecretClientCredentials行为定义了服务用来访问服务总线所使用的证书,即SharedSecret。

13 Windows Azure SDK 1.5版本不再将条目添加到Machine.config文件,故需要手动将用到的扩展名添加到项目的App.config文件中,在system.serviceModel元素里添加如下代码。注意Version的值要与读者引用的Microsoft.serviceBus.dll的版本一致。

<extensions>
<behaviorExtensions>
<addname="transportClientEndpointBehavior"
type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, 
          Microsoft.ServiceBus, Version=.0, Culture=neutral, PublicKeyToken= 31bf3856ad364e35" />
</behaviorExtensions>

<bindingExtensions>
<addname="webHttpRelayBinding"
type="Microsoft.ServiceBus.Configuration.WebHttpRelayBindingCollectionElement, 
          Microsoft.ServiceBus, Version=.0, Culture=neutral, PublicKeyToken= 31bf3856ad364e35" />
</bindingExtensions>
</extensions>

14 按F7键,生成解决方案。

最终Program.cs的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Web;

namespace Microsoft.ServiceBus.Samples
{


    [ServiceContract(Name = "ImageContract", 
        Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
publicinterfaceIImageContract
    {
        [OperationContract, WebGet]
Stream GetImage();
    }

publicinterfaceIImageChannel : IImageContract, IClientChannel { }

    [ServiceBehavior(Name = "ImageService", 
        Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
classImageService : IImageContract
    {
conststring imageFileName = "image.jpg";

Image bitmap;

public ImageService()
        {
this.bitmap = Image.FromFile(imageFileName);
        }

publicStream GetImage()
        {
MemoryStream stream = newMemoryStream();
this.bitmap.Save(stream, ImageFormat.Jpeg);

            stream.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

return stream;
        }
    }

classProgram
    {
staticvoid (string[] args)
        {
        }
    }
}

最终App.config的内容如下。

<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
<system.serviceModel>

<extensions>
<behaviorExtensions>
<addname="transportClientEndpointBehavior"
type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, 
              Microsoft.ServiceBus, Version=.0, Culture=neutral,
              PublicKeyToken=31bf3856ad364e35" />
</behaviorExtensions>

<bindingExtensions>
<addname="webHttpRelayBinding"
type="Microsoft.ServiceBus.Configuration.WebHttpRelayBindingCollectionElement, 
              Microsoft.ServiceBus, Version=.0, Culture=neutral, 
              PublicKeyToken=31bf3856ad364e35" />
</bindingExtensions>
</extensions>

<bindings>
<!-- Application Binding -->
<webHttpRelayBinding>
<bindingname="default">
<securityrelayClientAuthenticationType="None" />
</binding>
</webHttpRelayBinding>
</bindings>

<services>
<!-- Application Service -->
<servicename="Microsoft.ServiceBus.Samples.ImageService"
behaviorConfiguration="default">
<endpointname="RelayEndpoint"
contract="Microsoft.ServiceBus.Samples.IImageContract"
binding="webHttpRelayBinding"
bindingConfiguration="default"
behaviorConfiguration="sharedSecretClientCredentials"
address="" />
</service>
</services>

<behaviors>
<endpointBehaviors>
<behaviorname="sharedSecretClientCredentials">
<transportClientEndpointBehaviorcredentialType="SharedSecret">
<clientCredentials>
<sharedSecretissuerName="owner"issuerSecret="fYSDEsI2GhgaCGE6msnul9Ze2DAYvsNGcTnslROykpE=" />
</clientCredentials>
</transportClientEndpointBehavior>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behaviorname="default">
<serviceDebughttpHelpPageEnabled="false"httpsHelpPageEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>

</system.serviceModel>
</configuration>

4.步骤四:将该基于REST的服务托管到服务总线
① 创建服务的基地址。在Main()方法中创建一个变量用来存放服务总线工程的服务命名空间。根据此服务命名空间生成URI。

② 创建和配置该Web服务的主机。使用生成的URI地址创建Web服务主机。

③ 运行Web服务主机。打开服务,给出提示,结束后关闭服务。最后Main()中代码如下:

staticvoid (string[] args)
{
//记录服务命名空间
string serviceNamespace = "InsertServiceNamespaceHere";
//生成URI地址
Uri address = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "Image");

//创建主机
WebServiceHost host = newWebServiceHost(typeof(ImageService), address);

//运行服务
    host.Open();
Console.WriteLine("Copy the following address into a browser to see the image: ");
Console.WriteLine(address + "GetImage");
Console.WriteLine();
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
    host.Close();
}

④ 图6-71所示为运行结果。图6-72所示为最终的界面。


<a href=https://yqfile.alicdn.com/188d0635fe9b5cf8a6646c99b84161818f95db29.png" >
相关文章
|
7天前
|
网络协议 Linux 网络安全
微软工程师偷偷在用!这款SSH工具让Windows操控CentOS比Mac还优雅!
远程登录Linux服务器是管理和维护服务器的重要手段,尤其在远程办公、云服务管理等场景中不可或缺。通过工具如XShell,用户可以方便地进行远程管理。SSH协议确保了数据传输的安全性,命令行界面提高了操作效率。配置XShell连接CentOS时,需确保Linux系统开启sshd服务和22端口,并正确设置主机地址、用户名和密码。此外,调整字体和配色方案可优化使用体验,解决中文显示问题。
61 17
微软工程师偷偷在用!这款SSH工具让Windows操控CentOS比Mac还优雅!
|
8天前
|
安全 固态存储 文件存储
Windows 7纯净版重装教程|附微软原版镜像下载+驱动安装避坑技巧
本文详细介绍如何安全、高效地重装电脑系统,解决蓝屏、崩溃等问题。基于10年经验,涵盖从官方镜像获取、启动盘制作、数据备份到系统部署的全流程,并针对老旧机型优化。提供驱动一键安装工具和系统激活指南,确保无后门风险。文中还列出常见问题解决方案及操作禁忌,帮助用户顺利完成系统重装,让电脑重获新生。建议收藏并转发给有需要的朋友,欢迎留言咨询疑难问题。
|
10天前
|
安全 Linux iOS开发
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
32 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
|
4月前
|
云安全 存储 监控
云计算安全:AWS与Azure的安全策略与实践比较
【10月更文挑战第26天】本文详细比较了AWS和Azure在安全性方面的策略和实践,涵盖身份与访问管理、数据加密与保护以及安全监控与响应。通过代码示例展示了两家云服务提供商在实际应用中的具体操作,帮助企业在选择云服务时做出明智决策。
88 0
|
2月前
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
189 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
2月前
|
人工智能 数据处理 C#
AI Dev Gallery:微软开源 Windows AI 模型本地运行工具包和示例库,助理开发者快速集成 AI 功能
微软推出的AI Dev Gallery,为Windows开发者提供开源AI工具包和示例库,支持本地运行AI模型,提升开发效率。
129 13
|
4月前
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
263 4
|
4月前
|
云安全 存储 监控
云计算安全:AWS与Azure的安全策略与实践比较
【10月更文挑战第27天】本文对比分析了AWS和Azure在云计算安全领域的策略与实践,涵盖技术、定价、混合云工具等方面。通过代码示例展示了如何在两个平台上实施安全措施,如监控告警、数据加密和身份管理。总结了两者的优缺点,帮助读者根据具体需求选择合适的云服务提供商。
93 4
|
Windows
Windows Server AppFabric与WCF
作者:jiankunking 出处:http://blog.csdn.net/jiankunking 来源:《WCF服务编程(第三版)》
813 0
|
Windows
Windows Server AppFabric与WCF
来源:《WCF服务编程(第三版)》
1054 0

热门文章

最新文章