一、WCF入门
1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序,删除系统生成的两个文件IService1.cs与Service1.svc, 添加自定义的WCF【服务文件】User.svc。建立完成后如下图所示:
2、在IUser.cs中添加方法ShowName接口和在User.svc中时间方法,如图:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServer { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IUser”。 [ServiceContract] public interface IUser { [OperationContract] string ShowName(string name); } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServer { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“User”。 public class User : IUser { public string ShowName(string name) { string wcfName = string.Format("WCF服务,显示姓名:{0}", name); return wcfName; } } }
大家可以看到,在WCF中的接口与普通接口的区别只在于两个上下文,其他的和我们正常学习的接口一样。定义这个上下文要添加System.ServiceModel的引用。
[ServiceContract],来说明接口是一个WCF的接口,如果不加的话,将不能被外部调用。
[OperationContract],来说明该方法是一个WCF接口的方法,不加的话同上。
此时我们的第一个WCF服务程序就建立好了,将User.svc“设为起始页”,然后F5运行一下试试,如下图所示,VS2010自动调用了WCF的客户端测试工具以便我们测试程序:
我们双击上图中的 ShowName() 方法,测试一下方法,出现如下图:
4、在游览器中出现如下图所示,说明服务部署成功。
二、客户端动态调用WCF
1、新建WinForm程序,添加WCF访问接口类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Channels; using System.Reflection; namespace WcfClient { /// <summary> /// 使用ChannelFactory为wcf客户端创建独立通道 /// </summary> public class WcfChannelFactory { public WcfChannelFactory() { } /// <summary> /// 执行方法 WSHttpBinding /// </summary> /// <typeparam name="T">服务接口</typeparam> /// <param name="uri">wcf地址</param> /// <param name="methodName">方法名</param> /// <param name="args">参数列表</param> public static object ExecuteMetod<T>(string uri, string methodName, params object[] args) { //BasicHttpBinding binding = new BasicHttpBinding(); //出现异常:远程服务器返回错误: (415) Cannot process the message because the content type ‘text/xml; charset=utf-8‘ was not the expected type ‘application/soap+xml; charset=utf-8‘.。 WSHttpBinding binding = new WSHttpBinding(); EndpointAddress endpoint = new EndpointAddress(uri); using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint)) { T instance = channelFactory.CreateChannel(); using (instance as IDisposable) { try { Type type = typeof(T); MethodInfo mi = type.GetMethod(methodName); return mi.Invoke(instance, args); } catch (TimeoutException) { (instance as ICommunicationObject).Abort(); throw; } catch (CommunicationException) { (instance as ICommunicationObject).Abort(); throw; } catch (Exception vErr) { (instance as ICommunicationObject).Abort(); throw; } } } } //nettcpbinding 绑定方式 public static object ExecuteMethod<T>(string pUrl, string pMethodName,params object[] pParams) { EndpointAddress address = new EndpointAddress(pUrl); Binding bindinginstance = null; BasicHttpBinding ws = new BasicHttpBinding(); ws.MaxReceivedMessageSize = 20971520; bindinginstance = ws; using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address)) { T instance = channel.CreateChannel(); using (instance as IDisposable) { try { Type type = typeof(T); MethodInfo mi = type.GetMethod(pMethodName); return mi.Invoke(instance, pParams); } catch (TimeoutException) { (instance as ICommunicationObject).Abort(); throw; } catch (CommunicationException) { (instance as ICommunicationObject).Abort(); throw; } catch (Exception vErr) { (instance as ICommunicationObject).Abort(); throw; } } } } } }
2、添加接口IUser.cs文件,把服务端的IUser.cs文件拷贝过来:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfClient { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IUser”。 [ServiceContract] public interface IUser { [OperationContract] string ShowName(string name); } }
3、调用
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.ServiceModel; namespace WcfClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string uri = "http://localhost:32904/User.svc?wsdl"; object o = WcfChannelFactory.ExecuteMethod<IUser>(uri, "ShowName", this.textBox1.Text.Trim()); this.label1.Text = string.Format("服务:{0}", o.ToString()); } } }
4结果如图:
调用:
注意:WCF的bing方式,根据服务器的配置来,选择合适的bing方式:
WCF中常用的binding方式:
BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务。用于兼容旧的Web ASMX 服务。
WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用于 non-duplex 服务通讯。
WSDualHttpBinding: 和 WSHttpBinding 相比,它支持 duplex 类型的服务。
WSFederationHttpBinding: WS-Federation 安全通讯协议。
NetTcpBinding: 使用 TCP 协议,用于在局域网(Intranet)内跨机器通信。有几个特点:可靠性、事务支持和安全,优化了 WCF 到 WCF 的通信。限制是服务端和客户端都必须使用 WCF 来实现。
NetNamedPipeBinding: 使用命名管道进行安全、可靠、高效的单机服务通讯方式。
NetMsmqBinding: 使用消息队列在不同机器间进行非连接通讯。
NetPeerTcpBinding: 使用 P2P 协议在多机器间通讯。
MsmqIntegrationBinding: 将 WCF 消息转化为 MSMQ 消息,使用现有的消息队列系统进行跨机器通讯。如MSMQ。
名称 |
传输 |
编码 |
共同操作 |
BasicHttpBinding |
HTTP/HTTPS |
Text |
Yes |
NetTcpBinding |
TCP |
Binary |
No |
NetPeerTcpBinding |
P2P |
Binary |
No |
NetNamedPipeBinding |
IPC |
Binary |
No |
WSHttpBinding |
HTTP/HTTPS |
Text,MTOM |
Yes |
WSFederationBinding |
HTTP/HTTPS |
Text,MTOM |
Yes |
WSDualHttpBinding |
HTTP |
Text,MTOM |
Yes |
NetMsmqBinding |
MSMQ |
Binary |
No |
MsmqIntegrationBinding |
MSMQ |
Binary |
Yes |
Binding名称 |
Configuration Element |
描述 |
BasicHttpBinding |
basicHttpBinding |
一个指定用符合基本网络服务规范通讯的binding,它用http进行传输,数据格式为text/xml |
WSHttpBinding |
wsHttpBinding |
一个安全的通用的binding,但它不能在deplex中使用 |
WSDualHttpBinding |
wsDualHttpBinding |
一个安全的通用的binding,但能在deplex中使用 |
WSFederationHttpBinding |
wsFederationHttpBinding |
一个安全的通用的支持WSF的binding,能对用户进行验证和授权 |
NetTcpBinding |
netTcpBinding |
在wcf应用程序中最适合跨机器进行安全通讯的binding |
NetNamedPipeBinding |
netNamedPipeBinding |
在wcf应用程序中最适合本机进行安全通讯的binding |
NetMsmqBinding |
netMsmqBinding |
在wcf应用程序中最适合跨机器进行安全通讯的binding,并且支持排队 |
NetPeerTcpBinding |
netPeerTcpBinding |
一个支持安全的,多机交互的binding |
|
msmqIntegrationBinding |
|
下载源码地址:http://download.csdn.net/detail/ffuqiaoq/8537793
原文:http://www.cnblogs.com/George-Qiao/p/4370930.html