haoli 发表于 2015-8-12 11:21:20

在IIS上发布基于Windows Azure Service Bus的WCF服务

概要
  随着Windows Azure在中国的落地, 相信越来越多的人会用到Windows Azure。Windows Azure提供了丰富的基于云的各种服务,其中包括Service Bus(服务总线),通过Service Bus, 我们可以将传统的WCF Service注册到Window Azure Service Bus上。本文以IIS8, WCF4.0为例,详细介绍如何将部署在IIS里面的WCF服务如何主动注册到Windows Azure Service Bus。

  注册到Windows Azure Service Bus上的WCF服务, 其在运行时候的架构如下,

  传统的WCF服务有多种方式来进行HOST,比如self-hosted 或者IIS。如果通过self-hosted的方式的话, 只需要在开启HOST的时候主动注册到Windows Azure Service Bus即可。
如果WCF通过IIS的方式来Host,WAS会依据进来的请求来激活w3wp.exe进程。如果没有没有请求进入到web server的话,WCF不会主动注册到Windows Azure Service Bus的。 这样就存在一个问题:如果客户端第一次去调用该Windows AzureService Bus Endpoint的时候, 但是由于服务端WCF服务根本没有注册到Windows Azure Service Bus, 那么就会收到“No service is hosted at the specified address.”的异常. 因此将部署在IIS里面的WCF服务注册到Windows Azure Service Bus上需要经过一些特殊的处理。

前提条件
  1. 开发工具: Visual Studio 2012 + Windows Azure SDK2.0
2.一个注册好的Windows Azure 账号
3.Windows Server 2012或者Windows 8
4.IIS8

实施步骤
  1.首先我们需要在Windows Azure 里面创建一个Service Bus的namespace. 比如, 我创建了一个叫做IISSBWCF的namespace, 其会自动生成一个相应令牌:

  2. 准备好Service Bus的访问令牌之后, 下面我们就需要创建一个WCF服务, 并将该WCF服务注册到该Windows Azure Service Bus上。
  1) 打开VS2012, 创建一个WCF service application, 在该工程中, 我们需要引用ServiceBus相关Assembly。 我们可以通过点击”References” ,右击 选择”Manage NuGet Packages…”, ,然后在线搜索”Windows Azure Service Bus”, 找到后安装即可, 如下所示:

  2) WCF Contract的定义和服务的实现和传统WCF服务没有任何区别。接下来是要把该WCF服务注册到Windows Azure Service Bus上了。 下面的配置演示了通过配置的方式注册到Windows Azure Service Bus:



“<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="EnableAutoStart" value="true"/>
<add key="ActivatedURL" value="/iis-hosted-sb-wcf/Service1.svc"/>
</appSettings>
<system.serviceModel>
<services>
<service name="IIS_Hosted_SB.Service1">
<endpoint address="https://iissbwcf.servicebus.windows.net" binding="basicHttpRelayBinding" contract="IIS_Hosted_SB.IService1"behaviorConfiguration="sbTokenProvider"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="sbTokenProvider">
<transportClientEndpointBehavior>
<tokenProvider>
<sharedSecret issuerName="owner" issuerSecret="这里是我的Access Token,做替换处理” />
</tokenProvider>
</transportClientEndpointBehavior>
</behavior>      
</endpointBehaviors>
<serviceBehaviors>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
<extensions>
<!-- In this extension section we are introducing all known service bus extensions. User can remove the ones they don't need. -->
<behaviorExtensions>
<add name="transportClientEndpointBehavior" type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</bindingElementExtensions>
<bindingExtensions>
<add name="basicHttpRelayBinding" type="Microsoft.ServiceBus.Configuration.BasicHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</bindingExtensions>
</extensions>
</system.serviceModel>
</configuration>
  3) 当我们通过IIS第一次browse这个svc文件的时候, 那么其会自动注册到Windows Azure Service Bus上。注册成功后, 我们在Windows Azure 服务总线的管理台里面,也可以看到起处理活动的状态,如下:

  3. 现在问题是: 如果我们没有主动去访问svc文件来激活该WCF服务的话,那么客户端在通过Windows Azure Service Bus去调用这个WCF服务的时候, 就会遇到如下异常,

  4. 从IIS8开始(IIS7.5通过extension的方式来实现),我们提供了预热功能,即使没有请求进来, 我们也可以采用preload的方式完成初始化的工作,激活相关服务。而我们的WCF服务正好需要利用这一点。
  实施步骤如下:
1)首先需要将WCF服务所运行的Application pool设置为AlwaysRunning.
2)然后实现IProcessHostPreloadClient接口的Preload方法:在里面实现对svc服务的激活。示例代码如下:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.ServiceModel.Activation;
using System.ServiceModel;
namespace IIS_Hosted_SB
{
public class AutoStartService:System.Web.Hosting.IProcessHostPreloadClient
{
public void Preload(string[] parameters)
{
bool isServceActivated = false;
int attempts = 0;
while (!isServceActivated && (attempts < 10))
{
Thread.Sleep(1 * 1000);
try
{
string ActivatedURL = System.Configuration.ConfigurationManager.AppSettings["ActivatedURL"].ToString();
ServiceHostingEnvironment.EnsureServiceAvailable(ActivatedURL);
WriteTrace(“Windows Azure Service Bus Endpoint is activated”);
isServceActivated = true;
}
catch (Exception exception)
{
attempts++;
//continue on these exceptions, otherwise fail fast
if (exception is EndpointNotFoundException
|| exception is ServiceActivationException
|| exception is ArgumentException)
{
//log
                  }
else
{
throw;
}
}
}
}
}
}
  3) 修改IIS配置文件(%windir%\system32\inetsrv\config\applicationhost.config), 将刚才的自动激活服务Provider应用到该WCF服务的VD上。修改后的配置文件如下:



<sites>
<site name="Default Web Site" id="1">
<application path="/">
<virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
</application>
<application path="/iis-hosted-sb-wcf" applicationPool="DefaultAppPool" serviceAutoStartEnabled="true" serviceAutoStartProvider="AutoStartProvider">
<virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\iis-hosted-sb-wcf" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:" />
<binding protocol="net.tcp" bindingInformation="808:*" />
<binding protocol="net.msmq" bindingInformation="localhost" />
<binding protocol="msmq.formatname" bindingInformation="localhost" />
<binding protocol="net.pipe" bindingInformation="*" />
</bindings>
<traceFailedRequestsLogging enabled="true" />
</site>

<serviceAutoStartProviders>
<add name="AutoStartProvider" type="IIS_Hosted_SB.AutoStartService,IIS_Hosted_SB" />
</serviceAutoStartProviders>
</system.applicationHost>
  4) 经过以上这些配置后, 我们可以发现只要一旦该application pool被回收之后,马上会有一个新的w3wp.exe被自动激活,同时其会自动注册到Windows Azure Service Bus上。 任何时候客户端通过Windows Azure Service Bus访问该WCF service,我们都可以得到正确的响应, 如下所示:


参考文档
  How to Use the Service Bus Relay Service
http://www.windowsazure.com/en-us/develop/net/how-to-guides/service-bus-relay/
  IIS hosting of Wcf Services with Servicebus Endpoints
http://archive.msdn.microsoft.com/ServiceBusDublinIIS/Release/ProjectReleases.aspx?ReleaseId=4336
  Application Initialization Part 2
http://blogs.iis.net/wadeh/archive/2012/05/01/application-initialization-part-2.aspx
  
  希望以上内容对您有所帮助
  Winston He
页: [1]
查看完整版本: 在IIS上发布基于Windows Azure Service Bus的WCF服务