Skip to content

async/await in service registration or service resolution. #2353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aspnet-hello opened this issue Jan 1, 2018 · 3 comments
Closed

async/await in service registration or service resolution. #2353

aspnet-hello opened this issue Jan 1, 2018 · 3 comments
Labels
enhancement This issue represents an ask for new feature or an enhancement to an existing one
Milestone

Comments

@aspnet-hello
Copy link

From @Antaris on Wednesday, October 14, 2015 3:10:50 AM

Is it possible to support async/await and Task based service resolution? I'll walk you through an example:

public class WorkContext : IWorkContext
{
  public WorkContext(ISecurityContext securityContext)
  {
    SecurityContext = securityContext;
  }

  public ISecurityContext SecurityContext { get; private set; }
}

public class WorkContextFactory : IWorkContextFactory
{
  private readonly ISecurityContextFactory _securityContextFactory;

  public WorkContextFactory(ISecurityContextFactory securityContextFactory)
  {
    _securityContextFactory = securityContextFactory;
  }

  public async Task<IWorkContext> CreateWorkContext(CancellationToken cancellationToken = default(CancellationToken))
  {
    // Get the security context for the current user
    var securityContext = await _securityContextFactory.GetForCurrentUser(cancellationToken);

    return new WorkContext(securityContext);
  }
}

In my example above, I'm defining some implementations of abstractions provided by my framework. In a dependency, the ISecurityContextFactory I implement a Task-based async/await model because I don't know that the actual implementation might do some DB work to say load the current user information.

When I come to wire up my services, I want to utilise my WorkContextFactory to build my type (seperation of concerns), so I might do something like the following:

yield return ServiceDescriptor.Scoped<IWorkContextFactory, WorkContextFactory>();
yield return ServiceDescriptor.Scoped<IWorkContext>(sp => sp.GetService<IWorkContextFactory>().CreateWorkContext().Result);

Obviously at the moment, I'm having to block until I get the result of Task<IWorkContext>, but it would be great if I could do something like:

yield return ServiceDescriptor.Scoped<IWorkContext>(sp => sp.GetService<IWorkContextFactory>().CreateWorkContext());

And have the DI system understand the Task result and await.

Is this possible?

Copied from original issue: aspnet/DependencyInjection#303

@aspnet-hello aspnet-hello added this to the Backlog milestone Jan 1, 2018
@aspnet-hello aspnet-hello added enhancement This issue represents an ask for new feature or an enhancement to an existing one repo:DependencyInjection labels Jan 1, 2018
@aspnet-hello
Copy link
Author

From @davidfowl on Wednesday, October 14, 2015 3:14:00 AM

Anything is possible but we so far we haven't been adding features that don't exist in other service providers. This looks like Func or Lazy but it's Task being resolved instead of T. Maybe look at how/if other containers solve this and come back with some more data?

@aspnet-hello
Copy link
Author

From @Antaris on Saturday, October 17, 2015 3:42:34 AM

I don't think this scenario is currently widely supported, I've not found a good working examples, so this looks to be one for the backlog until other containers implement something similar.

@davidfowl
Copy link
Member

Currently there's no such thing as an async constructor so the recommended pattern is to use async methods after synchronously resolving the service.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

No branches or pull requests

2 participants