首页 新闻 会员 周边

关于WCF FaultException异常返回给客户端

0
悬赏园豆:60 [待解决问题]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Contracts
{
    [DataContract]
    public class ExceptionMsg
    {
        [DataMember]
        public string Message { get; set; }
        [DataMember]
        public string ErrorCode { get; set; }

    }
}

 [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        [FaultContract(typeof(ExceptionMsg))]
        int Devide(int x,int y);

        // TODO: 在此添加您的服务操作
    }
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
    public class ServiceCalculator : Contracts.ICalculator
    {
        public int Devide(int x, int y)
        {
            
            
            if (y == 0)
            {
                ExceptionMsg exMsg = new ExceptionMsg();
                exMsg.Message = "除数不能为0";
                exMsg.ErrorCode = "11111";
                throw new FaultException<ExceptionMsg>(exMsg);
            }
            
            return x / y;

        }

}

为什么不能返回异常给客户端呢,而是直接就出错了呢,throw new FaultException<ExceptionMsg>(exMsg);不起作用,代码还往下执行。

问题补充:

我的配置是<system.serviceModel>
    <services>
      <service name="Services.ServiceCalculator" behaviorConfiguration="Debugging">
        <endpoint address="net.tcp://127.0.0.1:6564/ServiceCalculator" binding="netTcpBinding"
          bindingConfiguration="" contract="Contracts.ICalculator" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Debugging">
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

有人知道吗?在这里先谢谢大家了

冷火的主页 冷火 | 初学一级 | 园豆:4
提问于:2012-03-31 17:10
< >
分享
所有回答(4)
0

客户端捕获这个异常了吗?

dudu | 园豆:30994 (高人七级) | 2012-03-31 17:19

直接要服务端异常了

支持(0) 反对(0) 冷火 | 园豆:4 (初学一级) | 2012-03-31 17:23

我就是想输出自定义异常给客户端

支持(0) 反对(0) 冷火 | 园豆:4 (初学一级) | 2012-03-31 17:23
支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2012-04-01 10:15
0

http://msdn.microsoft.com/zh-cn/library/ms576199.aspx可以看看這個說明

無限遐想 | 园豆:3740 (老鸟四级) | 2012-03-31 17:24

这个我也看过了,不是很理解。大侠帮忙改一下吧

支持(0) 反对(0) 冷火 | 园豆:4 (初学一级) | 2012-03-31 17:31

@冷火: 你要這樣寫

throw new FaultException<ExceptionMsg>(new ExceptionMsg("A Greeting error occurred. You said: " + msg));
把錯誤信息放到ExceptionMsg類中,
  [DataContractAttribute]  public class ExceptionMsg{   
private string report; public ExceptionMsg(string message)
{ this.report = message; } [DataMemberAttribute]
public string Message { get { return this.report; }
set { this.report = value; } } }
支持(0) 反对(0) 無限遐想 | 园豆:3740 (老鸟四级) | 2012-03-31 17:34

@無限遐想: 怪事,还是一样出错

支持(0) 反对(0) 冷火 | 园豆:4 (初学一级) | 2012-03-31 17:40

@冷火: 你的服務 接口中,有暴露那個累嗎?

像這樣

 public interface ISampleService{    [OperationContract]   
[FaultContractAttribute( typeof(GreetingFault),
Action="http://www.contoso.com/GreetingFault",
ProtectionLevel=ProtectionLevel.EncryptAndSign
)] string SampleMethod(string msg); }
支持(0) 反对(0) 無限遐想 | 园豆:3740 (老鸟四级) | 2012-03-31 17:45

@無限遐想: 要怎样暴露呢?

支持(0) 反对(0) 冷火 | 园豆:4 (初学一级) | 2012-03-31 17:46

@冷火:  [DataContractAttribute]
    public class ExceptionMsg
    {
        public ExceptionMsg(string Message)
        {
            this.Message = Message;
        }
        [DataMemberAttribute]
        public string Message { get; set; }

    }

支持(0) 反对(0) 冷火 | 园豆:4 (初学一级) | 2012-03-31 17:48

@無限遐想: 客户端

 CalClients clients = new CalClients();
            try
            {
                int d = clients.Devide(2, 0);
                MessageBox.Show(d.ToString());
               // MemberInfo info =new MemberInfo();
                //info.FirstName = "dfdfdf";
                //clients.AddMember(info);
            }
            catch (FaultException<ExceptionMsg> exception)
            {
                MessageBox.Show(exception.Message);
            }
            catch (FaultException ex)
            {
                MessageBox.Show(ex.Message);
            }

支持(0) 反对(0) 冷火 | 园豆:4 (初学一级) | 2012-03-31 17:49

@冷火: 就像我上面寫的哦

 public interface ISampleService這個是你的接口{    [OperationContract]   
[FaultContractAttribute( typeof(ExceptionMsg),
Action="http://www.contoso.com/ExceptionMsg", 你的 wcf的url
ProtectionLevel=ProtectionLevel.EncryptAndSign
)] string SampleMethod(string msg); }
大致就是這樣,你仔細調試一下哦。
支持(0) 反对(0) 無限遐想 | 园豆:3740 (老鸟四级) | 2012-03-31 17:50
0

感觉你这段代码本身就有问题,一个是返回的exception,一个返回int,应该就报错吧?

happydaily | 园豆:253 (菜鸟二级) | 2012-03-31 18:41
0

http://msdn.microsoft.com/zh-cn/library/system.servicemodel.faultcontractattribute.aspx

chenping2008 | 园豆:9836 (大侠五级) | 2012-09-05 13:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册