《微软云计算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" >
相关文章
|
11月前
|
开发框架 监控 安全
Windows Defender 导致 Web IIS 服务异常停止排查
某日凌晨IIS服务异常停止,经查为Windows Defender安全补丁KB2267602触发引擎更新,导致系统资源波动,进而引发应用池回收。确认非人为操作,系统无重启。通过分析日志与监控,定位原因为Defender更新后扫描加重负载。解决方案:将IIS及.NET相关路径添加至Defender排除列表,避免业务影响。
1090 116
|
人工智能 弹性计算 安全
阿里云计算巢私有化MCP市场:企业级AI工具的安全部署新选择
阿里云计算巢私有化MCP市场,依托阿里云弹性计算资源,提供自主可控的私有化部署方案。支持OpenAPI、SSE、StreamableHttp等多种接入方式,结合Higress云原生网关实现高效网络控制,所有工具直接部署在用户云账号下,5分钟极速部署,保障数据安全与使用便捷性。适用于对数据安全要求高、需访问内网资源、服务隔离及统一管理多种MCP工具的企业场景。
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
4200 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
人工智能 Kubernetes 安全
通过阿里云计算巢部署 NVIDIA NIM,加速企业大语言模型 SaaS 化
通过阿里云计算巢部署 NVIDIA NIM,加速企业大语言模型 SaaS 化
|
Linux iOS开发 MacOS
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
545 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
|
存储 监控 数据可视化
SaaS云计算技术的智慧工地源码,基于Java+Spring Cloud框架开发
智慧工地源码基于微服务+Java+Spring Cloud +UniApp +MySql架构,利用传感器、监控摄像头、AI、大数据等技术,实现施工现场的实时监测、数据分析与智能决策。平台涵盖人员、车辆、视频监控、施工质量、设备、环境和能耗管理七大维度,提供可视化管理、智能化报警、移动智能办公及分布计算存储等功能,全面提升工地的安全性、效率和质量。
436 0
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
1969 4
|
人工智能 运维 安全
中企出海大会|打造全球化云计算一张网,云网络助力中企出海和AI创新
阿里云网络作为全球化战略的重要组成部分,致力于打造具备AI技术服务能力和全球竞争力的云计算网络。通过高质量互联网服务、全球化网络覆盖等措施,支持企业高效出海。过去一年,阿里云持续加大基础设施投入,优化海外EIP、GA产品,强化金融科技与AI场景支持。例如,携程、美的等企业借助阿里云实现业务全球化;同时,阿里云网络在弹性、安全及性能方面不断升级,推动中企迎接AI浪潮并服务全球用户。
2257 8
|
监控 安全 网络安全
云计算与网络安全:技术挑战与解决方案
随着云计算技术的飞速发展,其在各行各业的应用越来越广泛。然而,随之而来的网络安全问题也日益凸显。本文将从云服务、网络安全和信息安全等技术领域出发,探讨云计算面临的安全挑战及相应的解决方案。通过实例分析和代码示例,旨在帮助读者更好地理解云计算与网络安全的关系,提高网络安全防护意识。
488 56

热门文章

最新文章