botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[C#] Exception when trying to update Autofac Registration

Open ankitbko opened this issue 9 years ago • 19 comments

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 -

  1. Enter correct MicrosoftAppId and MicrosoftAppPassword in App.config.
  2. Run the program, put breakpoint on the catch block. Notice that DummyDialog.MessageRecieved is never called. Exception occurs before this.
  3. Remove the Configure Action 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?

ankitbko avatar Feb 16 '17 13:02 ankitbko

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 avatar Feb 22 '17 16:02 willportnoy

@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 😁 autofac-issue

ankitbko avatar Feb 23 '17 12:02 ankitbko

"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 avatar Feb 27 '17 00:02 willportnoy

@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?

ankitbko avatar Feb 27 '17 03:02 ankitbko

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.

willportnoy avatar Feb 28 '17 16:02 willportnoy

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.

ankitbko avatar Feb 28 '17 19:02 ankitbko

@willportnoy Any thoughts on this? Is putting NonSerialized attribute solving this issue for you too?

ankitbko avatar Mar 05 '17 11:03 ankitbko

@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 avatar Mar 05 '17 19:03 SalimDr

@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 avatar Mar 06 '17 15:03 ankitbko

@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.

dilanmert avatar Mar 06 '17 20:03 dilanmert

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.

angsm avatar Mar 07 '17 07:03 angsm

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

arafattehsin avatar Mar 07 '17 09:03 arafattehsin

@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?

arafattehsin avatar Mar 07 '17 09:03 arafattehsin

@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.Update to update the container registration (keep in mind that this is obsolete) or
  • Try putting NonSerialized attribute over mark field in Fiber.cs as 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 avatar Mar 07 '17 11:03 ankitbko

@ankitbko do you know if it's solved by upgrading the autofac version? It seemed like an autofac issue when I encountered it.

willportnoy avatar Mar 09 '17 21:03 willportnoy

@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).

ankitbko avatar Mar 11 '17 20:03 ankitbko

@willportnoy @ankitbko I updated my bot to 3.8.0 and Autofac to 4.5.0 and error still occurs.

m-ehxan avatar May 10 '17 12:05 m-ehxan

@ankitbko Do you still have this issue? Do you need further assistance?

msmohan avatar Jun 07 '17 01:06 msmohan

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.

willportnoy avatar Jul 11 '17 18:07 willportnoy