WCF步步为营(五):数据契约

1. WCF只能传输序列化的类型,WCF 能自动序列化.net内置的之类型,但是如果需要传输自定义的类型,必须把自定义的类型标注DataContract

image

DataContract标注这个类作为数据契约,DataMember属性指明那些字段公布为原数据,是否必需,顺序是多少。

2. 上面的定义,使得Student可以用在服务契约里,下面的Name可以让客户端的名称和服务端不同。

image

3. 下面是我们生成的代理类,可以看到客户端的名字,而且由于Student的Address未声明DataMember,所以客户端是不可见的

Code
//------------------------------------------------------------------------------

// <auto-generated>

//    This code was generated by a tool.

//    Runtime Version:2.0.50727.1433

//

//    Changes to this file may cause incorrect behavior and will be lost if

//    the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------ 

namespace JackWangServiceClient.CalcService {

    
using System.Runtime.Serialization;

    
using System;   

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.Runtime.Serialization""3.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name
="Student", Namespace="http://schemas.datacontract.org/2004/07/JackWangWCFService")]
    [System.SerializableAttribute()]
    
public partial class Student : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged { 

        [System.NonSerializedAttribute()]
        
private System.Runtime.Serialization.ExtensionDataObject extensionDataField; 
        
private string FirstNameField;

        
private string LastNameField;

        [System.Runtime.Serialization.OptionalFieldAttribute()]

        
private int AgeField;

        [
global::System.ComponentModel.BrowsableAttribute(false)]

        
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {

            
get {

                
return this.extensionDataField;

            }

            
set {

                
this.extensionDataField = value;

            }

        }

 

        [System.Runtime.Serialization.DataMemberAttribute(IsRequired
=true)]

        
public string FirstName {

            
get {

                
return this.FirstNameField;

            }

            
set {

                
if ((object.ReferenceEquals(this.FirstNameField, value) != true)) {

                    
this.FirstNameField = value;

                    
this.RaisePropertyChanged("FirstName");

                }

            }

        }

 

        [System.Runtime.Serialization.DataMemberAttribute(IsRequired
=true)]

        
public string LastName {

            
get {

                
return this.LastNameField;

            }

            
set {

                
if ((object.ReferenceEquals(this.LastNameField, value) != true)) {

                    
this.LastNameField = value;

                    
this.RaisePropertyChanged("LastName");

                }

            }

        }

 

        [System.Runtime.Serialization.DataMemberAttribute(Order
=2)]

        
public int Age {

            
get {

                
return this.AgeField;

            }

            
set {

                
if ((this.AgeField.Equals(value) != true)) {

                    
this.AgeField = value;

                    
this.RaisePropertyChanged("Age");

                }

            }

        }

 

        
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

 

        
protected void RaisePropertyChanged(string propertyName) {

            System.ComponentModel.PropertyChangedEventHandler propertyChanged 
= this.PropertyChanged;

            
if ((propertyChanged != null)) {

                propertyChanged(
thisnew System.ComponentModel.PropertyChangedEventArgs(propertyName));

            }

        }

    }

 

    [System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel""3.0.0.0")]

    [System.ServiceModel.ServiceContractAttribute(ConfigurationName
="CalcService.CalcService")]

    
public interface CalcService {

 

        [System.ServiceModel.OperationContractAttribute(Action
="http://tempuri.org/CalcService/AddInt", ReplyAction="http://tempuri.org/CalcService/AddIntResponse")]

        
long AddInt(int firstInt, int SecondInt);

 

        [System.ServiceModel.OperationContractAttribute(Action
="http://tempuri.org/CalcService/addAgeOfStudent", ReplyAction="http://tempuri.org/CalcService/addAgeOfStudentResponse")]

        JackWangServiceClient.CalcService.Student addAgeOfStudent(JackWangServiceClient.CalcService.Student student);

    }

 

    [System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel""3.0.0.0")]

    
public interface CalcServiceChannel : JackWangServiceClient.CalcService.CalcService, System.ServiceModel.IClientChannel {

    }

 

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel""3.0.0.0")]

    
public partial class CalcServiceClient : System.ServiceModel.ClientBase<JackWangServiceClient.CalcService.CalcService>, JackWangServiceClient.CalcService.CalcService {

 

        
public CalcServiceClient() {

        }

 

        
public CalcServiceClient(string endpointConfigurationName) :

                
base(endpointConfigurationName) {

        }

 

        
public CalcServiceClient(string endpointConfigurationName, string remoteAddress) :

                
base(endpointConfigurationName, remoteAddress) {

        }

 

        
public CalcServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :

                
base(endpointConfigurationName, remoteAddress) {

        }

 

        
public CalcServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :

                
base(binding, remoteAddress) {

        }

 

        
public long AddInt(int firstInt, int SecondInt) {

            
return base.Channel.AddInt(firstInt, SecondInt);

        }

 

        
public JackWangServiceClient.CalcService.Student addAgeOfStudent(JackWangServiceClient.CalcService.Student student) {

            
return base.Channel.addAgeOfStudent(student);

        }

    }

}
 

 

4. 客户端调用示例:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using JackWangServiceClient.CalcService;

namespace JackWangServiceClient

{

    class Program

    {

        static void Main(string[] args)

        {

            CalcServiceClient proxy = new CalcServiceClient();

            long result = proxy.AddInt(50, 60);

            Student myStudent = new Student();

            myStudent.FirstName = "Jack";

            myStudent.LastName = "Wang";

            myStudent.Age = 18;

            Student resultStudent = proxy.addAgeOfStudent(myStudent);

            Console.Out.WriteLine("result from server is:" + result);

            Console.Out.WriteLine(resultStudent.FirstName + "," + resultStudent.LastName + "," + resultStudent.Age);

            Console.ReadLine();

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值