How to intercept WCF faults and return custom response instead? - wcf

Consider the following very basic WCF service implementation:
public enum TransactionStatus
{
Success = 0,
Error = 1
}
public class TransactionResponse
{
public TransactionStatus Status { get; set; }
public string Message { get; set; }
}
[ServiceContract]
[XmlSerializerFormat]
public interface ITestService
{
[OperationContract]
TransactionResponse DoSomething(string data);
}
public class TestService : ITestService
{
public TransactionResponse DoSomething(string data)
{
var result = ProcessData(data); // may throw InvalidOperationException
return new TransactionResponse()
{
Status = TransactionStatus.Success,
Message = result
};
}
private string ProcessData(string data)
{
if (data = "foobar")
throw new InvalidOperationException();
return data;
}
}
In the instance that the DoSomething method does throw an InvalidOperationException, I would like to intercept the fault and return a TransactionResponse object, rather than have WCF raise a FaultException with the client. How can I do this without surrounding each method body in a huge try catch statement? Is there some where I can hook into? Can I do this with some sort of attribute or something? An example of how I would like to handle it can be demonstrated using ASP.NET MVC:
public class ApiController : BaseController
{
protected override void OnException(ExceptionContext filterContext)
{
var ex = filterContext.Exception;
var message = HttpContext.IsDebuggingEnabled ? ex.ToString() : ex.Message;
_logger.Error("Error processing request for controller {0}, action {1}",
filterContext.RequestContext.RouteData.Values["controller"],
filterContext.RequestContext.RouteData.Values["action"]);
_logger.Error(ex.ToString());
filterContext.ExceptionHandled = true;
filterContext.Result = ToXml(new ApiResult(false)
{
Message = message
});
}
// ...
}
Using the above method in MVC, I can ensure that no matter which controller action throws an exception, I can handle it and return an appropriately formatted ActionResult containing the necessary info. Is there a way to do this kind of thing with WCF?

Check out the WCF IErrorHandler interface - it allows you to centrally define one way in your service implementation to catch all exceptions and either swallow them, or convert them to WCF-friendly SOAP exceptions. This will make sure the channel between the client and the server isn't faulted, e.g. it can still be used after this call failed.
I don't understand why you'd want to "catch" the SOAP faults and convert those to something else, though.... nor do I know of any support that WCF would give you. The basic assumption is: catch .NET exceptions and convert them into interoperable SOAP faults

Related

Hanfire not logging custom exception with metadata

I have asp.net core 2.1 application along with HangFire 1.6.17. HangFire is configured to execute a background job at certain interval. The background job calls external API using HttpClient. If the http call fails, then the method throws custom exception with metadata. Idea is hangfire will log the exception with metadata. I followed best-practices-for-exceptions to create exception
public class MyHttpRequestException : Exception
{
public string Content { get; private set; }
public string RequestUri { get; private set; }
public string HttpResponse { get; private set; }
public MyHttpRequestException()
{
}
public MyHttpRequestException(string message)
: base(message)
{
}
public MyHttpRequestException(string message, Exception innerException)
: base(message, innerException)
{
}
public MyHttpRequestException(string message, string content, string httpResponse, string requestUri)
: base(message)
{
Content = content;
RequestUri = requestUri;
HttpResponse = httpResponse;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(base.ToString());
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("Content");
sb.AppendLine(Content);
sb.AppendLine("RequestUri");
sb.AppendLine(RequestUri);
sb.AppendLine("HttpResponse");
sb.AppendLine(this.HttpResponse);
return sb.ToString();
}
}
I also have extension method for HttpResponseMessage which ensures API request is successful, and if not throws MyHttpRequestException
public static class HttpResponseMessageExtensions
{
public static async Task EnsureSuccessStatusCodeAsync(this HttpResponseMessage response)
{
if (response.IsSuccessStatusCode)
{
return;
}
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var httpResponse = response.ToString();
var requestUri = response.RequestMessage.RequestUri.ToString()
if (response.Content != null)
response.Content.Dispose();
throw new MyHttpRequestException("Error while making http request.", content, httpResponse, requestUri);
}
}
Here is my background Job which is invoked by Hangfire recurring job scheduler
public async Task DoSomething(string url)
{
var response = await _httpClient.GetAsync(url)
await response.EnsureSuccessStatusCodeAsync();
// do something here if everything is okay
}
Issue
When EnsureSuccessStatusCodeAsync method throws MyHttpRequestException then Hangfire logs the exception as expected, and i see that in HangFire's dashboard. However Hangfire only logs Exception message and stack trace. I don't see my custom properties are being logged ( ie. Content, RequestUri, HttpResponse)
In clssic .NET we use SerializationInfo like this SO post
How do i create a custom exception in .NET Core so metadata will also get logged?
Note:
When the MyHttpRequestException gets thrown i noticed exception's ToString() method is getting called
however, i dont see whatever ToString() returns is getting logged by Hangfire.
I dont know if this is hangfire issue, or i need to implement MyHttpRequestException is different way.
The stack trace that you see in Dashboard is formatted. You can see here and here.
Because this custom stack trace format you can see your custom properties.

