Wcf异常处理方法
1) 在我的windows应用程序客户端中,使用ChannelFactory方法调用多个wcf服务。在一个客户端方法逐个调用多个服务中,空闲超时后,每当发生故障事件时,我都会为一个服务对象创建一个通道,但如何维护同时也发生故障的另一个服务通道? 或者用更简单的话来说Wcf异常处理方法,wcf,faultexception,Wcf,Faultexception,1) 在我的windows应用程序客户端中,使用ChannelFactory方法调用多个wcf服务。在一个客户端方法逐个调用多个服务中,空闲超时后,每当发生故障事件时,我都会为一个服务对象创建一个通道,但如何维护同时也发生故障的另一个服务通道? 或者用更简单的话来说 2) 在我的windows应用程序客户端中,使用ChannelFactory方法使用wcf服务,我可以在为该服务创建通道的service factory类中捕获Faultexception吗?一般来说,我们可以使用Faultexce
2) 在我的windows应用程序客户端中,使用ChannelFactory方法使用wcf服务,我可以在为该服务创建通道的service factory类中捕获Faultexception吗?一般来说,我们可以使用Faultexception类捕获服务器端引发的异常 服务器。
公共类MyService:IService
{
公共字符串SayHello(int值)
{
如果(值)
public class MyService : IService
{
public string SayHello(int value)
{
if (value<=0)
{
throw new FaultException("Parameter should be greater than 0");
}
return "Hello Stranger";
}
}
ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
IService service = factory.CreateChannel();
try
{
Console.WriteLine(service.SayHello(0));
}
catch (FaultException ex)
{
FaultReason reason = ex.Reason;
Console.WriteLine(reason.GetMatchingTranslation().Text);
}
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:1000");
ServiceHost sh = new ServiceHost(typeof(MyService), uri);
sh.Open();
Console.WriteLine("service is ready");
Console.ReadKey();
sh.Close();
}
}
[ServiceContract(Namespace ="mydomain",ConfigurationName ="isv")]
public interface IService
{
[OperationContract]
string Delete(int value);
[OperationContract]
void UpdateAll();
}
[ServiceBehavior(ConfigurationName = "sv")]
public class MyService : IService
{
public string Delete(int value)
{
if (value<=0)
{
throw new ArgumentException("Parameter should be greater than 0");
}
return "Hello";
}
public void UpdateAll()
{
throw new InvalidOperationException("Operation exception");
}
}
public class MyCustomErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
FaultException faultException = new FaultException(error.Message);
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault,"my-error");
}
}
//only need to implement the ApplyDispatchBehavior method.
public class MyEndpointBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
throw new NotImplementedException();
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
throw new NotImplementedException();
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
MyCustomErrorHandler myCustomErrorHandler = new MyCustomErrorHandler();
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(myCustomErrorHandler);
}
public void Validate(ServiceEndpoint endpoint)
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:1000");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
IService service = factory.CreateChannel();
try
{
Console.WriteLine(service.Delete(0));
Console.ReadKey();
}
catch (FaultException ex)
{
FaultReason reason = ex.Reason;
Console.WriteLine(reason.GetMatchingTranslation().Text);
}
try
{
service.UpdateAll();
}
catch (FaultException ex)
{
Console.WriteLine(ex.Reason.GetMatchingTranslation().Text);
}
}
}
[ServiceContract(Namespace = "mydomain", ConfigurationName = "isv")]
public interface IService
{
[OperationContract]
string Delete(int value);
[OperationContract]
void UpdateAll();
}