65.9K
CodeProject 正在变化。 阅读更多。
Home

通过 Windows Azure AppFabric 服务总线公开 FTP 服务器

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013年3月14日

CC (ASA 3U)

6分钟阅读

viewsIcon

15134

使用 Windows Azure AppFabric 服务总线公开 FTP 服务器。

引言

随着各种云计算基础设施的日趋稳定,越来越多的应用程序被部署到这些基础设施中。在这些应用程序中,有相当数量的应用程序正在从传统部署迁移到云。但是,有时由于某些限制,可能无法将整个应用程序都迁移到云,而只能迁移部分。例如,可以将表示层和业务逻辑层迁移到云,但后端数据库仍然保留在本地。

在我的一些博客(参考 - 一篇另一篇)中,我曾介绍过使用 Azure 构建块轻松地将本地数据库(即位于防火墙后面的企业网络内的数据库)公开给互联网上的消费者应用程序的各种方法。在本文中,我将尝试解释一种方法,介绍如何轻松地将现有本地 FTP 服务器公开到企业防火墙之外,以支持可能已迁移到云或其他外部部署(互联网)的某些应用程序。

要实现这一点,我们将利用 Azure AppFabric 服务总线,通过 WCF 服务终结点公开 FTP 服务器上的文件。WCF 服务可以部署在 IIS 上,也可以部署在任何其他托管应用程序(如控制台应用程序)中。但是,在使用 IIS 时,我们需要确保启用 WCF 服务的自动启动,否则该服务默认情况下将无法供任何客户端使用。有关启用自动启动的步骤,请参考此 MSDN 页面,这些步骤将确保在 IIS 启动时立即启动部署在 IIS 中的服务,而不是等待第一个服务请求到来。

从宏观上看,FTP 服务器基本上是一个文件存储库,并根据需要提供对文件的访问。因此,我们可以开发一个基于 REST 的 WCF 服务层,该层将通过 AppFabric 服务总线公开一些接口,并在内部访问本地 FTP 服务器以获取目标文件。

现在,让我们看看如何实现上述实现,并使用 Windows Azure AppFabric 服务总线将本地 FTP 服务器的文件公开到企业防火墙之外。

假设

读者需要了解以下内容:

  • 基于 REST 的 WCF 服务实现
  • Windows Azure AppFabric 服务总线及相关的中继绑定
  • 访问 FTP 服务器上文件的机制

要遵循的步骤

1. 服务实现

如果使用 IIS 托管服务,则创建一个 WCF 服务库类型的项目。如果使用控制台应用程序等其他应用程序托管,则创建一个控制台应用程序类型的项目。

a. 添加用于公开 REST 接口的服务协定

[ServiceContract]

public interface IExposeFTPContract

{

[OperationContract]

[WebGet(UriTemplate = "/{fileName}")]

Message GetFile(string fileName);

}

其中“Message”代表一个通信单元,其定义在命名空间:System.ServiceModel.Channels 中。

b. 添加服务接口定义

public class ExposeFTPContract: IExposeFTPContract

{

public Message GetFile(string fileName)

{

//读取文件并将其作为 Message 对象返回

//以下 Console.WriteLine 用于跟踪调用

Console.WriteLine("Accessed at- " + DateTime.Now.ToString());

Console.WriteLine("--------------------------------------------");

using (FileStream fileStream = File.Open(

<使用提供的文件名到 FTP 服务器的完整文件路径,

FileMode.Open))

{

using (create some suitable reader using the fileStream, e.g. fileReader)

{

Message message = Message.CreateMessage(MessageVersion.None,"", fileReader);

using (MessageBuffer messageBuffer = message.CreateBufferedCopy(1000))

{

return messageBuffer.CreateMessage();

}

}

}

}

}

这里的目标是提供一个访问 FTP 服务器上文件的接口。这是通过服务接口使文件可访问的一种方法;我相信还有其他更好的方法。

2. 服务配置

在主机配置文件中,即,如果使用 IIS,则是 web.config;如果使用控制台应用程序,则是 app.config,添加以下配置设置:

a. 添加服务终结点

<services>

<service name="namespace.ExposeFTPContract"

behaviorConfiguration="ExposeFTPServiceBehavior">

<endpoint behaviorConfiguration="webhttpSharedSecretClientCredentials"

contract=”namespace.IExposeFTPContract"

binding="webHttpRelayBinding" bindingConfiguration="webHttpRelayEndpointConfig" />

</service>

</services>

