using System.Threading.Tasks; using MyProject.Services; public class UserService { private readonly IUsersService _usersService; public UserService(IUsersService userService) { _usersService = userService; } public async TaskAddUserAsync(string name, string email) { var user = new User(name, email); await _usersService.AddUserAsync(user); return user; } }
using Microsoft.Extensions.DependencyInjection; using MyProject.Services; public class Program { public static void Main(string[] args) { var services = new ServiceCollection(); services.AddScopedIn this example, we have a console application that uses dependency injection to create an instance of the UserService class. The AddUserAsync method is called to create a new user and the returned user object is printed to the console. The UsersService class is assumed to be included in a separate package or library.(); var serviceProvider = services.BuildServiceProvider(); var userService = serviceProvider.GetService (); var user = userService.AddUserAsync("John Doe", "[email protected]").Result; Console.WriteLine("User created: " + user.Name); } }