WCF异常处理错误
请说明确切的问题是什么 myWCF异常处理错误,wcf,exception,Wcf,Exception,请说明确切的问题是什么 myGlobalErrorHandlerBehaviurattribute类 using System; using System.Collections.Generic; using System.Linq;`enter code here` using System.Text; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; namespace HelloServ
GlobalErrorHandlerBehaviurattribute
类
using System;
using System.Collections.Generic;
using System.Linq;`enter code here`
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace HelloService
{
public class GlobalErrorHandlerBehavoiurAttribute:Attribute,IServiceBehavior
{
private readonly Type errorhanderType;
public GlobalErrorHandlerBehavoiurAttribute(Type errorhanderType)
{
this.errorhanderType = errorhanderType;
}
public void AddBindingParameters(ServiceDescription serviceDescription,
System.ServiceModel.ServiceHostBase serviceHostBase,
System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
IErrorHandler handler = (IErrorHandler)Activator.CreateInstance(this.errorhanderType);
foreach (ChannelDispatcherBase ChannelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channeldispatcher = ChannelDispatcherBase as ChannelDispatcher;
if (channeldispatcher != null)
{
channeldispatcher.ErrorHandlers.Add(handler);
}
}
}
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
}
}
我的服务实现代码
public int Divide(int num,int dem)
{
//if (dem == 0)
//{
// throw new FaultException("Dem can not be zero", new FaultCode("DivideByZero"));
//}
//try
//{
return num / dem;
enter code here
//}
//catch (DivideByZeroException ex)
//{
// DivideByZeroFault dividebyzero = new DivideByZeroFault();
// dividebyzero.Error = ex.Message;
// dividebyzero.Details = "Deno can not be greater than zero";
// throw new FaultException<DivideByZeroFault>(dividebyzero);
//}
}
当我们通过10/0时,它将显示
GlobalErrorHandler
类的“发生一般错误”消息。但它并没有显示出来。我已经在GlobalErrorHandler
类中放置了braekpoints,但它不会在GlobalErrorHandler
类中运行。启用在抛出异常时中断。关掉“只是我的代码”,有点混乱。请告诉我具体要做什么。菜单->调试->异常。。。选中此框可在引发异常时中断。菜单->工具->选项。。。在LHS上选择“调试”,在RHS上找到“仅启用我的代码”并取消检查。我也打开了服务和应用程序的异常抛出。但问题仍然存在。当然它仍然存在!但是现在你可以看到它抛出的点了。
public int Divide(int num,int dem)
{
//if (dem == 0)
//{
// throw new FaultException("Dem can not be zero", new FaultCode("DivideByZero"));
//}
//try
//{
return num / dem;
enter code here
//}
//catch (DivideByZeroException ex)
//{
// DivideByZeroFault dividebyzero = new DivideByZeroFault();
// dividebyzero.Error = ex.Message;
// dividebyzero.Details = "Deno can not be greater than zero";
// throw new FaultException<DivideByZeroFault>(dividebyzero);
//}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
//CompanyService.CompanyPublicServiceClient client = new CompanyService.CompanyPublicServiceClient("BasicHttpBinding_ICompanyPublicService");
int num = Convert.ToInt32(textBox2.Text);
int dem = Convert.ToInt32(textBox3.Text);
//HelloService.IHelloService client = new HelloService.HelloServiceClient("wsHttpBinding_IHelloService");
// HelloService.IHelloService client = new HelloService.HelloServiceClient();
label2.Text = client.Divide(num, dem).ToString();
}
catch (FaultException faultexecption)
{
label2.Text = faultexecption.Message;
}