复习WCF(5)----- xxx
在复习WCF(2)那篇的最后面稍微讲了一下数据协定 这里在稍微说一下
实际上就是一个普通的类而已,一个基本的Class。完全可以看成多层架构中的实体层,就只是数据载体而已
区别在于使用DataContractAttribute属性和DataMemberAttribute属性修饰
note:如果某个属性没有用DataMemberAttribute修饰 那客户端的代理类就不会生成此属,DataMemberAttribute对静态成员无效
[DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } }
异常处理 其实和平常的try catch一样:
class Client { static void Main() { DemonstrateCommunicationException(); DemonstrateTimeoutException(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); } static void DemonstrateCommunicationException() { CalculatorClient client = new CalculatorClient(); try { double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); Console.WriteLine("Simulated network problem occurs..."); client.Abort(); value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); Console.WriteLine("Service successfully returned all results."); } catch (TimeoutException exception) { Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } catch (CommunicationException exception) { Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } } static void DemonstrateTimeoutException() { CalculatorClient client = new CalculatorClient(); try { double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); Console.WriteLine("Set timeout too short for method to complete..."); client.InnerChannel.OperationTimeout = TimeSpan.FromMilliseconds(0.001); value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); Console.WriteLine("Service successfully returned all results."); } catch (TimeoutException exception) { Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } catch (CommunicationException exception) { Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } } }
Grass Mud Horse

浙公网安备 33010602011771号