title: How can you add a Microsoft Graph client service as a MediatR service in .NET Core 3.1?
body: <p>So I have a .NET Core web API with it's own local data context, and I'd like to add the ability to call Microsoft Graph as a downstream API.</p>
  <p>However, when I try to add the necessary properties to call the Graph API, I get a build error:</p>
  <pre><code>Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MediatR.IRequestHandler`2[Application.Users.Me+Query,Microsoft.Graph.User] Lifetime: Transient ImplementationType: Application.Users.Me+Handler': Unable to resolve service for type 'Microsoft.Graph.GraphServiceClient' while attempting to activate 'Application.Users.Me+Handler'.)
  </code></pre>
  <p>Here is my startup class:</p>
  <pre><code>using API.Middleware;
  using Application.TestEntities;
  using FluentValidation.AspNetCore;
  using MediatR;
  using Microsoft.AspNetCore.Authorization;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Mvc.Authorization;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using Persistence;
  using Microsoft.Identity.Web;
  
  namespace API
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddDbContext&lt;DataContext&gt;(opt =&gt;
              {
                  opt.UseSqlite(Configuration.GetConnectionString(&quot;DefaultConnection&quot;));
              });
              services.AddCors(opt =&gt;
              {
                  opt.AddPolicy(&quot;CorsPolicy&quot;, policy =&gt;
                  {
                      policy.AllowAnyHeader().AllowAnyMethod().WithOrigins(&quot;http://localhost:3000&quot;);
                  });
              });
  
              services.AddMicrosoftIdentityWebApiAuthentication(Configuration)
                  .EnableTokenAcquisitionToCallDownstreamApi()
                  .AddInMemoryTokenCaches();
  
              services.AddMediatR(typeof(List.Handler).Assembly);
              services.AddControllers(opt =&gt;
              {
                  var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
                  opt.Filters.Add(new AuthorizeFilter(policy));
              })
              .AddFluentValidation(cfg =&gt; cfg.RegisterValidatorsFromAssemblyContaining&lt;Create&gt;());
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              app.UseMiddleware&lt;ErrorHandlingMiddleware&gt;();
              if (env.IsDevelopment())
              {
                  // app.UseDeveloperExceptionPage();
              }
  
              app.UseCors(&quot;CorsPolicy&quot;);
  
              app.UseRouting();
  
              app.UseAuthentication();
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllers();
              });
          }
      }
  }
  </code></pre>
  <p>And my application handler for calling downstream:</p>
  <pre><code>using System.Threading;
  using System.Threading.Tasks;
  using MediatR;
  using Microsoft.Graph;
  using Microsoft.Identity.Web;
  
  namespace Application.Users
  {
      public class Me
      {
          public class Query : IRequest&lt;User&gt; { }
  
          public class Handler : IRequestHandler&lt;Query, User&gt;
          {
              private readonly ITokenAcquisition _tokenAcquisition;
              private readonly GraphServiceClient _graphServiceClient;
              public Handler(ITokenAcquisition tokenAcquisition, GraphServiceClient graphServiceClient)
              {
                  _tokenAcquisition = tokenAcquisition;
                  _graphServiceClient = graphServiceClient;
              }
  
              public async Task&lt;User&gt; Handle(Query request, CancellationToken cancellationToken)
              {
                  var user = await _graphServiceClient.Me.Request().GetAsync();
                  return user;
              }
          }
      }
  }
  </code></pre>
  <p>Hopefully I'm on the right track here, but please let me know if I'm not.</p>