首页 > 其他 > 详细

WCF SOA服务应用

时间:2014-02-25 00:59:13      阅读:608      评论:0      收藏:0      [点我收藏+]

WCF是微软官方推出的一个基于服务的整合框架,它整合了以前的Web Service、MSMQ、Remoting等通信技术,通过灵活的配置,让服务编程更加容易、可扩展。这篇文章主要目的就是带领大家从开发到测试到部署到调用WCF服务应用,让大家对其有个整体上的了解。

一、建立一个WCF应用程序

我们给这个WCF应用程序起名为WeatherWcfService,并添加天气预报的WebService引用。

我们选择http://www.webxml.com.cn/zh_cn/weather_icon.aspx 这个地址的Web服务作为我们的天气预报的Web服务的提供者。

bubuko.com,布布扣

复制Endpoint中的url地址。

bubuko.com,布布扣

如图所示,添加服务引用。

bubuko.com,布布扣

将web服务的url粘贴到地址栏,点击转到按钮,等待服务加载后点击确定按钮。

通过该项目的Web.config配置文件可以看到多了以下配置。

bubuko.com,布布扣
bubuko.com,布布扣
<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> 
View Code
bubuko.com,布布扣

可以看到,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文件中实现新添加的接口。

bubuko.com,布布扣
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);  
        }  
    }  
}  
bubuko.com,布布扣

这样一个提供天气预报的WCF服务就写好了,我们用测试工具测试一下。

三、WCF服务测试

微软官方为我们准备好了一个测试WCF服务的客户端工具,我们点击项目的svc文件,运行项目(Ctrl+F5),WCF测试工具会自动加载。

bubuko.com,布布扣

你也可以直接到VS的安装目录找到这个测试工具,比如我的目录为:D:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE

双击GetWeather()方法,填入测试值,点击调用按钮即可。

bubuko.com,布布扣

会看到我们已经收到了想要的天气预报数据。

四、发布WCF服务到IIS

由于我使用的是IIS8.0版本。在用VS2012发布WCF项目到指定目录后,并不能直接浏览svc(服务),需要在IIS里面进行一些设置。

bubuko.com,布布扣

1、添加设置MIME类型

bubuko.com,布布扣

2、添加设置处理程序映射

bubuko.com,布布扣

配置好之后,在浏览器浏览一下这个WCF服务。我的URL为:http://192.168.0.2/wcf/Service1.svc,如果能正常打开,说明WCF服务已经在IIS寄宿成功。

bubuko.com,布布扣

五、编写调用这个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); 
        
    

bubuko.com,布布扣

可以看到,我们通过客户端调用WCF服务,获得了互联网提供的WebService天气预报的服务。

读者可以自行进一步处理以上获得的数据,在Web或者App(IOS、Android、Windows Phone等)中提供一个天气预报查询的应用。天气预报的图标可以自行到网站上面下载。

WCF SOA服务应用

原文:http://www.cnblogs.com/guwei4037/p/3563879.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!