转:asmx迷10分钟升级成wcf熟手指南

时间:2023-12-16 18:06:02

前言:本文旨在帮助从未接触过wcf(.svc文件)的webservice开发人员,快速将传统的webService/asmx技术迁移到wcf。高手就不用浪费时间往下看了:)

以下所有操作均为vs2010 sp1环境

一、服务物理文件创建方式的变化

1.1 asmx的创建方式

新建项-->Web服务

转:asmx迷10分钟升级成wcf熟手指南

1.2 wcf的创建方式

新建项-->Wcf服务

转:asmx迷10分钟升级成wcf熟手指南

二、方法定义上的区别

2.1 asmx中的方法定义

转:asmx迷10分钟升级成wcf熟手指南

2.2 wcf 中的方法定义

这里先啰嗦一下,默认情况下创建一个wcf服务(比如HelloWorld2.svc文件)后,vs.net会自动创建一个IHelloWorld2.cs的接口,然后HelloWorld2类会实现这个接口。

转:asmx迷10分钟升级成wcf熟手指南

如果这让您感觉不安,可以大胆的把IHelloWorld2.cs接口删除!然后把HelloWorld2.svc.cs中的代码改成下面这样:

转:asmx迷10分钟升级成wcf熟手指南

即:

a)在需要暴露的class前加上[ServcieContract]标签

b)在需要暴露的Method前加上[OperationContract]标签

三、客户端服务引用方式的变化

实际开发中,通常service层是一个单独项目,而调用service的是另一个项目,所以我们再创建一个Web Application(或WebSite),以充当调用服务器的客户端应用

3.1 asmx引用方式

右击"添加服务引用",会出来一个对话框

转:asmx迷10分钟升级成wcf熟手指南

如果您用习惯了vs.net2008的"添加服务引用",对这个界面可能会感觉很陌生,没关系,vs2010允许您找回旧时熟悉的感觉,继续点击"高级"

转:asmx迷10分钟升级成wcf熟手指南

继续点击"添加服务引用",下面这个界面应该再熟悉不过了吧
转:asmx迷10分钟升级成wcf熟手指南

点击"此解决方案中的Web服务",选择HelloWorld.asmx,Web引用名填写成"YJM"(当然也可以自己随便写)

转:asmx迷10分钟升级成wcf熟手指南

点击"添加引用"完事,完了以后,在vs2010中与vs2008有一些区别:

在vs2010中web.config中会变成

<applicationSettings>
<WebClient.Properties.Settings>
<setting name="WebClient_YJM_HelloWorld" serializeAs="String">
<value>http://localhost:3538/HelloWorld.asmx</value>
</setting>
</WebClient.Properties.Settings>
</applicationSettings>

而vs2008添加服务引用后,是类似

<appSettings>
<add key="YJM.HelloWorld" value="http://localhost:3538/HelloWorld.asmx"/>
</appSettings>

注:部署到生产环境后,只要修改配置文件中的 http://localhost:3538/HelloWorld.asmx 即可实现webservice url的动态调整.(当然如果要在运行时动态指定url,就要代码处理了,后面的示例代码会给出方法)

3.2 wcf 引用方式

右击"添加服务引用",点击"发现"

转:asmx迷10分钟升级成wcf熟手指南

选择HelloWorld2.svc,命名空间填写"YJM2"(当然也能是其它名字)

转:asmx迷10分钟升级成wcf熟手指南

点击“确定”完成,web.config中会多出一节内容:

    <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_HelloWorld2" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:3538/HelloWorld2.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_HelloWorld2" contract="YJM2.HelloWorld2"
name="BasicHttpBinding_HelloWorld2" />
</client>
</system.serviceModel>

注:部署到生产环境后,只要修改配置文件中的 http://localhost:3538/HelloWorld2.svc 即可实现wcf url的动态调整.

四、客户端调用代码的变化

这部分太简单了,直接上代码吧

using System;
using WebClient.YJM;
using WebClient.YJM2; namespace WebClient
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region asmx/webservice调用示例
using (HelloWorld ws = new HelloWorld())
{
//System.Diagnostics.Debug.WriteLine(ws.Url);
//System.Diagnostics.Debug.WriteLine(Properties.Settings.Default.WebClient_YJM_HelloWorld);
//ws.Url = "http://www.youWeb.com/xxx.asmx"; //如果需要动态切换asmx的地址,这一行代码就给出了方法
string result = ws.Hello("client_asmx");
Response.Write("asmx call:" + result + "
");
}
#endregion #region svc/wcf 调用示例
using (HelloWorld2Client client = new HelloWorld2Client())
{
//System.Diagnostics.Debug.Write(client.Endpoint.ListenUri.ToString());
//client.Endpoint.ListenUri = new Uri("http://www.youWeb.com/xxx.svc");//如果需要动态切换svc的地址,这一行代码就给出了方法
string result = client.Hello("client_wcf");
Response.Write("wcf call:" + result + "
");
}
#endregion
}
}
}

运行结果:

转:asmx迷10分钟升级成wcf熟手指南

示例源码下载:http://files.cnblogs.com/yjmyzz/asmx_update_to_wcf.zip

相信有webservice经验的同学们,看完本篇,已经能顺利升级到wcf了。当然wcf深究下去,远不止本文所写的这么简单,这门技术水还是相当深的,强烈推荐多看看园子里http://www.cnblogs.com/artech/ 的文章,他是园子里(甚至可以说是国内) 我心目中的 wcf第一人,多去看看,必有收获。