WCF oneway exception faults channel

I haven't found a clear answer on this. so if there is already a question about this, my bad.
I have a WCF service that pushes data via a callback method to connected clients. this callback method is oneway. so everytime there is new data I loop over the connected users and push the data.
The problem I have right now is when a client disconnects it throws an error and the channel becomes faulted.
I always thought that oneway didn't care if the message arrives at the destination. So if there's no client, then bad luck. but no exception.
but there is an exception and that exception faults the channel.
Now I've read somewhere that if you enable reliable sessions, that the exception won't fault the channel. Is this true?
How can I prevent that the channel goes into faulted state when an exception happens on a oneway call?
The list of registered and avaiable clients you can store in some resource such as List. Create another interface which exposes Connect/Disconnect methods. Connect is invoked when application starts off and within method client is added to the list. Disconnect in turn is invoked when application shuts down in order to get rid client of list. OnStartup/OnClosing events or their equivalents, depending on what kind of application client is, refer to moment when application is launched and closed. Such a solution ensures that resource stores only users avaiable to be reached.
[ServiceContract]
interface IConnection
{
[OperationContract(IsOneWay = true)]
void Connect();
[OperationContract(IsOneWay = true)]
void Disconnect();
}
[ServiceContract]
interface IServiceCallback
{
[OperationContract(IsOneWay = true)]
void CallbackMethod();
}
[ServiceContract(CallbackContract = typeof(IServiceCallback))]
interface IService
{
[OperationContract]
void DoSth();
}
class YourService : IConnection, IService
{
private static readonly List<IServiceCallback> Clients = new List<IServiceCallback>();
public void Connect()
{
var newClient = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
if (Clients.All(client => client != newClient))
Clients.Add(newClient);
}
public void Disconnect()
{
var client = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
if (Clients.Any(cl => cl == client))
Clients.Remove(client);
}
public void DoSth()
{
foreach(var client in Clients)
client.CallbackMethod();
}
}
At the end expose another endpoint with IConnection so that client can create proxy meant to be used only for connection/disconnection.
EDIT:
I know it has been a while since I posted an answear but I did not find in order to prepare an example. The workaround is to let service's interface derive IConnection and then expose only service as an endpoint. I attach simple example of WCF and WPF app as client. Client's application violates MVVM pattern but in this case it is irrelevant. Download it here.
To add on what Maximus said.
I've implemented this pattern in a class where clients can subscribe to get updates of internal states of a system, so a monitoring client can show graphs and other clients do other stuff like enabling/disabling buttons if some state is active.
It removes faulted channels from the list when they fail. Also all current states are sent when a client connects.
here's the code, hope it helps!
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Publish : IPublish
{
private struct SystemState
{
public string State;
public string ExtraInfo;
}
private static Dictionary<Key<string>, IPublishCallback> mCallbacks = new Dictionary<Key<string>, IPublishCallback>();
private static Dictionary<string, SystemState> mStates = new Dictionary<string, SystemState>();
public void RegisterClient(string name, string system)
{
lock (mCallbacks)
{
IPublishCallback callback = OperationContext.Current.GetCallbackChannel<IPublishCallback>();
Key<string> key = new Key<string>(name, system);
if (!mCallbacks.ContainsKey(key))
{
mCallbacks.Add(key, callback);
}
else
{
mCallbacks[key] = callback;
}
foreach (KeyValuePair<string, SystemState> s in mStates)
{
mCallbacks[key].ServiceCallback(s.Key, s.Value.State, s.Value.ExtraInfo);
}
}
}
public void UnregisterClient(string name)
{
lock (mCallbacks)
{
outer: foreach (var key in mCallbacks.Keys)
{
if (key.Key1 == name)
{
mCallbacks.Remove(key);
goto outer;
}
}
}
}
public void SetState(string system, string state, string extraInfo)
{
lock (mCallbacks)
{
List<Key<string>> toRemove = new List<Key<string>>();
SystemState s = new SystemState() { State = state, ExtraInfo = extraInfo };
SystemState systemState;
if (!mStates.TryGetValue(system, out systemState))
mStates.Add(system, s);
else
mStates[system] = s;
foreach (KeyValuePair<Key<string>, IPublishCallback> callback in mCallbacks)
{
try
{
callback.Value.ServiceCallback(system, state, extraInfo);
}
catch (CommunicationException ex)
{
toRemove.Add(new Key<string>(callback.Key.Key1, callback.Key.Key2));
}
catch
{
toRemove.Add(new Key<string>(callback.Key.Key1, callback.Key.Key2));
}
}
foreach (Key<string> key in toRemove)
mCallbacks.Remove(key);
}
}
}

