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

FiberModule.Key_DoNotSerialize not working

Open kbrimble opened this issue 10 years ago • 29 comments

Hi,

I have the following code in my Global.asax.cs:

        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            var builder = new ContainerBuilder();
            builder.RegisterType<AgendaSearchService>().Keyed<IAgendaSearchService>(FiberModule.Key_DoNotSerialize).AsImplementedInterfaces().SingleInstance();

            builder.RegisterType<AgendaDialog>().As<IDialog<object>>().InstancePerDependency();

            // Get your HttpConfiguration.
            var config = GlobalConfiguration.Configuration;

            // Register your Web API controllers.
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            // OPTIONAL: Register the Autofac filter provider.
            builder.RegisterWebApiFilterProvider(config);

            // Set the dependency resolver to be Autofac.
            var container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }

And my dialog is constructed like so:

{
    [Serializable]
    [LuisModel("modelId", "subscriptionId")]
    public class AgendaDialog : LuisDialog<object>
    {
        private readonly IAgendaSearchService _searchService;

        public AgendaDialog(IAgendaSearchService searchService)
        {
            SetField.NotNull(out _searchService, nameof(searchService), searchService);
        }

But I still get the Type

'Namespace.AgendaSearchService' in Assembly 'Bot Application1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

exception when trying to resolve the dependencies for my dialog.

My root dialog is resolved using an auto-factory in my controller:

        private readonly Func<IDialog<object>> _makeRootFactory;

        public MessagesController(Func<IDialog<object>> makeRootFactory)
        {
            _makeRootFactory = makeRootFactory;
        }

Is there something that I am missing?

kbrimble avatar Oct 24 '16 11:10 kbrimble

It seems likely the singleton service implementing IAgendaSearchService must still be marked serialized. With the "do not serialize" key, we use a serialization surrogate to write out the type [name] so that when deserialized, the service instance can be recovered from the container with that type [name] as a key.

willportnoy avatar Oct 24 '16 19:10 willportnoy

Does this mean that even if the implementation of IAgendaSearchService is not serializable it won't matter if I mark it as such because it won't be serialised anyway?

What about external dependencies that are not marked as serializable? If they need to be tagged anyway, doesn't that defeat the purpose?

kbrimble avatar Oct 25 '16 07:10 kbrimble

(I'm not completely convinced that the [Serializable] attribute is required to use FiberModule.Key_DoNotSerialize)

Regardless, you can register any serialization surrogates you want in the container - for example, here are some we use:

https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Fibers/Serialization.cs#L51

willportnoy avatar Oct 25 '16 08:10 willportnoy

(I'm not completely convinced that the [Serializable] attribute is required to use FiberModule.Key_DoNotSerialize)

My code didn't work without that attribute, it was the whole point of raising the issue.

I'm confused as why I would want to use the serialization surrogate when I thought that the whole point of the DoNotSerialize key was to avoid having to do this in the first place.

My understanding was that all I had to do was add .Keyed<IAgendaSearchService>(FiberModule.Key_DoNotSerialize) to my binding to allow my dependency to not be serialized but to be injected into the dialog but that doesn't seem the case. That's why I think I must be missing something.

kbrimble avatar Oct 25 '16 09:10 kbrimble

I have experienced the same problem. I did get a nonserializable service working in my Dialog after a lot of trial and error, my guess is that the Key_DoNotSerialize registration only works if you register and call your Dialog with the MakeRoot methods.

Maarten88 avatar Oct 29 '16 14:10 Maarten88

@Maarten88 I am calling my dialog with the make root method but still can't get my non-serializable objects injected.

kbrimble avatar Nov 01 '16 07:11 kbrimble

We have the same issue. Every service we tag with FiberModule.Key_DoNotSerialize is still being serialized.

As far as I was able to tell, problem is that Conversation class creates a separate static container that registers only DialogModule that in turn registers FiberModule. FiberModule has the code to enumerate all the services that are marked with that key. In this method. Because this is all static and private, we are unable to add additional Autofact modules to it. Maybe there is an option to do something during runtime, but I'm not that familiar with Autofact.

We worked around it by creating our own Coversation class, by basically copying the code and adding additional Autofact modules to the static constructor.

grbinho avatar Nov 16 '16 15:11 grbinho

@grbinho you can do builder.Update(Conversation.Container) to override global registrations in there. It's totally nonobvious how Autofac allows this, took me some time to figure out too. I use that now to register my (non serializable) services, so that I can resolve them inside Forms.

Maarten88 avatar Nov 18 '16 01:11 Maarten88

@Maarten88 Tnx for the tip. Much better solution :)

grbinho avatar Nov 18 '16 07:11 grbinho

@willportnoy can you comment or close appropriately?

chrimc62 avatar Nov 27 '16 00:11 chrimc62

@chrimc62 I think the resolution is that services may need to marked as [Serializable] to use FiberModule.Key_DoNotSerialize. Otherwise, the available options are listed here: https://docs.botframework.com/en-us/technical-faq/#how-can-i-reference-non-serializable-services-from-my-c-dialogs

willportnoy avatar Nov 27 '16 01:11 willportnoy

@willportnoy the issue that I originally raised was that marking a dialog that has non-serializable constructor parameters as serializable throws exceptions on serialisation when using FiberModule.Key_DoNotSerialize. Your solution doesn't solve that problem. I still believe that this is a bug. I can provide more examples if necessary.

kbrimble avatar Nov 27 '16 09:11 kbrimble

I'm happy to reopen for further discussion. The "do not serialize" key just means "recover the object instance, keyed by object type, from the container on deserialization". As far as I know, it still has to be marked with the serializable attribute. I'd be happy to fix, if it's fixable.

Longer term, we might change the serialization framework as we move to .net core.

willportnoy avatar Nov 27 '16 13:11 willportnoy

My guess is that it's still trying to serialize the instance rather than ignore it and then recover it later.

I've had a brief look at the code and on serialization it does check if it has the Key_DoNotSerialize but it looks like it's not being respected for whatever reason.

kbrimble avatar Nov 27 '16 13:11 kbrimble

@kbrimble

I had the same issue (started with code from Alarmbot sample). As far as I was able to tell from the framework code, "do not serialize" only applies to the Autofac container that was registered in a static Conversation class. Don't know it this is by design, or a bug.

As @Maarten88 suggested, you can do something like this in your Global.asax

// Set the dependency resolver to be Autofac.			
builder.Update(Conversation.Container);
config.DependencyResolver = new AutofacWebApiDependencyResolver(Conversation.Container);

grbinho avatar Nov 28 '16 08:11 grbinho

@grbinho we've been creating a new container in addition to the Conversation.Container. I'll see if registering our dependencies into the Conversation container makes a difference.

kbrimble avatar Nov 28 '16 09:11 kbrimble

@kbrimble I noticed this issue is still open. Do you need any further help on this?

carlosscastro avatar Mar 10 '17 19:03 carlosscastro

@carlosscastro I'll have a look today as to whether using the technique described in this comment solves my issue and close if so 👍

kbrimble avatar Mar 13 '17 09:03 kbrimble

Great @kbrimble , let us know how that goes.

carlosscastro avatar Mar 13 '17 21:03 carlosscastro

@grbinho @Maarten88 anything else you need to do to get it working? When I replace the normal builder.Build() with builder.Update(), my project loses the ability to resolve the LuisService instance that I'm registering in my module class. Using builder.Build() it resolves fine but I have the same serialization issues.

jsiegmund avatar Apr 17 '17 12:04 jsiegmund