设为首页 收藏本站
查看: 1321|回复: 0

[经验分享] 在IIS上发布基于Windows Azure Service Bus的WCF服务

[复制链接]

尚未签到

发表于 2015-8-12 11:21:20 | 显示全部楼层 |阅读模式
概要
  随着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。
DSC0000.png
  注册到Windows Azure Service Bus上的WCF服务, 其在运行时候的架构如下,
DSC0001.gif
  传统的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 Azure  Service 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, 其会自动生成一个相应令牌:
DSC0002.png
  2. 准备好Service Bus的访问令牌之后, 下面我们就需要创建一个WCF服务, 并将该WCF服务注册到该Windows Azure Service Bus上。
  1) 打开VS2012, 创建一个WCF service application, 在该工程中, 我们需要引用ServiceBus相关Assembly。 我们可以通过点击”References” ,右击 选择”Manage NuGet Packages…”, ,然后在线搜索”Windows Azure Service Bus”, 找到后安装即可, 如下所示:
DSC0003.png
  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 服务总线的管理台里面,也可以看到起处理活动的状态,如下:
DSC0004.png
  3. 现在问题是: 如果我们没有主动去访问svc文件来激活该WCF服务的话,那么客户端在通过Windows Azure Service Bus去调用这个WCF服务的时候, 就会遇到如下异常,
DSC0005.png
  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,我们都可以得到正确的响应, 如下所示:
DSC0006.png

参考文档
  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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-97873-1-1.html 上篇帖子: How to run a (Tomcat)Java application server on a Azure virtual machine 下篇帖子: Windows Azure Cloud Service (22) Web Role的Full IIS特性
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表