1)在我的Windows应用程序客户端中,使用ChannelFactory方法调用多个wcf服务。在其中一个客户端方法中逐个调用多个服务,在空闲超时后,每当Faulted事件触发时我为一个服务对象创建一个通道但是如何维护同时也出现故障的另一个服务通道?或者更简单的话2)在我的Windows应用程序客户端中,使用ChannelFactory方法使用wcf服务,我可以在服务工厂类中捕获Faultexception,我在那里为该服务创建通道吗?
一般来说,我们可以使用FaultException类来捕获服务器端抛出的异常。
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);
}
通过这种方式,客户端可以正确捕获服务器抛出的异常。如果我们需要统一服务器抛出的所有错误,我们可以实现IErrorhandler接口并编写自定义错误处理类。我做了一个演示希望它对你有用。
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();
}