C# 应用程序引发了未处理的异常。System.InvalidOperationException:无法>;解析类型的服务
我有一个简单的asp.net核心web api,我正在尝试使用visual studio模板默认附带的api/值。但是我得到了这个错误: 托管环境:生产内容根路径:C:\api.E-commerce 现在收听:应用程序已启动。按 按Ctrl+C组合键关闭。失败:Microsoft.AspNetCore.Server.Kestrel[13] 连接id“0HL3RHG5NLHAL”:应用程序引发了未经处理的异常。System.InvalidOperationException:无法删除 解析类型的服务 'ECommerce.Api.Repository.ECommerce.iecommercepository'而 正在尝试激活“ContactsApi.Controllers.ValuesController”。C# 应用程序引发了未处理的异常。System.InvalidOperationException:无法>;解析类型的服务,c#,.net,asp.net-web-api,asp.net-core,asp.net-core-mvc,C#,.net,Asp.net Web Api,Asp.net Core,Asp.net Core Mvc,我有一个简单的asp.net核心web api,我正在尝试使用visual studio模板默认附带的api/值。但是我得到了这个错误: 托管环境:生产内容根路径:C:\api.E-commerce 现在收听:应用程序已启动。按 按Ctrl+C组合键关闭。失败:Microsoft.AspNetCore.Server.Kestrel[13] 连接id“0HL3RHG5NLHAL”:应用程序引发了未经处理的异常。System.InvalidOperationException:无法删除 解析类型的服
在 Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp,类型类型,类型要求,布尔值isDefaultParameterRequired)
在lambda_方法(闭包、IServiceProvider、对象[])处 Microsoft.AspNetCore.Mvc.Internal.TypeActivatorCache.CreateInstance[TInstance](IServiceProvider serviceProvider,类型implementationType)位于 Microsoft.AspNetCore.Mvc.Controllers.DefaultControllerFactory.CreateController(ControllerContext (上下文)在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(状态& 接下来,在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u22.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext (上下文)在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(状态& 接下来,在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u20.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.AspNetCore.Builder.RouterMiddleware.d_u4.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d_u18.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d\u 18.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware1.d_u18.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d\u 18.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.d_u7.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.d_u7.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.d_u8.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.d_u3.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.d\uu 2.MoveNext()
启动代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using ContactsApi.Contexts;
using ECommerce.Api.Models.ERP;
using ECommerce.Api.Repository.ERP;
using System.IO;
//using Microsoft.AspNetCore.Mvc;
//using Microsoft.AspNetCore.Mvc.Cors.Internal;
namespace ContactsApi
{
public partial class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
// var host = new WebHostBuilder()
//.UseKestrel()
//.UseContentRoot(Directory.GetCurrentDirectory())
//.UseIISIntegration() // IMPORTANT!!!
//.UseStartup<Startup>()
//.Build();
// host.Run();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
.AddJsonOptions(a => a.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()); ;
//using Dependency Injection
//services.AddSingleton<IContactsRepository, ContactsRepository>();
//services.AddSingleton<ITodoTerrenoRepository, TodoTerrenoRepository>();
services.AddDbContext<ContactsContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AuthentConnection")));
services.AddDbContext<TODOTERRENOContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins", builder => builder.AllowAnyOrigin());
options.AddPolicy("AllowAllHeaders", builder => builder.AllowAnyHeader());
options.AddPolicy("AllowCredentials", builder => builder.AllowCredentials());
});
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = true;
options.ForwardClientCertificate = true;
options.ForwardWindowsAuthentication = true;
});
}
// 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)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors("AllowAllOrigins");
app.UseCors("AllowAllHeader");
ConfigureAuth(app);
//app.UseMvc(routes =>
//{
// routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
//});
//// Shows UseCors with CorsPolicyBuilder.
//app.UseCors("AllowSpecificOrigin");
app.UseMvc();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json.Serialization;
使用Microsoft.EntityFrameworkCore;
使用ContactsApi.context;
使用ECommerce.Api.Models.ERP;
使用ECommerce.Api.Repository.ERP;
使用System.IO;
//使用Microsoft.AspNetCore.Mvc;
//使用Microsoft.AspNetCore.Mvc.Cors.Internal;
名称空间联系人SAPI
{
公共部分类启动
{
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(环境内容根路径)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using ECommerce.Api.Repository.Ecommerce;
namespace ContactsApi.Controllers
{
//[Authorize]
[Route("api/[controller]")]
public class ValuesController : Controller
{
public IEcommerceRepository ContactsRepo { get; set; }
public ValuesController(IEcommerceRepository _repo)
{
ContactsRepo = _repo;
}
// GET api/values
// [HttpGet]
//public IEnumerable<string> Get()
//{
// ContactsRepo.CrearUsuario();
// return new string[] { "value1", "value2" };
//}
// [HttpGet]
[HttpGet("{DatosUsuario}")]
public string GestionarUsuario(string DatosUsuario)
{
return ContactsRepo.GestionarUsuario(DatosUsuario).Result;
}
// GET api/values/5
[HttpGet]
// public string Get(in)
public string Get()
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddTransient<IEcommerceRepository, EcommerceRepository>();
...
}