ComponentNotFoundException using NServiceBus.Newtonsoft.Json

150 views
Skip to first unread message

dun...@gmail.com

unread,
Mar 4, 2016, 4:52:04 PM3/4/16
to Particular Software
We had problems with the included Json.Net core serializer converting some values when equaling some properties that should not be serialized (get/set reference other properties, does not contain their own data). Binary serializer does not have this problem. The solution is to mark the properties as [JsonIgnore], but it wasn't possible with the included serializer. So I found this page and it seems to be the solution to my problem.

Installed the nuget packages, changed the UseSerialization parameter in my services endpoint to NewtonsoftSerializer and it built fine. When starting the services, I receive this exception:

Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service Newtonsoft.Json.JsonWriter was found.

Is this an error with my CastleWindsor usage, or something internal to NServiceBus? (I always had a bit of trouble understanding WindsorContainer rules, but my service doesn't have anything really special in there).


Thanks

Helpful information to include
Product name: NServiceBus
Version:5.0
Stacktrace:
Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service Newtonsoft.Json.JsonWriter was found
   à Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   à Castle.Facilities.TypedFactory.TypedFactoryComponentResolver.Resolve(IKernelInternal kernel, IReleasePolicy scope)
   à Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation)
   à Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation)
   à Castle.DynamicProxy.AbstractInvocation.Proceed()
   à Castle.Proxies.Func`2Proxy_1.Invoke(Stream arg)
   à NServiceBus.Newtonsoft.Json.JsonMessageSerializer.Serialize(Object message, Stream stream) dans c:\TeamCity\buildAgent\work\a1dd2048520fca65\NServiceBus.Newtonsoft.Json\JsonMessageSerializer.cs:l
igne 67
   à NServiceBus.SerializeMessagesBehavior.Invoke(OutgoingContext context, Action next) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\SerializeMessagesBehavior.cs:lig
ne 25
   à NServiceBus.BehaviorChain`1.InvokeNext(T context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 107
   à NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 95
   à NServiceBus.FixSendIntentBehavior.Invoke(OutgoingContext context, Action next) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\FixSendIntentBehavior.cs:ligne 19
   à NServiceBus.BehaviorChain`1.InvokeNext(T context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 107
   à NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 95
   à NServiceBus.CreatePhysicalMessageBehavior.Invoke(OutgoingContext context, Action next) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\CreatePhysicalMessageBehavio
r.cs:ligne 59
   à NServiceBus.BehaviorChain`1.InvokeNext(T context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 107
   à NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 95
   à NServiceBus.PopulateAutoCorrelationHeadersForRepliesBehavior.Invoke(OutgoingContext context, Action next) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Sagas\PopulateAutoCorrelati
onHeadersForRepliesBehavior.cs:ligne 47
   à NServiceBus.BehaviorChain`1.InvokeNext(T context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 107
   à NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 95
   à NServiceBus.MutateOutgoingMessageBehavior.Invoke(OutgoingContext context, Action next) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\MessageMutator\MutateOutgoingMessageBehavior.c
s:ligne 29
   à NServiceBus.BehaviorChain`1.InvokeNext(T context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 107
   à NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 95
   à NServiceBus.SendValidatorBehavior.Invoke(OutgoingContext context, Action next) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\SendValidatorBehavior.cs:ligne 21
   à NServiceBus.BehaviorChain`1.InvokeNext(T context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 107
   à NServiceBus.BehaviorChain`1.Invoke() dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:ligne 52
   à NServiceBus.Pipeline.PipelineExecutor.Execute[T](BehaviorChain`1 pipelineAction, T context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:ligne 130
   à NServiceBus.Pipeline.PipelineExecutor.InvokePipeline[TContext](IEnumerable`1 behaviors, TContext context) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.c
s:ligne 86
   à NServiceBus.Pipeline.PipelineExecutor.InvokeSendPipeline(DeliveryOptions deliveryOptions, LogicalMessage message) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineEx
ecutor.cs:ligne 116
   à NServiceBus.Unicast.UnicastBus.InvokeSendPipeline(DeliveryOptions sendOptions, LogicalMessage message) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\UnicastBus.cs:ligne 62
9
   à NServiceBus.Unicast.UnicastBus.SendMessage(SendOptions sendOptions, LogicalMessage message) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\UnicastBus.cs:ligne 610
   à NServiceBus.Unicast.UnicastBus.Send[T](String destination, Action`1 messageConstructor) dans C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\UnicastBus.cs:ligne 484

william...@particular.net

unread,
Mar 7, 2016, 4:05:09 AM3/7/16
to Particular Software
Hi there!

This is not an issue that we're currently aware of.  To verify that the code works as documented, I made a quick sample using NServiceBus.CastleWindsor and NServiceBus.Newtonsoft.Json and it appears to function correctly.

Are you up to date on your Nuget packages? (NServiceBus version 5.2.14, NServiceBus.CastleWindsor 5.0.0, NServiceBus.Newtonsoft.Json 0.2.0)

If you are and you still receive the error, is there any way that you could put a repro somewhere for us to take a look at?

William

dun...@gmail.com

unread,
Mar 7, 2016, 11:15:21 AM3/7/16
to particula...@googlegroups.com
Hello,

After re-creating a new project from scratch, it first worked. I fiddled a bit trying to see differences with my project and managed to reproduce the problem by adding this line:

container.AddFacility<TypedFactoryFacility>();

(When configuring the WindsorContainer passed to the EndpointConfig via UseContainer)

You can read more about the TypedFactoryFacility here.

Of course, we use it for a separate usage unrelated to nservicebus in our application, but would like to keep the Factories working. Again, it only crash when using NewtonsoftSerializer and not JsonSerializer.

dun...@gmail.com

unread,
Mar 8, 2016, 8:52:11 AM3/8/16
to particula...@googlegroups.com
I uploaded a very small and simple VS2013 project where the exception happens:

<LINK REMOVED>

Sorry for the file host, not sure which one I should be using, this one might be bad (require to wait 30sec) but I know it works.

I've read online that this problem might happens if you call the AddFacility<TypedFactoryFacility>(); after the Components registration, but that's not the case here.

David Boike

unread,
Mar 8, 2016, 9:33:10 AM3/8/16
to Particular Software
Sorry, I removed the link because it looked all sorts of spammy and I don't want to run the risk that someone gets some malware from our forums.

It would be preferable to post a repro to somewhere reputable, perhaps Dropbox, Google Drive, or on GitHub?

It's still not entirely clear to me what you're implying:

1. There's a bug in the NServiceBus CastleWindsor adapter?
2. There's a bug in Castle Windsor itself?
3. There's an oddity with TypedFactoryFacility, but as long as you avoid it, you're fine?
4. Something else?

Thanks,
David

dun...@gmail.com

unread,
Mar 8, 2016, 10:33:17 AM3/8/16
to particula...@googlegroups.com
It was not a dangerous host, just free, but I reuploaded on my personal google drive instead.

https://drive.google.com/file/d/0B57c0TVewRSIVjI4cWstNWJod3c

What am I implying?
There's an incompatibility between the Castle Windsor feature to use factories for dependency injection and the NServiceBus.Newtonsoft.Json 0.2.0 nuget package.
There's no problem configuring Windsor for using factories using the internal JsonSerializer, or including/using Newtonsoft.Json elsewhere in the project (and not using it to serialize via NServiceBus).

Dependency injection is far from my area of expertise, but everything point in a problem with NServiceBus.Newtonsoft.Json

william...@particular.net

unread,
Mar 9, 2016, 12:40:35 AM3/9/16
to Particular Software
Thanks for the repro, I'm working through it now.

William

william...@particular.net

unread,
Mar 9, 2016, 4:27:17 AM3/9/16
to Particular Software
Found the problem.

The cause is the TypedFactoryFacility which uses implicit registration to replace all Func<T>'s with a CastleProxy that wraps the Func<T>, causing the code to try and resolve the types via the container instead of using the code provided in the Func<T>.   The implicit registration looks for any publicly accessible Func<T>'s and wraps them.


The code internal to the NServiceBus.Newtonsoft.Json package has a Func<T> that instantiates a new instance of JsonReader (or JsonWriter, depending on the context). This behavior is being replaced by Castle's TypedFactoryFacility  which is causing the failure as those are not types that Castle should be interfering with.

Do you need to use the TypedFactoryFacility? Or is it possible for you to disable it?

As a work-around you can try disable just the implicit Func<T> portion of the TypedFactoryFacility by registering it's initialization manually, but leaving out the implicit DelegateFactory stuff, e.g:

            var container = new WindsorContainer();
            container.Kernel.Register(new IRegistration[] {
               Component.For<TypedFactoryInterceptor>().NamedAutomatically(TypedFactoryFacility.InterceptorKey),
               Component.For<ITypedFactoryComponentSelector>().ImplementedBy<DefaultTypedFactoryComponentSelector>().NamedAutomatically("Castle.TypedFactory.DefaultInterfaceFactoryComponentSelector"),
            });
            container.Kernel.ComponentModelBuilder.AddContributor(new TypedFactoryCachingInspector());

This will only work if you don't need other Func<T>'s automatically injected into your application. Can you let me know if this will work for you?

I have logged an issue here to track a longer term fix from our side, but I'm not sure when it will be implemented.
Reply all
Reply to author
Forward
0 new messages