WCF 学习笔记 (1) - Specify Fault Behaviors for the Service
2009-01-03 03:13
549 查看
本帖示范在 WCF 中,如何用 FaultContractAttribute 指定 SOAP 错误,并搭配泛型的 FaultException<自订类> 去封装自订的错误讯息,以回传给客户端。本帖提供 WCF + ASP.NET 3.5 的示例下载。
-------------------------------------------------
本帖的示例代码下载点:
http://files.cnblogs.com/WizardWu/090103.zip
(执行本示例,需要 Visual Studio 2008 + SP1,不需要数据库)
-------------------------------------------------
用 VS 2008 + SP1,双击 WcfServiceLibrary1.sln 打开 solution 后,直接按 F5 执行示例,可看到下图 1 的画面。当您在 TextBox 输入数字并按 Button 时,WCF Service 会用 int.Parse 方法,将输入的字符串转型成数字;当您输入的非数字时,就会在 WCF 中引发转型错误,接着会在 Service1.cs 中抛回一个 FaultException<TDetial>。
Default.aspx.cs
using System.ServiceModel;
using WcfServiceLibrary1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.transferWCF();
}
private void transferWCF()
{
ServiceReference1.Service1Client prox = new ServiceReference1.Service1Client();
int i = 0;
try
{
i = prox.GetData(TextBox1.Text);
Response.Write(i);
}
catch (FaultException<FaultInfo> fault)
{
Response.Write(fault.Detail.Reason);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
上方代码中的 Detail 属性,为 FaultException<TDetail> 泛型类所特有的属性,里然包含错误的详细信息,但旧式非泛型的 FaultException 类没有这个属性。
建置 solution 完成后,若您去浏览 WSDL,会发现里面有 fault contract 的相关信息。
-----------------------------------
参考书籍:
[1] Microsoft .NET Framework 3.5-Windows Communication Foundation, MCTS EXAM 70-503 Training Kit
http://www.microsoft.com/learning/en/us/Books/12486.aspx
http://www.amazon.com/MCTS-Self-Paced-Training-70-503-PRO-Certification/dp/0735625654
-----------------------------------
参考文件:
[2] MSDN Library
FaultContractAttribute 类:
http://msdn.microsoft.com/zh-cn/library/system.servicemodel.faultcontractattribute.aspx
FaultException<TDetail> 泛型类:
http://msdn.microsoft.com/zh-cn/library/ms576199.aspx
FaultException<TDetail>Detail 属性:
http://msdn.microsoft.com/zh-cn/library/ms575596.aspx
[3] WCF学习----我的第一个WCF程序
/article/4679093.html
-----------------------------------
-------------------------------------------------
本帖的示例代码下载点:
http://files.cnblogs.com/WizardWu/090103.zip
(执行本示例,需要 Visual Studio 2008 + SP1,不需要数据库)
-------------------------------------------------
用 VS 2008 + SP1,双击 WcfServiceLibrary1.sln 打开 solution 后,直接按 F5 执行示例,可看到下图 1 的画面。当您在 TextBox 输入数字并按 Button 时,WCF Service 会用 int.Parse 方法,将输入的字符串转型成数字;当您输入的非数字时,就会在 WCF 中引发转型错误,接着会在 Service1.cs 中抛回一个 FaultException<TDetial>。
Default.aspx.cs
using System.ServiceModel;
using WcfServiceLibrary1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.transferWCF();
}
private void transferWCF()
{
ServiceReference1.Service1Client prox = new ServiceReference1.Service1Client();
int i = 0;
try
{
i = prox.GetData(TextBox1.Text);
Response.Write(i);
}
catch (FaultException<FaultInfo> fault)
{
Response.Write(fault.Detail.Reason);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
上方代码中的 Detail 属性,为 FaultException<TDetail> 泛型类所特有的属性,里然包含错误的详细信息,但旧式非泛型的 FaultException 类没有这个属性。
建置 solution 完成后,若您去浏览 WSDL,会发现里面有 fault contract 的相关信息。
-----------------------------------
参考书籍:
[1] Microsoft .NET Framework 3.5-Windows Communication Foundation, MCTS EXAM 70-503 Training Kit
http://www.microsoft.com/learning/en/us/Books/12486.aspx
http://www.amazon.com/MCTS-Self-Paced-Training-70-503-PRO-Certification/dp/0735625654
-----------------------------------
参考文件:
[2] MSDN Library
FaultContractAttribute 类:
http://msdn.microsoft.com/zh-cn/library/system.servicemodel.faultcontractattribute.aspx
FaultException<TDetail> 泛型类:
http://msdn.microsoft.com/zh-cn/library/ms576199.aspx
FaultException<TDetail>Detail 属性:
http://msdn.microsoft.com/zh-cn/library/ms575596.aspx
[3] WCF学习----我的第一个WCF程序
/article/4679093.html
-----------------------------------
相关文章推荐
- Dubbo学习笔记:No provider available for the service ...异常问题的解决
- WCF:The caller was not authenticated by the service. ---> System.ServiceModel.FaultException: The request for security token could not be satisfied because authentication failed.
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.9.Progressbar控件
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.2.更换主题
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.3.创建控件
- 【点击模型学习笔记】Predicting Clicks_Estimating the Click-Through Rate for New Ads_MS_www2007
- 【点击模型学习笔记】Predicting Clicks_Estimating the Click-Through Rate for New Ads_MS_www2007
- Java for Web学习笔记(六八):Service和Repository(3)异步Async和调度Schedule
- Java for Web学习笔记(七十):Service和Repository(5)回调处理Consumer
- Programming WCF Services 学习笔记二、Service Contract
- scala 学习笔记(scala for the impatient)
- How to: Publish Metadata for a WCF Service.(What is the Metadata Exchange Endpoint purpose.)
- LUA学习笔记2-Iterators and the Generic for
- WCF Error RANT: An error occured creating the configuration section handler for system.serviceModel/
- Elasticsearch.The.Definitive.Guide学习笔记 -- 1. You know, for search
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.4.Tabs控件
- Linux系统下Mysql提示you need the SUPER privilege for this operation的解决办法---Linux学习笔记(5)
- Java for Web学习笔记(七一):Service和Repository(6)在Spring框架中使用Listener
- 【应用篇】WCF学习笔记(二):ServiceContract、DataContract <第一部分>
- WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题