WCF catch custom FaultException

I Want to use custome FaultException like this
[DataContract]
public class Fault
{
[DataMember]
public string Message { get; set; }
}
public class MyFault<T>:FaultException<T> where T : Fault
{
public MyFault(T faultClass)
: base(faultClass, new FaultReason(faultClass.Message))
{
}
}
...
throw new MyFault<Fault>(new Fault {Message = "hello"});
...
Interface
[OperationContract]
[FaultContract(typeof(Fault))]
string GetData2(int value);
but when it get to the client it's transform to FaultException
try
{
var res = serv.GetData2(1);
}
catch (MyFault<WcfService1Sample.Fault> ex)
{
label1.Text = ex.Reason.ToString(); //-> i want here
}
catch (FaultException<ServiceReference1.Fault> ex)
{
label1.Text = ex.Reason.ToString(); //-> catch here
}
catch (Exception)
{
throw;
}
can i catch custom fault on clint ?
or the WCF automatic convert it to FaultException
how do i make this issue work
thanks kfir
There are two issues, I think. First:
What shambulator said: Your custom Fault class must be
a DataContract, and use
DataMember to mark all properties that shall be serialized.
If this is done, then WCF will
translate a service-side throw new FaultException<MyFault>(new MyFault { ... }); into a message which contains your fault, including the data; and
translate this fault on the client-side to a FaultException<MyFault>.
Second:
This client-side FaultException<> is generated by WCF. Maybe one can register a custom Exception-Translator, but I have never used it, never needed it, and not found such a thing after a one-minute Google search. I'd recommend just going with the FaultException plus custom Fault-type.

How to throw an exception to the client over wcf callback

I have a wcf service which has a one way operation and it sends the result in a callback. Here is my service definition
[ServiceContract(CallbackContract = typeof(IIrmCallback), SessionMode = SessionMode.Required)]
public interface IFileService
{
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
[ServiceKnownType(typeof(StatementFile))]
[ServiceKnownType(typeof(InvoiceFile))]
void UploadFile(IFile file);
}
public interface IFileCallback
{
[OperationContract]
void FileProcessed(string result);
}
public MyService : IFileService
{
IFileCallBack callbackchannel;
object result;
public void UploadFile(IFile file)
{
callbackChannel = OperationContext.Current.GetCallbackChannel<IIrmCallback>();
Task.Factory.StartNew(() => ProcessFile(file));
}
private ProcessFile(IFile file)
{
// file gets processed
callbackChannel.FileProcessed(result)
}
}
So now if there is some error during the file processing is there someway I can throw an exception back to the caller in the callback?
You can use FaultException
When an operation throws an exception, a FaultException will be returned to the client.
You need to implement a FaultContract in your service interface specifying the types of faults that are possible in that given method and then throw a new fault that inherits from FaultException within the method.
On the client, make sure to catch and handle FaultException and Exception objects individually because Exceptions will cause the WCF channel to fault and it cannot be reused, while a FaultException will allow you to continue using the existing channel.

Serializable exception and WCF

if i made my exception Serializable like this article from msdn , so can my exception serialized over WCF ?
Yes you can serialize exceptions and return them with WCF. I wrote an app where it was necessary for the WCF client to have the real exception that occurred on the server; not just a high level fault.
Here are the steps to implement what we have done:
1 - Declare a class EncodedException with a single string property
public class EncodedException
{
public string SerializedException {get;set;}
}
2 - On your service contract add the attribute to indicate that your service could return a FaultException exception.
[ServiceContract()]
public class MyService
{
[OperationContract]
[FaultContract(typeof(EncodedException),
ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public string Method1 ();
}
3 - In you service implementation add a try/catch in all service operations:
public void Method1()
{
try
{
// some code here
}
catch( Exception ex)
{
EncodedException encodedException = Helper.SerializeException( ex );
throw new FaulException<EncodedException>();
}
}
4 - In your client code catch the exception and unwrap it:
public void someMethod()
{
try
{
serviceClient.Method1();
}
catch( FaulException<EncodedException> ex)
{
Exception decodedException = Helper.DeserializeException( ex );
throw new decodedException();
}
}
5 - Write the Helper to serialize/deserialize the exception. If you need help with that part too, let me know.
I hope this helps.
I don't see why not, if you've successfully created an object that can be serialized over classic asmx, then it be fine in WCF.