WCF消息交換模式之請求-響應模式

WCF的消息交換模式(MEP)有三種:請求/響應、單向模式和雙工模式。WCF的默認MEP是請求/響應模式。api

請求/響應模式操做簽名代碼以下,無需指定模式,默認就是。異步

[OperationContractAttribute]  
string Hello(string greeting,string mesg);

[OperationContractAttribute]  
void SaveMesg(string mesg);

請求/響應模式內容:spa

  1. 客戶端能夠傳遞一個或多個參數給服務操做方法,服務操做方法會把返回值傳回給相應的客戶端調用者。
  2. 客戶端會收到服務操做的返回值,即便服務操做返回的是void,客戶端仍回收到空消息。
  3. 客戶端若是是異步調用服務,則無需等待收到返回值就可繼續往下執行其餘代碼,若是同步則需等待返回值。
  4. 若是服務端操做執行報錯,客戶端能夠收到返回的錯誤信息。

參考網址:https://docs.microsoft.com/en-us/dotnet/framework/wcf/designing-service-contractscode

服務操做返回SOAP錯誤信息內容:對象

能夠在服務操做簽名指定返回的錯誤對象爲FaultException <TDetail>,該異常對象會轉換爲FaultContractAttribute指定的SOAP錯誤blog

 接口:
 [OperationContract]
 [FaultContractAttribute(typeof(GreetingFault))]
 string SampleMethod(string msg);
 實現:
 public string  SampleMethod(string msg)
 {
  throw new FaultException<GreetingFault>(new GreetingFault("A Greeting error    occurred. You said: " + msg));
 }

參考網址:https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.faultexception-1?view=netframework-4.8接口

相關文章
相關標籤/搜索