当前位置:编程学习 > 网站相关 >>

化零为整WCF(2) - 契约Contract(ServiceContract、OperationContract、DataContract

作者:webabcd


介绍
WCF(Windows Communication Foundation) - 契约(Contract):服务契约(ServiceContract),操作契约(OperationContract),数据契约(DataContract),服务已知类型(ServiceKnownType),数据成员(DataMember)。


示例
1、服务
IPersonManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
    /**//// <summary>
    /// 人员管理接口
    /// </summary>
    // Namespace - 服务契约的命名空间
    // Name - 服务契约的名称(会对应到相关的wsdl,默认情况下本例为接口名“IPersonManager”)
    // ConfigurationName - 服务契约在宿主中所配置的服务名称(默认情况下本例为类的全名“WCF.ServiceLib.Contract.IPersonManager”)
    [ServiceContract(Namespace = "http://webabcd.cnblogs.com", Name = "IPersonManager", ConfigurationName = "ConfigurationNameTest")]
    // 服务已知类型 - Student(数据契约)继承自Person(数据契约),要指定Student为已知类型,其才会被序列化
    [ServiceKnownType(typeof(Student))]
    public interface IPersonManager
    {
        /**//// <summary>
        /// 获取某人的姓名
        /// </summary>
        /// <param name="p">Person对象</param>
        /// <returns></returns>
        // Name - 操作契约的名称(会对应到相关的wsdl,默认情况下本例为方法名“GetName”)
        [OperationContract(Name="GetPersonName")]
        string GetName([MessageParameter(Name = "person")] Person p);
    }
}
PersonManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
    /**//// <summary>
    /// 人员管理类
    /// </summary>
    public class PersonManager : IPersonManager
    {
        /**//// <summary>
        /// 获取某人的姓名
        /// </summary>
        /// <param name="p">Person对象</param>
        /// <returns></returns>
        public string GetName(Person p)
        {
             return "Name: " + p.Name;
        }
    }
}
Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
    /**//// <summary>
    /// Person的实体类
    /// </summary>
    // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Person”)
    [DataContract(Name = "PersonModel")]
    public class Person
    {
        /**//// <summary>
        /// Person的实体类的Age属性
        /// </summary>
        // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Age”)
        // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
        // Order - 数据成员在相关的wsdl中的顺序
        // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
        [DataMember(Name = "PersonAge", IsRequired = false, Order = 1)]
        public int Age { get; set; }

        /**//// <summary>
        /// Person的实体类的Name属性
        /// </summary>
        // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Name”)
        // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
        // Order - 数据成员在相关的wsdl中的顺序
        // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
        [DataMember(Name = "PersonName", IsRequired = false, Order = 0)]
        public string Name { get; set; }
    }
}

Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
    /**//// <summary>
    /// Student的实体类
    /// </summary>
    // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Student”)
    [DataContract(Name = "StudentModel")]
    public class Student : Person
    {
        /**//// <summary>
        /// Student的实体类的School属性
        /// </summary>
        // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“School”)
        // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
        // Order - 数据成员在相关的wsdl中的顺序
        // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
        [DataMember(Name = "School", IsRequired = false, Order = 0)]
        public string School { get; set; }
    }
}


2、宿主
PersonManager.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Contract.PersonManager" %>
Web.config
<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ContractBehavior">
          <!--httpGetEnabled - 使用get方式提供服务-->
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
 

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,