Jadi saya memiliki .NET Core web API dengan konteks data lokalnya sendiri, dan saya ingin menambahkan kemampuan untuk memanggil Microsoft Graph sebagai API hilir.

Namun, ketika saya mencoba menambahkan properti yang diperlukan untuk memanggil Graph API, saya mendapatkan error build:

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'.)

Inilah kelas startup saya:

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<DataContext>(opt =>
            {
                opt.UseSqlite(Configuration.GetConnectionString("DefaultConnection"));
            });
            services.AddCors(opt =>
            {
                opt.AddPolicy("CorsPolicy", policy =>
                {
                    policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:3000");
                });
            });

            services.AddMicrosoftIdentityWebApiAuthentication(Configuration)
                .EnableTokenAcquisitionToCallDownstreamApi()
                .AddInMemoryTokenCaches();

            services.AddMediatR(typeof(List.Handler).Assembly);
            services.AddControllers(opt =>
            {
                var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
                opt.Filters.Add(new AuthorizeFilter(policy));
            })
            .AddFluentValidation(cfg => cfg.RegisterValidatorsFromAssemblyContaining<Create>());
        }

        // 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<ErrorHandlingMiddleware>();
            if (env.IsDevelopment())
            {
                // app.UseDeveloperExceptionPage();
            }

            app.UseCors("CorsPolicy");

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

Dan penangan aplikasi saya untuk menelepon hilir:

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<User> { }

        public class Handler : IRequestHandler<Query, User>
        {
            private readonly ITokenAcquisition _tokenAcquisition;
            private readonly GraphServiceClient _graphServiceClient;
            public Handler(ITokenAcquisition tokenAcquisition, GraphServiceClient graphServiceClient)
            {
                _tokenAcquisition = tokenAcquisition;
                _graphServiceClient = graphServiceClient;
            }

            public async Task<User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _graphServiceClient.Me.Request().GetAsync();
                return user;
            }
        }
    }
}

Mudah-mudahan saya berada di jalur yang benar di sini, tapi tolong beri tahu saya jika tidak.

0
apriestley 20 November 2020, 20:22

1 menjawab

Jawaban Terbaik

Benar jadi ini adalah pengawasan sederhana di pihak saya.

Sesuai @franklores, Anda perlu mendaftarkan Microsoft Graph di layanan kelas startup Anda:

services.AddMicrosoftIdentityWebApiAuthentication(Configuration)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();

Menambahkan yang berikut ke pengaturan aplikasi (cakupan mungkin berbeda):

  "DownstreamAPI": {
    "BaseUrl": "https://graph.microsoft.com/v1.0",
    "Scopes": "user.read"
  },

Dan pastikan untuk menginstal Microsoft.Identity.Web.MicrosoftGraph untuk mengaktifkan fungsi AddMicrosoftGraph().

2
apriestley 23 November 2020, 15:09