读书人

|ZYCIIS| WebService怎么返回自定义异

发布时间: 2013-05-02 09:39:29 作者: rapoo

|ZYCIIS| WebService如何返回自定义错误,为什么我返回的是FaultException异常而不是SoapException异常 谢谢
我原来WebService的返回错误的时候ASP.NET是返回一段好多的错误文本
在网上找到的封装错误方法的文章
http://www.cnblogs.com/heekui/archive/2008/02/28/1084312.html
但是我ASP.NET接收到的错误是FaultException异常而不是SoapException异常

那应该如何返回自定义异常处理呢?

谢谢
[解决办法]
看这里:
http://stackoverflow.com/questions/3839317/catching-a-custom-exception-thrown-by-a-webmethod-on-asp-net-webservice
[解决办法]
有那么麻烦么?直接throw 直接就可以获得了啊。Soap的客户端代理直接转化了啊。

/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
throw new Exception("123");
//return "Hello World";
}
}

client:

class Program
{
static void Main(string[] args)
{
using (var client = new ServiceReference1.Service1SoapClient())
{
try
{
var str = client.HelloWorld();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}
Console.Read();
}
}



System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.Ex
ception: 123
在 WebService1.Service1.HelloWorld() 位置 f:\Programming\VSProject2012\Web\We
bService1\WebService1\Service1.asmx.cs:行号 23
--- 内部异常堆栈跟踪的结尾 ---

读书人网 >Web Service

热点推荐