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配置文件可以看到多了以下配置。
<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接口中添加契约。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: 在此添加您的服务操作
[OperationContract]
string[] GetWeather(string cityName);
}
然后在Service1.svc文件中实现新添加的接口。
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服务的客户端工具,我们点击项目的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函数填入如下代码:
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等)中提供一个天气预报查询的应用。天气预报的图标可以自行到网站上面下载。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。