[C#] Exception when trying to update Autofac Registration
With container.Update being obsolete, the next possible workaround is to Add Registrations to Child Scopes. However I am facing a very weird issue using it. I am getting the following exception -
"Type 'Microsoft.Bot.Builder.Internals.Fibers.NullWait`1[[Microsoft.Bot.Builder.Dialogs.Internals.DialogTask, Microsoft.Bot.Builder, Version=3.5.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' in Assembly 'Microsoft.Bot.Builder, Version=3.5.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."
Repo - https://github.com/ankitbko/bot-issue-autofac
Steps to reproduce -
- Enter correct MicrosoftAppId and MicrosoftAppPassword in App.config.
- Run the program, put breakpoint on the catch block. Notice that DummyDialog.MessageRecieved is never called. Exception occurs before this.
- Remove the
ConfigureAction from BeginLifetimeScope i.e -Conversation.Container.BeginLifetimeScope(DialogModule.LifetimeScopeTag)Now everything will work perfectly.
The issue arises only when you try to register to a child container. The weird thing is that you may comment the entire Configure method (make it empty) and it would still throw error as long as you pass Configure method as action to BeginLifetimeScope.
Note that the entire thing used to work earlier (v 3.3.0). - https://github.com/ankitbko/bot-issue-autofac/tree/v3.3.0
What am I doing wrong?
I think I saw this issue too - when starting a child scope, all of the keyed services seemed to disappear, causing this logic to fail:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Microsoft.Bot.Builder.Autofac/Fibers/FiberModule.cs#L63
and then a serialization surrogate is not applied to the first FiberModule.Key_DoNotSerialize which happens to be NullWait:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Microsoft.Bot.Builder.Autofac/Fibers/FiberModule.cs#L165
I did not investigate further - I worked around the issue by just creating and updating my container in just one place.
@willportnoy I found that 973229623 commit introduced this bug. I think L73 of fiber.cs was the issue. Putting [NonSerialized] on top of private IWait<C> mark = NullWait<C>.Instance; solves the error, however I don't know what would be its side effect.
Do you think this is the right approach?
PS: I just love git bisect 😁

"git bisect" is awesome, particularly if you have the tests to support its usage. :)
The issue with that fix is that the "mark" field is the field that stores the next wait for the https://github.com/Microsoft/BotBuilder/commit/9732296235a274bcc429f7e647b46fc9d1f3d989 feature to work.
I would have assumed that removing that commit just meant that some other Key_DoNotSerialize instance would be serialized - none of these objects should be serialized and should instead by resolved from the container, and it means that somehow the logic in DoNotSerialize is failing - the only time I've seen that before is when I was using child scopes for updating registrations.
@willportnoy I used the console application as the "test" to each of the commit(I referenced the project directly instead of nuget package). The console app just updates the registration in child scope and starts a dialog. At each bisect I would just run and see if child scope registration caused any exception or not. It seems that the issue started to arise at https://github.com/Microsoft/BotBuilder/commit/9732296235a274bcc429f7e647b46fc9d1f3d989 commit. Everything was working fine till https://github.com/Microsoft/BotBuilder/commit/8768c83dbc548bf1b9a4da95821eded4da5d2fe2.
Well, I was not talking about removing the commit. I just put [NonSerialized] on top of mark field and the child scope registration started working.
Why it is failing only when using child scope to update registration is very weird.
As I mentioned before, since this entire thing was working till v3.3.3(I have multiple bots working just great), what alternatives do you suggest if not using container.Update?
I guess I'm surprised that adding the [NonSerialized] fixed it without further issues - in my experiements, the child scope broke the entire "do not serialize" keyed service thing. If it's just NullWait, maybe there's something wrong with our autofac registration for it.
I ran the tests with [NonSerialized] attribute on mark and 3 of them failed.
- DialogTask_RememberLastWait -
Message: Test method Microsoft.Bot.Builder.Tests.DialogTaskTests.DialogTask_RememberLastWait threw exception: System.NullReferenceException: Object reference not set to an instance of an object. - DialogTask_RememberLastWait_ReturningFromChild -
Message: Test method Microsoft.Bot.Builder.Tests.DialogTaskTests.DialogTask_RememberLastWait_ReturningFromChild threw exception: System.InvalidOperationException: Stack is empty - Dispatch_NextDispatchGroup -
Message: Test method Microsoft.Bot.Builder.Tests.DispatchDialogTests.Dispatch_NextDispatchGroup threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
However I ran Annotated Sandwitch bot and it ran perfectly. One of my other bot also seems to work fine with this attribute on top. But if the tests are failing then there should be some issue.
One other thing I did not understand is that why wait field just below mark does not require [NonSerialized] attribute.
[NonSerialized]
private IWait<C> mark = NullWait<C>.Instance;
private IWait<C> wait = NullWait<C>.Instance;
PS: By the way, Pizza bot was throwing Luis Quota Exceeded.
@willportnoy Any thoughts on this? Is putting NonSerialized attribute solving this issue for you too?
@ankitbko Hi i found the same issue and with the attribute NonSerialized it solve my problem also. but if i take the 3.3.3 package it will work.
@SalimDr Thanks a lot for trying it out. It confirms that this is not a local issue with me. When you put the NonSerialized attribute, did you notice any problem with the bot? Did all the functionality worked?
@ankitbko I'm working on to integrate bot framework(v3.5.3) with skype for business, and try to use LUIS. With this scenario I had same error and your solution worked well for me.
May I know the steps to add the NonSerialized line? Would I need to download the bot builder source code and modify it and create a nuGet package?
Thank you.
I'm still having the issue;
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: Type 'Microsoft.Bot.Builder.Internals.Fibers.NullWait`1[[Microsoft.Bot.Builder.Dialogs.Internals.DialogTask, Microsoft.Bot.Builder, Version=3.5.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' in Assembly 'Microsoft.Bot.Builder, Version=3.5.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
Is there anything I could do to avoid it? I have got the package from NuGet.
Thanks Arafat
@ankitbko I'm trying to run your bot and I am still having a great struggle in executing it. The above issue is one of them. Can you please advise what can be done?
@angsm You will need to clone this repository first. Then go to Fiber.cs and put NonSerialized attribute over private IWait<C> mark = NullWait<C>.Instance;.
You will then need to make a direct project reference from your bot instead of using nuget package.
However, once you have done it recheck all the functionalities of your bot. There are 3 test cases which are failing with the attribute. Let me know if everything works for you.
@arafattehsin As of now using child scope to add registration is not working. You could -
- Use Bot Builder SDK v3.3.3 or
- Use
conatiner.Updateto update the container registration (keep in mind that this is obsolete) or - Try putting
NonSerializedattribute overmarkfield inFiber.csas mentioned above. (this breaks 3 test cases)
@dilanmert Thanks for trying it out. Are all the bot functionalities working after putting this attribute? 3 of the test cases fail after adding the attribute.
I am sorry I was not able to dig deep into this issue. Have been very busy last couple of weeks(this week too). When I get time will try to get to bottom of this and see why the tests are failing.
PS: Debugging serialization issue is tough. 😩
@ankitbko do you know if it's solved by upgrading the autofac version? It seemed like an autofac issue when I encountered it.
@willportnoy I updated Bot.Builder to 3.5.5 and Autofac to 4.4.0 but error still occurs. I don't think its an Autofac issue, since it worked before and stopped working with the commit https://github.com/Microsoft/BotBuilder/commit/9732296235a274bcc429f7e647b46fc9d1f3d989.
Also 2 of the 3 tests which fails with the NonSerialized attribute were also introduced in the same commit. Maybe there is something off with the Autofac registration (not sure).
@willportnoy @ankitbko I updated my bot to 3.8.0 and Autofac to 4.5.0 and error still occurs.
@ankitbko Do you still have this issue? Do you need further assistance?
I believe the above commit should fix this issue, so I'm closing it for now - feel free to open a new issue if you have further problems.