none
Unable to get API project to work RRS feed

  • Question

  • I'm new to C#'s entity framework and a core framework API.  Specifically, I think my problem lies with my not understanding DI.  I'm getting this error when trying to hit the route that I've defined in my controller:

    System.InvalidOperationException: 'Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' in the application startup code.'

    It seems to start here at the app.UseMvc() call:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseHsts();
                }

                app.UseHttpsRedirection();
                app.UseMvc();
            }

    This error is showing up in my startup.cs file.  Here is the ConfigureServices code block:

     public void ConfigureServices(IServiceCollection services)
            {
                var connectionString = "Server=172.22.20.103;Database=CMDB;User Id=username;Password=password;";

                services.AddDbContext<VirtualMachineContext>(options => options.UseSqlServer(connectionString));
                services.AddDbContext<ServiceDeskChangeOrderContext>(options => options.UseSqlServer(connectionString));
                services.AddDbContext<VirtualMachineSizeContext>(options => options.UseSqlServer(connectionString));
            }


    Have I done something wrong or is my problem elsewhere in the code?

    • Moved by CoolDadTx Monday, January 7, 2019 4:42 PM ASP.NET related
    Friday, January 4, 2019 7:59 PM

All replies

  • Try the suggestion that appears in the error text:

       services.AddMvc( );

    (Inside the ConfigureServices function).

    Friday, January 4, 2019 8:37 PM
  • No go, if I try that then any attempt I make at calling a route fails.  It's possible I just don't know what I'm doing.  Here's the route I'm trying to do a GET against; I always get a 404:

    Here is my context file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.EntityFrameworkCore;
    using CloudGlass;

    namespace CloudGlass.Models
    {
        public class VirtualMachineSizeContext : DbContext
        {
            public VirtualMachineSizeContext(DbContextOptions<VirtualMachineSizeContext> options)
                : base(options)
            {
            }

            public List<VirtualMachineSize> GetVirtualMachineSizes()
            {
                List<VirtualMachineSize> list = new List<VirtualMachineSize>();

                list.Add(new VirtualMachineSize("One"));
                list.Add(new VirtualMachineSize("Two"));
                list.Add(new VirtualMachineSize("Three"));

                return list;
            }
        }
    }

    Here is my relevant code in my controller:

    [HttpGet]
            public ActionResult<IEnumerable<VirtualMachineSize>> GetVirtualMachineSizes()
            {
                return _context.GetVirtualMachineSizes();
            }

    Am I just really messing up here?

    Friday, January 4, 2019 9:42 PM
  • The below is how I do it for WebAPI, but the thing is that I am using a Data Access Layer that the WebAPI is using where the connection string is actually passed to  and used for  EF Core.

    You may not be doing what I have done exactly, but you're going to be close to it.

    https://corderoski.wordpress.com/2017/09/18/how-to-read-appsettings-json-from-class-library-in-asp-net-core/

    ASP.NET WebAPI and EF Core can be discussed at het Core forum in ASP.NET forums where posters post about how to get to the connectionstring and use it. 

    What you are trying to do is questionable. 

    https://forums.asp.net/

    using System.Net;
    using DAL;
    using Entities;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Diagnostics;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Serilog;
    
    namespace ProgMgmntCore2Api
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
                Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
            }
    
            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)
            {
                //DAL
                services.AddTransient<IDaoProject, DaoProject>();
                services.AddTransient<IDaoTask, DaoTask>();
                services.AddTransient<IDaoCache, DaoCache>();
    
                //Configuration
                services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
    
                // Add framework services.
                services.AddMvc(options =>
                {
                    options.Filters.Add(new ErrorHandlingFilter(Configuration));
    
                });
    
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory, IApplicationLifetime appLifetime)
            {
                //this is used by Postmon and Global Exception handelling to show exception.
                app.UseExceptionHandler(
                    options =>
                    {
                        options.Run(
                            async context =>
                            {
                                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                                context.Response.ContentType = "text/html";
                                var ex = context.Features.Get<IExceptionHandlerFeature>();
                                if (ex != null)
                                {
                                    var err = $"<h1>Error: {ex.Error.Message} </h1>{ex.Error.StackTrace } {ex.Error.InnerException.Message} ";
                                    await context.Response.WriteAsync(err).ConfigureAwait(false);
                                }
                            });
                    }
                );
    
    
                loggerfactory.AddSerilog();
                app.UseHttpsRedirection();
                app.UseMvc();
    
            }
        }
    }
    

    namespace Entities
    {
        public class ConnectionStrings
        {
            public string DefaultConnection { get; set; }
            public string ProjectManagementConnection { get; set; }
        }
    }
    
    

    using Entities;
    using Microsoft.Extensions.Options;
    
    namespace DAL
    {
        public class AppConfiguration
        {
            private readonly string _projectManagementConnection = "";
            private readonly IOptions<ConnectionStrings> _options;
    
            public AppConfiguration(IOptions<ConnectionStrings> options) 
            {
                _options = options;
                _projectManagementConnection = _options.Value.ProjectManagementConnection;
            }
    
            public string GetProjectMgmntConn() => $"{_projectManagementConnection}";
    
        }
    }
    
    using System.Collections.Generic;
    using System.Linq;
    using DAL.Models.DB;
    using Entities;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    
    namespace DAL
    {
        public class DaoCache : IDaoCache
        {
            private readonly IOptions<ConnectionStrings> _options;
          
            public DaoCache(IOptions<ConnectionStrings> options)
            {
                _options = options;
            }
    
            public DtoCache GetCache()
            {
                var dtocache = new DtoCache
                {
                    ProjectTypes = new List<DtoProjectType>(),
                    Statuses = new List<DtoStatus>(),
                    Resources = new List<DtoResource>(),
                    Durations = new List<DtoDuration>()
                };
    
                using (var context = new ProjectManagementContext(_options))
                {
                    var projectypes = (from a in context.ProjectTypes select a).ToList();
                    CreateProjectTypes(dtocache, projectypes);
    
                    var statuses = (from a in context.Statuses select a).ToList();
                    CreateStatuses(dtocache, statuses);
    
                    var resources = (from a in context.Resources select a).ToList();
                    CreateResources(dtocache, resources);
    
                    var durations = (from a in context.Durations select a).ToList();
                    CreateDurations(dtocache, durations);
                }
    
                return dtocache;
            }
    
            private static void CreateProjectTypes(DtoCache dtocache, List<ProjectTypes> projectypes)
            {
                foreach (var pt in projectypes)
                {
                    var dto = new DtoProjectType
                    {
                        ProjectTypeId = pt.ProjectTypeId,
                        Text = pt.Text,
                        Value = pt.Value
                    };
    
                    dtocache.ProjectTypes.Add(dto);
                }
            }
    
            private static void CreateStatuses(DtoCache dtocache, List<Statuses> statuses)
            {
                foreach (var st in statuses)
                {
                    var dto = new DtoStatus()
                    {
                        StatusId = st.StatusId,
                        Text = st.Text,
                        Value = st.Value
                    };
    
                    dtocache.Statuses.Add(dto);
                }
            }
    
            private static void CreateResources(DtoCache dtocache, List<Resources> resources)
            {
    
                foreach (var rc in resources)
                {
                    var dto = new DtoResource()
                    {
                        ResourceId = rc.ResourceId,
                        Text = rc.Text,
                        Value = rc.Value
                    };
    
                    dtocache.Resources.Add(dto);
                }
            }
    
            private static void CreateDurations(DtoCache dtocache, List<Durations> durations)
            {
    
                foreach (var du in durations)
                {
                    var dto = new DtoDuration()
                    {
                        DurationId = du.DurationId,
                        Text = du.Text,
                        Value = du.Value
                    };
    
                    dtocache.Durations.Add(dto);
                }
            }
        }
    }
    

    using Entities;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Options;
    
    namespace DAL.Models.DB
    {
        public partial class ProjectManagementContext : DbContext
        {
            private readonly IOptions<ConnectionStrings> _options;
            public ProjectManagementContext(IOptions<ConnectionStrings> options)
            {
                _options = options;
            }
    
            public ProjectManagementContext(DbContextOptions<ProjectManagementContext> options)
                : base(options)
            {
            }
    
            public virtual DbSet<Projects> Projects { get; set; }
            public virtual DbSet<Tasks> Tasks { get; set; }
            public virtual DbSet<ProjectTypes> ProjectTypes { get; set; }
            public virtual DbSet<Durations> Durations { get; set; }
            public virtual DbSet<Resources> Resources { get; set; }
            public virtual DbSet<Statuses> Statuses { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseSqlServer(new AppConfiguration(_options).GetProjectMgmntConn());
                }
            }
    }


    {
      "ConnectionStrings": {
        "DefaultConnection": "Dummy",
        "ProjectManagementConnection":
          "Server=DESKTOP-A86QNRQ\\SQLEXPRESS;Database=ProjectManagement;User Id=xx;Password=xyz"
      },
      "Serilog": {
        "Using": ["Serilog.Sinks.Console"],
        "MinimumLevel": "Error",
        "WriteTo": [
          { "Name": "Console" },
          {
            "Name": "RollingFile",
            "Args": {
              "pathFormat": "c:\\logsapi\\log-{Date}.txt",
              "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
            }
          }
        ],
        "Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
        "Properties": {
          "Application": "My Application",
          "Logging": {
            "LogLevel": {
              "Default": "Warning"
            }
          },
          "AllowedHosts": "*"
        }
      }
    }
    

    Friday, January 4, 2019 10:29 PM
  • Hi Farslayer,

    Thank you for posting here.

    Since your question is more related to ASP.NET Core, you could post a new thread in ASP.NET Core forum for suitable support.

    https://forums.asp.net/1255.aspx/1?ASP+NET+Core

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 7, 2019 5:48 AM
  • Please post questions related to web API in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 7, 2019 4:41 PM