本文参考自:http://www.cnblogs.com/wangweimutou/p/4414393.html,纯属读书笔记,加深记忆
一、简介
当我们打开WCF基础客户通道,无论是显示打开还是通过调用操作自动打开、使用客户端或者通过对象调用操作,或者关闭基础客户端通道,都会在客户端应用程序中出现异常,WCF是基于网络的通讯服务,错误异常也是要基于消息传递的,在WCF中提供了一个错误消息处理的类FaultException,WCF客户端可以通过它,来接收服务端传递回来的异常信息。
二、WCF异常类型
1、意外异常:意外异常包括
(1)、灾难性故障(OutOfMemoryException)
(2)、编程错误(ArgumentException(参数异常)和InvalidOperationException(无效的操作异常))
通常没有有效的方法来处理意外错误,所以通产不应该在调用WCF客户端时捕获这些异常
2、预期异常:预期异常包括
(1)、TimeoutException
(2)、CommunicationException
(3)、CommunicationException 的任何派生类
上面这些异常表明在通信的过程中出现问题,该问题可以通过终止WCF客户端并报告通信故障而得到安全的处理,因为外部因素可能导致任何应用程序中出现这些错误,所以正确的应用程序必须捕获这些异常并在发生异常时进行恢复。
三、代码示例
工程结构如下图所示:
1、WCF服务层搭建:新建契约层、服务层、和WCF宿主,添加必须的引用(这里不会的参考本人前面的随笔),配置宿主,生成解决方案,打开Host.exe,开启服务。具体的代码如下:
ICalculate.cs
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace IService { [ServiceContract] public interface ICalculate { [OperationContract] int Add(int a, int b); } }
IUserInfo.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace IService { [ServiceContract] public interface IUserInfo { [OperationContract] User[] GetInfo(int? id); } [DataContract] public class User { [DataMember] public int ID { get; set; } [DataMember] public string Name { get; set; } [DataMember] public int Age { get; set; } [DataMember] public string Nationality { get; set; } } }
注:必须引入System.Runtime.Serialization命名空间,应为User类在被传输时必须是可序列化的,否则将无法传输
Calculate.cs
using IService; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Service { public class Calculate : ICalculate { public int Add(int a, int b) { return a + b; } } }
UserInfo.cs
using IService; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Service { public class UserInfo : IUserInfo { public User[] GetInfo(int? id) { List<User> Users = new List<User>(); Users.Add(new User { ID = 1, Name = "张三", Age = 11, Nationality = "China" }); Users.Add(new User { ID = 2, Name = "李四", Age = 12, Nationality = "English" }); Users.Add(new User { ID = 3, Name = "王五", Age = 13, Nationality = "American" }); if (id != null) { return Users.Where(x => x.ID == id).ToArray(); } else { return Users.ToArray(); } } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Service; using System.ServiceModel; namespace Host { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(Calculate))) { host.Opened += delegate { Console.WriteLine("服务已经启动,按任意键终止!"); }; host.Open(); Console.Read(); } } } }
App.Config
<?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service name="Service.Calculate" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1234/Calculate/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="IService.ICalculate" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
ok,打开Host.exe
服务开启成功!