AppFabric 命名空间也需要提供,因此,如果服务托管在 IIS 中,则在上面的“endpoint”中添加属性 address="http://<appfabric namespace>.servicebus.windows.net/,如下所示:

<endpoint behaviorConfiguration="webhttpSharedSecretClientCredentials"

contract=”namespace.IExposeFTPContract"

binding="webHttpRelayBinding" bindingConfiguration="webHttpRelayEndpointConfig" address="http://<appfabric namespace>.servicebus.windows.net"/>

如果使用控制台应用程序托管,则可以在主机初始化时提供 AppFabric 命名空间(稍后在“3. 服务托管”部分介绍)。

b. 添加服务行为

<serviceBehaviors>

<behavior name="ExposeFTPServiceBehavior">

<!-- To avoid disclosing metadata information, set the value

below to false and remove the metadata endpoint above before deployment -->

<serviceMetadata httpGetEnabled="true" />

<!-- To receive exception details in faults for debugging purposes,

set the value below to true. Set to false before deployment

to avoid disclosing exception information -->

<serviceDebug includeExceptionDetailInFaults="true" />

</behavior>

</serviceBehaviors>

c. 添加终结点行为

此终结点行为用于以下目的:

  • 将服务接口公开为 REST,以及
  • 提供连接到 AppFabric 服务总线的凭据,并将服务公开到企业防火墙之外。

<endpointBehaviors>

<behavior name="webhttpSharedSecretClientCredentials">

<!—提供连接到 AppFabric 服务总线的凭据-->

<transportClientEndpointBehavior credentialType="SharedSecret">

<clientCredentials>

<sharedSecret issuerName="appfabric issuer name e.g. owner" issuerSecret="appfabric secret key" />

</clientCredentials>

</transportClientEndpointBehavior>

<!—公开服务接口为 REST-->

<webHttp/>

</behavior>

</endpointBehaviors>

其中,issuer name 和 issuer secret 是使用 Azure 订阅创建 AppFabric 命名空间后可获得的。

d. 添加 WCF 绑定

需要配置 WCF 绑定“webHttpRelayBinding”,因为它将与 AppFabric 服务建立一个双向通信通道,以穿越企业防火墙。

<bindings>

<webHttpRelayBinding>

<binding name="webHttpRelayEndpointConfig">

<security mode="None" relayClientAuthenticationType="None"/>

</binding>

</webHttpRelayBinding>

</bindings>

3. 服务托管

如果服务要托管在控制台应用程序中,请更新项目中的 Program.cs 文件中的“Main”方法,使用以下代码:

try

{

Console.Title = "Expose Client FTP through appfabric Service Bus";

string serviceNamespaceDomain = AppFabric 命名空间,或可从配置设置中读取;

Console.WriteLine("Service Namespace Domain : " + serviceNamespaceDomain);

Console.WriteLine("");

//根据 AppFabric 命名空间创建服务 URI

Uri address = ServiceBusEnvironment.CreateServiceUri("http", serviceNamespaceDomain, "");

//读取配置创建服务主机

ServiceHost host = new ServiceHost(typeof(ExposeFTPContract), address);

Console.WriteLine("Starting service...");

//为终结点创建 ServiceRegistrySettings 行为。

// 这将使此服务与其他注册的服务一起可见

// 到同一个 AppFabric 命名空间,如果有人尝试访问

// URL - http://<appfabric namespace>.servicebus.windows.net

IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);

foreach (ServiceEndpoint subscriberEndpoint in host.Description.Endpoints)

{

subscriberEndpoint.Behaviors.Add(serviceRegistrySettings);

}

//打开服务

host.Open();

Console.WriteLine("Service started...");

Console.WriteLine("Service address: " + address);

Console.WriteLine("");

Console.WriteLine("FTP files may now be accessed as:");

Console.WriteLine(address + "<file name>");

Console.WriteLine("");

Console.WriteLine("Press [Enter] to exit");

Console.WriteLine("");

Console.WriteLine("--------------------------------------------");

Console.ReadLine();

//关闭服务

host.Close();

}

catch (Exception ex)

{

Console.WriteLine("Error: " + ex.Message);

}

如果使用 IIS 来托管,请确保托管的服务和相应的应用程序池正在运行。

结论

服务运行后,可以通过 Windows Azure AppFabric URL 访问本地 FTP 服务器上的文件,例如:http://<appfabric_namespace>.servicebus.windows.net/<file_name>

代码可以根据个人需求进行增强,但这说明了如何轻松地使用 Azure AppFabric 来为基于 Internet 的消费者应用程序公开本地 FTP 服务器。

© . All rights reserved.