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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM