WCF開發之異常與錯誤處理
對於WCF中的異常與錯誤處理我們必須先了解一個概念SOAP Faults,它實際上就是在服務端和客戶端之間來傳遞錯誤資訊的一種載體。
• 公共語言執行時(CLR)異常無法跨越服務邊界
– 未捕捉異常最多到達服務通道(service channel)
– 在報告給客戶端之前必須要進行序列化
• 所有的異常都被序列化為SOAP faults
– 基於標準的,可互操作的
– 給予SOAP 1.1 或者 SOAP 1.2 格式
SOAP1.1
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
An invalid operation has occurred.
</faultstring></s:Fault></s:Body></s:Envelope>
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
SOAP1.2
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">
http://www.w3.org/2005/08/addressing/soap/fault
</a:Action><a:RelatesTo>
urn:uuid:64c5619c-99c3-4a83-9bdcfcbb6f399f93</
a:RelatesTo></s:Header
An invalid operation
has occurred.
</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
非捕捉的異常
• CLR異常會從業務邏輯元件傳遞到服務層
• 在預設狀態下,異常的細節不會與客戶端應用程式共享
• 返回一個通用的SOAP Fault
丟擲異常
• 考慮下面這個CLR異常:
throw new InvalidOperationException("The method or operation is not implemented.");
• 如果沒有被捕捉到,服務模型將會返回一個SOAP Fault
– 預設情況下,異常中不包含細節資訊
– 能夠選擇新增細節資訊
• SOAP 格式依賴於繫結(binding)
IncludeExceptionDetailsInFaults(開啟開關[true]就可以看到具體的異常資訊了)
• IncludeExceptionDetailsInFaults
– Debugging行為控制如何對待未捕捉的異常
– 如果被開啟,生成的SOAP fault中,異常細節部分將包括棧跟蹤的資訊
• 傳送呼叫棧跟蹤細節是有風險的
• 只用於除錯目的
在<serviceDebug>區域中配置服務的行為:
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
behaviorConfiguration="serviceBehavior"><endpoint address="http://localhost:8000/Service"
contract="ExceptionService.IService" binding="wsHttpBinding"/></service></services><behaviors><serviceBehaviors><behavior name="serviceBehavior"><serviceDebug includeExceptionDetailInFaults ="true"/></behavior></serviceBehaviors></behaviors></system.serviceModel>
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
使用ServiceBehaviorAttribute來初始化行為:(code方式)
[ServiceBehaviorAttribute(IncludeExceptionDetailsInFaults=true)]
public class Service : IService
下面看一個Demo:
先提出一個知識點:
[OperationContract(IsOneWay=true)]的含義。
IsOneWay=false是預設的設定,是一種阻塞方式的呼叫,可以理解為同步呼叫,當客戶端傳送呼叫服務的請求後,客戶端被阻塞,直到收到服務端處理的返回結果才繼續工作,我們絕大多數的程式都是這種方式。
IsOneWay=true和非同步呼叫方式類似,但是不完全相同。還是有區別的:純非同步的呼叫方式,當客戶端發出呼叫請求後,客戶端不會被阻塞,而是可以繼續工作,此時客戶端不會理會請求的何時被處理了,或者說是不是會被WCF處理。OneWay的話是這樣的,只有當WCF通知了客戶端準備開始處理請求了此時客戶端才可以繼續工作,不被阻塞;但是如果客戶端提交的請求一直不被WCF處理,處於掛起狀態的話,此時客戶端會處於阻塞狀態。
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace BusinessServices
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config. [ServiceContract]
publicinterface IMyServiceA
{
[OperationContract]
void ThrowException();
[OperationContract(IsOneWay=true)]
void ThrowExceptionOneWay();
}
}
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace BusinessServices
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.publicclass MyService : IMyServiceA
{
publicvoid ThrowException()
{
thrownew Exception("This Exception is thrown by Charles");
}
publicvoid ThrowExceptionOneWay()
{
thrownew Exception("This Exception is thrown by Charles");
}
}
}
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
app.config file. System.Configuration does not support config files for libraries. --><system.serviceModel><services><service behaviorConfiguration="BusinessServices.Service1Behavior"
name="BusinessServices.MyService"><endpoint address="" binding="wsHttpBinding" contract="BusinessServices.IMyServiceA"><identity><dns value="localhost"/></identity></endpoint><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/><host><baseAddresses><add baseAddress="http://localhost:8731/WCF/Charlesliu"/></baseAddresses></host></service></services><behaviors><serviceBehaviors><behavior name="BusinessServices.Service1Behavior"><!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment --><serviceMetadata httpGetEnabled="True"/><!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information --><serviceDebug includeExceptionDetailInFaults="true"/></behavior></serviceBehaviors></behaviors></system.serviceModel></configuration>
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
![複製程式碼](http://common.cnblogs.com/images/copycode.gif)
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ClientTest
{
class Program
{
staticvoid Main(string[] args)
{
MyServiceReference.MyServiceAClient proxy =new MyServiceReference.MyServiceAClient();
try
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Calling proxy.ThrowException()");
Console.WriteLine("");
Console.ResetColor();
proxy.ThrowException();
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().ToString());
Console.WriteLine("ERROR: {0}", ex.Message);
}
Console.WriteLine();
Console.WriteLine("Proxy state after exception: {0}", proxy.State);
Console.WriteLine();
if (proxy.State == CommunicationState.Faulted)
proxy =new MyServiceReference.MyServiceAClient();
try
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Calling proxy.ThrowExceptionOneWay()");
Console.ResetColor();
proxy.ThrowExceptionOneWay();
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().ToString());
Console.WriteLine("ERROR: {0}", ex.Message);
}
Console.WriteLine();
Console.WriteLine("Proxy state after exception: {0}", proxy.State);
Console.WriteLine();
相關推薦
WCF開發之異常與錯誤處理
對於WCF中的異常與錯誤處理我們必須先了解一個概念SOAP Faults,它實際上就是在服務端和客戶端之間來傳遞錯誤資訊的一種載體。 • 公共語言執行時(CLR)異常無法跨越服務邊界 – 未捕捉異常最多到達服務通道(service channel) – 在報
Java中的異常與錯誤處理
ror sys lse AC alt xtend tro ima onu 編譯型異常和運行時異常 編譯時異常是指程序正確 而由外界條件不滿足而產生的異常 java 中要求必須去捕捉住這類異常 不然無法通過編譯 運行時異常是指程序存在著bug
Android開發常見異常與錯誤系列(一)
一、前言 這系列文章是自己在平時開發過程中遇到的問題。之前只是記在雲筆記上面,現在整理一下,發出來共享。 ps:像那些什麼沒有註冊Activity呀,許可權呀等最基本的就不再贅述。 二、ADB連線異常 有時我們發現,即使自己從工作管理員裡面把adb.ex
Python學習筆記之面對象與錯誤處理
實現 單繼承 父類 成對 數據類型 itl 同時 屬性 子類 反射 __import__()函數用於加載類和函數 __import__(name[, globals[, locals[, fromlist[, level]]]]) 參數說明: n
Python全棧開發之異常處理
我們 增加 出現 turn 錯誤信息 ado 過程 %s 出現異常 No.1 異常的概念 程序在運行過程中,遇到一個錯誤,會停止程序的運行,並且提示一些錯誤信息,這就是異常 程序停止執行並且提示錯誤信息這個動作,稱為拋出異常 No.2 捕獲異常 簡單捕獲異常格式 捕獲異常最
JAVA開發之 5-常見錯誤的處理
1)找不到檔案: 可能的原因: a)檔案的副檔名隱藏導致編譯失敗; 處理辦法: 將檔案的副檔名顯示出來 選擇工具中的資料夾選項,我們將它開啟; 選擇檢視這個選項卡; 在高階設定這一選項中,
Swift快速入門之可選型別與錯誤處理
可選型別 可選型別是個什麼東西呢?其實就是把空值與非空值也作為不同的型別來處理。這個空指的是變數值為null,而不是空字串的空,空陣列的空,也不是蒼井空的空。 那麼變數的型別除了傳統的型別之外,還要再說明能不能為空,才構成完整的型別。比如可以為nil的整型變
Android開發之WIFI與網路連線處理
網路連線處理 在說WiFi之前,先來說說網路連線處理。 在Android開發過程中,對於一個需要連線網路的Android裝置,對裝置的網路狀態檢測是很有必要的!有很多的App都需要連線網路。判斷裝置是否已經連線網路,並且在連線網路的狀態下判斷是wifi無線連線還是GPRS手機網路連線,這
IOS開發之——objectForKey與valueForKey在NSDictionary中的差異
什麽 iat app 報錯信息 lease 方法 去掉 defined atom 從 NSDictionary 取值的時候有兩個方法,objectForKey: 和 valueForKey:,這兩個方法具體有什麽不同呢? 先從 NSDictionary 文檔中來看這兩個方法
最近遇到的異常與錯誤總結
reference dstat 異常 總結 ria num 空指針 格式化 first 異常 NumberFormatException 數字格式化異常 ArithmeticException 算術異常 ArrayIndexOutOfBoundsException 數組
iOS開發之地圖與定位
control 移動 idt line tor mkmapview 什麽 .cn 構建 不管是QQ還是微信的移動client都少不了定位功能,之前在微信demo中沒有加入定位功能,今天就寫個定位的小demo來了解一下定位和地圖的東西。地圖和定位看上去是挺高大
python 3 之日期與時間處理模塊(date和datetime)
python 時間 處理模塊 前言相關術語的解釋時間的表現形式time模塊datetime模塊時間格式碼總結前言 在開發工作中,我們經常需要用到日期與時間,如: 作為日誌信息的內容輸出計算某個功能的執行時間用日期命名一個日誌文件的名稱記錄或展示某文章的發布或修改時間其他Python中提供了多個用於
異常與錯誤
網絡 strong 進行 機制 滿足 traceback 打印錯誤 projects img 一.異常和錯誤 什麽是錯誤 語法錯誤 無法通過python解釋器的語法檢測 在寫代碼的時候應該規避掉 邏輯錯誤 什麽是異常 程序運行時發生錯誤的信號 什麽時候容
大數據模塊開發之數據預處理
exce ews map 詳細 clas cas stream type repr 1. 主要目的過濾“不合規”數據,清洗無意義的數據格式轉換和規整根據後續的統計需求,過濾分離出各種不同主題(不同欄目path)的基礎數據。2. 實現方式開發一個mr程序WeblogPrePr
android 開發之 ListView 與Adapter 應用實踐
在開發android中,ListView 的應用顯得非常頻繁,只要需要顯示列表展示的應用,可以說是必不可少,下面是記錄開發中應用到ListView與Adapter 使用的例項: ListView 所在頁面中的佈局(listview_item.xml): <?xml version="1.0"
PHP異常與錯誤的區別
PHP錯誤:是屬於php程式自身的問題,一般是由非法的語法,環境問題導致的,使得編譯器無法通過檢查,甚至無法執行的情況。平時遇到的warming、notice都是錯誤,只是級別不同而已。 PHP異常:一般是業務邏輯上出現的不合預期、與正常流程不同的狀況,不是語法錯誤。 // 以除數為0為例,
iOS開發之NSDictionary與NSMutableDictionary
NSDictionary不可變字典 1、[NSDictionary dictionaryWithObjectsAndKeys:..]: 使用鍵值對兒直接建立字典物件,結尾必需使用nil標誌結束。 2、[NSDictionary initWithObjectsAndKeys:..]:
iOS開發之OC與swift開發混編教程,代理的相互呼叫,block的實現。OC呼叫Swift中的代理, OC呼叫Swift中的Block 閉包
本文章將從兩個方向分別介紹 OC 與 swift 混編 1. 第一個方向從 swift工程 中引入 oc類 1. 1 如何在swift的類中使用oc類 1.2 如何在swift中實現oc的代理方法 &
(四)NDK開發之 java 與C/C++ 程式碼互相呼叫
java 呼叫c/c++ 的方法,很簡單。我們勾選建立支援C/C++ 專案的時候,就已經生成了一個Demo 下面主要介紹,C/C++ 呼叫 java 的方法。 一、訪問 java 成員非靜態成員變數: JNI 呼叫java非靜態變數的 方法名格式 : Ge
前端開發之原型與原型鏈
&nb