我尝试使用mongodb
和dotnet core
Web API为购物车创建Web服务。我的购物车收藏有产品信息。产品必须是多个数组或对象,我尝试了一种解决方案,但出现此错误:
处理请求时发生未处理的异常。
InvalidOperationException:尝试激活“ CartsController”时,无法解析类型为“ CartService”的服务。
这是购物车的外观集合:
{
"_id" : ObjectId("5cab18a057ab66f2536feeb9"),
"Status" : "Inactive",
"Product" : [
{
"_id" : ObjectId("5ca9b27dbec46268305ce427"),
"Quantity" : 1.0,
"Name" : "Samsung",
"Price" : 1000.0
},
{
"_id" : ObjectId("5ca9b27dbec46268305ce427"),
"Quantity" : 2.0,
"Name" : "Samsung",
"Price" : 9999.0
}
],
"modified_on" : "5/4/2019"
}
这是课程模型:
public class Cart
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Status")]
public string Status { get; set; }
[BsonElement("Product")]
public Product Product { get; set; }
[BsonElement("modified_on")]
public DateTime modified_on { get; set; }
}
购物车服务
:public class CartService
{
private readonly IMongoCollection<Cart> _carts;
public CartService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("WebShopDb"));
var database = client.GetDatabase("WebShopDb");
_carts = database.GetCollection<Cart>("Carts");
}
public List<Cart> Get()
{
return _carts.Find(cart => true).ToList();
}
public Cart Get(string id)
{
return _carts.Find<Cart>(cart => cart.Id == id).FirstOrDefault();
}
public Cart Create(Cart cart)
{
_carts.InsertOne(cart);
return cart;
}
public void Update(string id, Cart cartIn)
{
_carts.ReplaceOne(cart => cart.Id == id, cartIn);
}
public void Remove(Cart cartIn)
{
_carts.DeleteOne(cart => cart.Id == cartIn.Id);
}
public void Remove(string id)
{
_carts.DeleteOne(cart => cart.Id == id);
}
}
购物车控制器:
[Route("api/[controller]")]
[ApiController]
public class CartsController : ControllerBase
{
private readonly CartService _cartService;
public CartsController(CartService cartService)
{
_cartService = cartService;
}
[HttpGet]
public ActionResult<List<Cart>> Get()
{
return _cartService.Get();
}
[HttpGet("{id:length(24)}", Name = "GetCart")]
public ActionResult<Cart> Get(string id)
{
var cart = _cartService.Get(id);
if (cart == null)
{
return NotFound();
}
return cart;
}
[HttpPost]
public ActionResult<Cart> Create(Cart cart)
{
_cartService.Create(cart);
return CreatedAtRoute("GetCart", new { id = cart.Id.ToString() }, cart);
}
[HttpPut("{id:length(24)}")]
public IActionResult Update(string id, Cart cartIn)
{
var cart = _cartService.Get(id);
if (cart == null)
{
return NotFound();
}
_cartService.Update(id, cartIn);
return NoContent();
}
[HttpDelete("{id:length(24)}")]
public IActionResult Delete(string id)
{
var cart = _cartService.Get(id);
if (cart == null)
{
return NotFound();
}
_cartService.Remove(cart.Id);
return NoContent();
}
}
答案 0 :(得分:1)
您需要在IoC中注册该服务。最好的方法是下一个:
使用您将在CartService实现中使用的所有方法创建接口 ICartService :
public interface ICartService {
List<Cart> Get();
Cart Get(string id);
Cart Create(Cart cart);
void Update(string id, Cart cartIn);
void Remove(Cart cartIn);
void Remove(string id);
}
在CartService.cs中实现该接口:
public class CartService : ICartService
{
private readonly IMongoCollection<Cart> _carts;
在Startup.cs中注册该服务(因为.NET Core包含IoC)
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<ICartService, CartService>();
...
对象类型(作用域,瞬态,单例)的快速说明:
瞬态对象总是不同的;提供了一个新实例 每个控制器和每个服务。
作用域内的对象在请求内是相同的,但跨对象却不同 不同的请求。
每个对象和每个请求的单个对象都是相同的。
并将 CartsController 更改为:
[Route("api/[controller]")]
[ApiController]
public class CartsController : ControllerBase
{
private readonly ICartService _cartService;
public CartsController(ICartService cartService)
{
_cartService = cartService;
}
它应该可以工作。