首页 文章 精选 留言 我的

精选列表

搜索[soa],共1154篇文章
优秀的个人博客,低调大师

WCF SOA服务应用

WCF是微软官方推出的一个基于服务的整合框架,它整合了以前的Web Service、MSMQ、Remoting等通信技术,通过灵活的配置,让服务编程更加容易、可扩展。这篇文章主要目的就是带领大家从开发到测试到部署到调用WCF服务应用,让大家对其有个整体上的了解。 一、建立一个WCF应用程序 我们给这个WCF应用程序起名为WeatherWcfService,并添加天气预报的WebService引用。 我们选择http://www.webxml.com.cn/zh_cn/weather_icon.aspx 这个地址的Web服务作为我们的天气预报的Web服务的提供者。 复制Endpoint中的url地址。 如图所示,添加服务引用。 将web服务的url粘贴到地址栏,点击转到按钮,等待服务加载后点击确定按钮。 通过该项目的Web.config配置文件可以看到多了以下配置。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 < system.serviceModel > < bindings > < basicHttpBinding > < binding name = "WeatherWSSoap" /> </ basicHttpBinding > < customBinding > < binding name = "WeatherWSSoap12" > < textMessageEncoding messageVersion = "Soap12" /> < httpTransport /> </ binding > </ customBinding > </ bindings > < client > < endpoint address = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx" binding = "basicHttpBinding" bindingConfiguration = "WeatherWSSoap" contract = "ServiceReference1.WeatherWSSoap" name = "WeatherWSSoap" /> < endpoint address = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx" binding = "customBinding" bindingConfiguration = "WeatherWSSoap12" contract = "ServiceReference1.WeatherWSSoap" name = "WeatherWSSoap12" /> </ client > < behaviors > < serviceBehaviors > < behavior > <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> < serviceMetadata httpGetEnabled = "true" httpsGetEnabled = "true" /> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> < serviceDebug includeExceptionDetailInFaults = "false" /> </ behavior > </ serviceBehaviors > </ behaviors > < protocolMapping > < add binding = "basicHttpsBinding" scheme = "https" /> </ protocolMapping > < serviceHostingEnvironment aspNetCompatibilityEnabled = "true" multipleSiteBindingsEnabled = "true" /> </ system.serviceModel > 可以看到,WCF把服务的一些要素配置在config文件里,比如这里面有两个终结点(endpoint)以及各自的三要素(地址:address;绑定:binding;契约:contract)。 二、编写WCF服务端代码 根据提供天气预报Web服务的网站的文档说明,我们可以通过GetWeather方法获得天气预报信息。 首先在IService1.cs文件的IService1接口中添加契约。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [ServiceContract] public interface IService1 { [OperationContract] string GetData( int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: 在此添加您的服务操作 [OperationContract] string [] GetWeather( string cityName); } 然后在Service1.svc文件中实现新添加的接口。 1 2 3 4 5 6 7 8 9 10 11 public string [] GetWeather( string cityName) { using (ChannelFactory<ServiceReference1.WeatherWSSoap> channelFactory = new ChannelFactory<ServiceReference1.WeatherWSSoap>( "WeatherWSSoap" )) { ServiceReference1.WeatherWSSoap service = channelFactory.CreateChannel(); using (service as IDisposable) { return service.getWeather(cityName, string .Empty); } } } 这样一个提供天气预报的WCF服务就写好了,我们用测试工具测试一下。 三、WCF服务测试 微软官方为我们准备好了一个测试WCF服务的客户端工具,我们点击项目的svc文件,运行项目(Ctrl+F5),WCF测试工具会自动加载。 你也可以直接到VS的安装目录找到这个测试工具,比如我的目录为:D:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE 双击GetWeather()方法,填入测试值,点击调用按钮即可。 会看到我们已经收到了想要的天气预报数据。 四、发布WCF服务到IIS 由于我使用的是IIS8.0版本。在用VS2012发布WCF项目到指定目录后,并不能直接浏览svc(服务),需要在IIS里面进行一些设置。 1、添加设置MIME类型 2、添加设置处理程序映射 配置好之后,在浏览器浏览一下这个WCF服务。我的URL为:http://192.168.0.2/wcf/Service1.svc,如果能正常打开,说明WCF服务已经在IIS寄宿成功。 五、编写调用这个WCF客户端代码 首先建立一个控制台项目。 其次添加服务的引用。跟上面WCF添加WebService服务引用一样,这里还是先添加服务引用,地址为:http://192.168.0.2/wcf/Service1.svc(这是我本地发布到IIS的WCF服务地址)。 然后我们在Main函数填入如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 using (ChannelFactory<ServiceReference1.IService1> channelFactory = new ChannelFactory<ServiceReference1.IService1>( "BasicHttpBinding_IService1" )) { ServiceReference1.IService1 service = channelFactory.CreateChannel(); using (service as IDisposable) { string [] values = service.GetWeather( "北京" ); foreach ( var item in values) { Console.WriteLine(item); } } } 可以看到,我们通过客户端调用WCF服务,获得了互联网提供的WebService天气预报的服务。 读者可以自行进一步处理以上获得的数据,在Web或者App(IOS、Android、Windows Phone等)中提供一个天气预报查询的应用。天气预报的图标可以自行到网站上面下载。 本文转自 guwei4037 51CTO博客,原文链接:http://blog.51cto.com/csharper/1362435

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册