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>
有人知道吗?在这里先谢谢大家了
http://msdn.microsoft.com/zh-cn/library/ms576199.aspx可以看看這個說明
这个我也看过了,不是很理解。大侠帮忙改一下吧
@冷火: 你要這樣寫
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; } } }
@無限遐想: 怪事,还是一样出错
@冷火: 你的服務 接口中,有暴露那個累嗎?
像這樣
public interface ISampleService{ [OperationContract]
[FaultContractAttribute( typeof(GreetingFault),
Action="http://www.contoso.com/GreetingFault",
ProtectionLevel=ProtectionLevel.EncryptAndSign
)] string SampleMethod(string msg); }
@無限遐想: 要怎样暴露呢?
@冷火: [DataContractAttribute]
public class ExceptionMsg
{
public ExceptionMsg(string Message)
{
this.Message = Message;
}
[DataMemberAttribute]
public string Message { get; set; }
}
@無限遐想: 客户端
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);
}
@冷火: 就像我上面寫的哦
public interface ISampleService這個是你的接口{ [OperationContract]
[FaultContractAttribute( typeof(ExceptionMsg),
Action="http://www.contoso.com/ExceptionMsg", 你的 wcf的url
ProtectionLevel=ProtectionLevel.EncryptAndSign
)] string SampleMethod(string msg); }
大致就是這樣,你仔細調試一下哦。
感觉你这段代码本身就有问题,一个是返回的exception,一个返回int,应该就报错吧?
http://msdn.microsoft.com/zh-cn/library/system.servicemodel.faultcontractattribute.aspx