ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

QUESTION: Does Grace support Azure Functions 3 #271

Closed shahabganji closed 3 years ago

shahabganji commented 3 years ago

Dear @ipjohnson,

I am developing an azure function application and I want to use Grace within that application as the container. Is it possible? if so would you mind guide me through that or put a link in which I could follow the instructions? I know something about other containers like this one, but I prefer to go with Grace.

Does it also supports this scenario?

silkfire commented 3 years ago

I think it probably works the same as in ASP.NET, except you just add the special attribute at the top of your file:

[assembly: FunctionsStartup(typeof(Startup))]

ipjohnson commented 3 years ago

@shahabganji I believe @silkfire is correct. Reading this documentation it sounds like it's supported out of the box with nothing special required from any 3rd party DI container.

Have you tried using it? I'm not familiar with azure functions so I'm a little in the dark in how they work.

shahabganji commented 3 years ago

@silkfire @ipjohnson I am aware of that, and it seems they are using the DryIoc behind the scenes and it supports my scenario actually. Thanks for your time and response guys 🙏

ipjohnson commented 3 years ago

@shahabganji I'm confused what specifically is not being supported?

shahabganji commented 3 years ago

@ipjohnson

In this sample when I use MS.DI it throws an error at runtime and based on this answer by Steven @dotnetjunkie, it seems to be a problem for MS.DI.

https://github.com/jbogard/MediatR/wiki/Container-Feature-Support https://github.com/jbogard/MediatR/issues/305#issuecomment-417002645

In Azure Function it works properly.

ipjohnson commented 3 years ago

@shahabganji gotcha, yes steven is correct it's a feature that is not supported in MS.DI. I was confused and thought there was an issue with Grace which I would want to try and address.

Thanks for the clarification

shahabganji commented 3 years ago

@ipjohnson Actually, I am not sure how to replace the built-in version in azure functions with Grace, I recall in ASP.NET Core we used UseGrace on host builder and then adding ConfigureContainer on the Startup class, and Grace will take place instead of the built-in container, but in Azure Functions, I did not see those capabilities, if you happen to find a way, please ping me 🙏

ipjohnson commented 3 years ago

Looks like all the magic happen here. I can't address it at this moment but I'll try and find 30 minutes this weekend to write up what it would look like if you used Grace instead of Autofac.

shahabganji commented 3 years ago

@ipjohnson yes, that is exactly the place where Autofac uses, and thank you so much for your time and answers

ipjohnson commented 3 years ago

So I spent some time looking at the autofac solution and it's not quite as straight forward as I originally anticipated. It looks like there is no MS sanctioned way of replacing the DI container.

It looks like you end up running two DI containers in the app vs. asp.net core where you have just one service provider. You also end up replacing some of the classes with DI specific implementations which may or may not age well.

I'm hesitant to publish a nuget package with something like this because I don't work with azure functions. That said I'd be willing to put out a repo with the code that you could copy into your project (3-4 files). Would that work?

shahabganji commented 3 years ago

@ipjohnson Thanks for the time and support 🙏🏼 , from the perspective that you described I think it is rational not to publish a NuGet package.

A repo would be awesome actually; at least I'll try it and will update you with the feedback, and maybe in the future releases they make it a bit third-party friendly.

Thank you anyway 👍🏼