WCF设计服务协议(一)

实现WCF的步骤如下:

  1. 设计服务协议
  2. 实现服务协议
  3. 配置服务
  4. 托管服务
  5. 生成客户端(这步可有可无)

设计或定义服务协议要么使用接口,要么使用类。建议接口,使用接口好处一堆例如修改接口的实现,但是服务协定有无需改变。

设计服务协议,接口上使用 ServiceContractAttribute ,方法上使用OperationContractAttribute。

服务协议的每个方法的参数和返回值的类型都必须是可序列化的,默认.net自带的类型都是可序列化的。

如果有在接口里面使用到自定义的类型,就要使用到数据协议了,那自定义的类型 使用DataContractAttribute,里面属性成员使用DataMemberAttribute。

namespace DataContractinWCF.Web
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        List<Customer> GetCustomerData(int CustomerID);
    }
    [DataContract]
    public class Customer
    {
        private string m_Name;
        private int m_Age;
        private int m_Salary;
        private string m_Designation;
        private string m_Manager;
        [DataMember]
        public string Name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
            }

        }

        [DataMember]
        public int Age
        {
            get
            {
                return m_Age;
            }
            set
            {
                m_Age = value;
            }
        }

        [DataMember]
        public int Salary
        {
            get
            {
                return m_Salary;
            }
            set
            {
                m_Salary = value;
            }
        }
        [DataMember]
        public string Designation
        {
            get
            {
                return m_Designation;
            }
            set
            {
                m_Designation = value;
            }
        }
        [DataMember]
        public string Manager
        {
            get
            {
                return m_Manager;
            }
            set
            {
                m_Manager = value;
            }
        }
    }
}
View Code

wcf的消息交换有三种模式:请求/答复、单向、双工。

默认是“请求/答复”模式,即使方法返回值是void,也可以是请求/答复。这种模式优点是,服务发生错误会返回soap错误

[OperationContractAttribute]  
string Hello(string greeting);

[OperationContractAttribute]  
void Hello(string greeting);
View Code

单向:设置IsOneWay=true,服务操作方法返回值必须是void。客户端发送消息后,就不再等待服务操作。即使服务报错也不返回,只能知道发送消息报错。

[OperationContractAttribute(IsOneWay=true)]  
void Hello(string greeting);
View Code

双工通信:比前两种复杂。。。跳过先。

如果方法参数有ref参数或out参数,不能使用单向通信,因为必须有返回响应值。

 关于消息保护级别:重要的就保护,不重要不保护,提高性能。级别有三种,None、Sign、EncrypteAndSign。

namespace System.Net.Security
{
    //
    // 摘要:
    //     指示为已经过身份验证流请求的安全服务。
    public enum ProtectionLevel
    {
        //
        // 摘要:
        //     仅使用身份验证。
        None = 0,
        //
        // 摘要:
        //     数据签名,以帮助确保传输数据的完整性。
        Sign = 1,
        //
        // 摘要:
        //     加密和签名数据,以帮助确保保密性和传输数据的完整性。
        EncryptAndSign = 2
    }
}
View Code

The Protection levels can be done at all the levels:下面的可以覆盖上面的,下面不设就会取上面的保护级别作为默认值。顶层ServiceContract默认值为默认值为 System.Net.Security.ProtectionLevel.None

    • Service Contract
      • Operation Contract
        • Message Contract
          • Message Header
            • Message Body

使用例子:DataContract和DataMember无法控制保护级别

namespace WcfService1
{
    [MessageContract(ProtectionLevel = ProtectionLevel.Sign)]
    public class Customer
    {
        [MessageHeader(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
        public string EmpID;
        [MessageBodyMember(ProtectionLevel = ProtectionLevel.None)]
        public string Name;
        [MessageBodyMember]
        public string Designation;
        [MessageBodyMember]
        public int Salary;
        [MessageBodyMember]
        public string Location;
    }
}
View Code

ProtectionLevel为None

 

 

 ProtectionLevel为Sign

 

 

 ProtectionLevel为EnptryAndSign

 

 

 

 

 

 

好文章:

http://www.wcftutorial.net/Message-Contract.aspx

http://www.topwcftutorials.net/2014/02/datacontract-vs-messagecontract-in-wcf.html

https://www.c-sharpcorner.com/article/protection-level-in-wcf/

https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/designing-service-contracts

 

posted @ 2020-07-07 17:30  舒碧  阅读(665)  评论(0编辑  收藏  举报