Grpc-dotnet: ConfigureServices returning an System.IServiceProvider isn't supported.

Created on 5 Jun 2020  路  7Comments  路  Source: grpc/grpc-dotnet

My trying to integrate grpc to my .Net core project.

enter image description here
im

I create Message project to store code are generated by Grcp tools.
s

and I implement the service in WEB A.
And setup program like this:

s

But I get Error

s

bug

All 7 comments

There is a problem with your Startup.cs file

See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-3.1

services.AddOptions();
            services.AddSingleton<CategoryService.CategoryServiceBase, CategoryServices>();

            services.AddGrpc();

And

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

What is the wrong??

Look at what the error message says in your screenshot, and look at the documentation I linked to. ConfigureServices must return void.

Look at what the error message says in your screenshot, and look at the documentation I linked to. ConfigureServices must return void.

Oh, I see.

This is my startup:

using Autofac;
using Autofac.Extensions.DependencyInjection;
using Consul;
using iNRES.Common;
using iNRES.Common.Consul;
using iNRES.Common.CrossCutting;
using iNRES.Common.Mvc;
using iNRES.Common.SecurityMiddleware;
using iNRES.Common.Swagger;
using iNRES.Service.Category.Application.MappingConfigurations;
using iNRES.Service.Category.Configurations;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Reflection;

namespace iNRES.Service.Category
{
    /// <summary>
    /// Class Startup
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// Configuration
        /// </summary>
        public IConfigurationRoot Configuration { get; private set; }
        public ILifetimeScope AutofacContainer { get; private set; }

        /// <summary>
        /// Container
        /// </summary>
        public IContainer Container { get; private set; }

        /// <summary>
        /// Initialize a new instance of the <see cref="Startup"/> class
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                            .SetBasePath(env.ContentRootPath)
                            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                            .AddEnvironmentVariables();
            this.Configuration = builder.Build();
        }

        /// <summary>
        /// ConfigureServices
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public void ConfigureServices(IServiceCollection services)
        {
            //Add Vnlis Security Middleware
            services.AddVnLisSecurityMiddleware();

            // add CustomizeMVC
            services.AddCustomMvc();

            // setup config connectionstring 
            services.AddConnectionString(Configuration);

            // setup add mediatR
            services.AddMediatR(this.GetType().Assembly);

            // add SwaggerDocs
            services.AddSwaggerDocs();

            // AddCors
            services.AddCors();

            // AddConsul
            services.AddConsul();

            // Add Grpc

            services.AddGrpcService();

            // RegisterService NativeInjectionStrapper
            RegisterServices(services);

        }

        // ConfigureContainer is where you can register things directly
        // with Autofac. This runs after ConfigureServices so the things
        // here will override registrations made in ConfigureServices.
        // Don't build the container; that gets done for you by the factory.
        public void ConfigureContainer(ContainerBuilder builder)
        {
            // Register your own things directly with Autofac, like:
            //builder.RegisterModule(new MyApplicationModule());
        }

        /// <summary>
        /// Configure
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="applicationLifetime"></param>
        /// <param name="startupInitializer"></param>
        /// <param name="loggerFactory"></param>
        [Obsolete]
        public void Configure(IApplicationBuilder app,
                              IWebHostEnvironment env,
                              IApplicationLifetime applicationLifetime,
                              IStartupInitializer startupInitializer,
                              ILoggerFactory loggerFactory,
                              IConsulClient client)
        {
            this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
            // use VnListSecurity
            app.UseVnLis(Configuration);

            // application builder extension
            ApplicationBuilder(app, env, loggerFactory);

            // Use SwaggerDocs
            app.UseSwaggerDocs();

            //Use Consul
            var consulServiceId = app.UseConsul();
            applicationLifetime.ApplicationStopped.Register(() =>
            {
                client.Agent.ServiceDeregister(consulServiceId);
                Container.Dispose();
            });

            startupInitializer.InitializeAsync();

        }

        /// <summary>
        /// Registers the services.
        /// </summary>
        /// <param name="services">The services.</param>
        private static void RegisterServices(IServiceCollection services)
        {
            NativeInjectorBootStrapper.Register(services);
        }

        /// <summary>
        /// ApplicationBuilder
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="loggerFactory"></param>
        private void ApplicationBuilder(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            app.SetupEnv(env, loggerFactory);
        }
    }
}

I'm getting new error:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'Autofac.ContainerBuilder'.
   at Microsoft.Extensions.Hosting.Internal.ConfigureContainerAdapter`1.ConfigureContainer(HostBuilderContext hostContext, Object containerBuilder)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at iNRES.Service.Category.Program.Main(String[] args) in D:\VNPT-project\VNPT-iNRES\VNPT-iNRES.Service.Category\src\iNRES.Service.Category\Program.cs:line 25

I don't know. I'm not familiar with using Autofac with ASP.NET Core.

I don't know. I'm not familiar with using Autofac with ASP.NET Core.

Ok, Thanks a lot.

I want ask you a question: can I using both grpc server and client in a WEB project? How can do it

Was this page helpful?
0 / 5 - 0 ratings