One single IConsumer<Fault<TInterface>> for several IConsumer<TInterface>

76 views
Skip to first unread message

Tobias Moe Thorstensen

unread,
May 26, 2016, 7:56:18 AM5/26/16
to masstransit-discuss
Hi!

I have a question regarding Consumers & Exception handling in consumers. 

Consider this working example:

public interface IFoo
{}

public class Foo : IFoo
{}

public class FooConsumer : IConsumer<IFoo>
{
   
public Task Consume(ConsumeContext<IFoo> context)
   
{
     
throw new Exception($"Exception from {this}");
   
}
}

public class FooConsumerFault : IConsumer<Fault<IFoo>>
{
   
public Task Consume(ConsumeContext<Fault<IFoo>> context)
   
{
     
var faults = context.Message.Exceptions;
     
Console.WriteLine(faults);


     
return Task.CompletedTask;
   
}
}


In our case, we will end up with alot of different consumers and our aim is to have just one FaultConsumer, something like this:


public interface IBase {}

public interface IFoo : IBase
{}

public class Foo : IFoo
{}

public class FooConsumer : IConsumer<IFoo>
{
   
public Task Consume(ConsumeContext<IFoo> context)
   
{
     
throw new Exception($"Exception from {this}");
   
}
}


public class BaseConsumerFault : IConsumer<Fault<IBase>>
{
   
public Task Consume(ConsumeContext<Fault<IBase>> context)
   
{
     
var faults = context.Message.Exceptions;
     
Console.WriteLine(faults);
     
return Task.CompletedTask;
   
}
}


Is this possible to achieve? 

Thanks! 


Chris Patterson

unread,
May 26, 2016, 8:43:03 AM5/26/16
to masstrans...@googlegroups.com
Just consume Fault, without any generic type. This way you can have one consumer. You can also use context.TryGetMessage() to cast to specific Fault<T> in your consumer if you like
Within the regular Fault consumer. 

__
Chris Patterson




--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/18f2c626-004f-4cdc-9afb-91a6de47ced4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Moe Thorstensen

unread,
May 26, 2016, 9:13:05 AM5/26/16
to masstransit-discuss
Thanks alot! 
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages