亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Autofac 和 ASP .Net Web API 版本

Autofac 和 ASP .Net Web API 版本

C#
阿波羅的戰車 2023-07-09 10:27:12
請注意,我使用的是 Autofac,但是當我運行 API 并使用此 URL 與Postman進行檢查時,:http://localhost:1234/api/calculations/corpname&51&114&1045但我有一個用于計算控制器的公共無參數構造函數:using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Web;using System.Web.Http;using AutoMapper;using MyAppTools.Data;using MyAppTools.Models;namespace MyAppTools.Controllers{? ? public class CalculationsController : ApiController? ? {? ? ? ? private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();? ? ? ? private readonly IMapper _mapper;? ? ? ? private readonly ICalcRepository _repository;? ? ? ? private readonly IMyModelUserRepository _myModelRepository;? ? ? ? public CalculationsController()? ? ? ? {? ? ? ? }? ? ? ? public CalculationsController(ICalcRepository repository, IMyModelUserRepository myModelRepository, IMapper mapper)? ? ? ? {? ? ? ? ? ? _repository = repository;? ? ? ? ? ? _myModelRepository = myModelRepository;? ? ? ? ? ? _mapper = mapper;? ? ? ? }? ? ? ? public async Task<IHttpActionResult> Get()? ? ? ? {? ? ? ? ? ? try? ? ? ? ? ? {? ? ? ? ? ? ? ? var result = await _repository.GetAllCalculationsAsync();? ? ? ? ? ? ? ? if (result == null) return NotFound();? ? ? ? ? ? ? ? // Mapping? ? ? ? ? ? ? ? // This centralizes all of the config? ? ? ? ? ? ? ? var mappedResult = _mapper.Map<IEnumerable<CalculationModel>>(result);? ? ? ? ? ? ? ? return Ok(mappedResult);? ? ? ? ? ? }? ? ? ? ? ? catch (Exception ex)? ? ? ? ? ? {? ? ? ? ? ? ? ? Logger.Error(ex);? ? ? ? ? ? ? ? // Be careful about returning exception here? ? ? ? ? ? ? ? return InternalServerError();? ? ? ? ? ? }? ? ? ? }到目前為止,我已經在這里檢查解決方案:Autofac 和 ASP .Net MVC 4 Web API https://www.nopcommerce.com/boards/t/38102/none-of-the-constructors-found-with-autofaccoreactivatorsreflectiondefaultconstructorfinder-on-type.aspx但我認為這不適用于我的情況。
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

錯誤消息中有趣的部分是

Autofac.Core.Activators.Reflection.DefaultConstructorFinder使用on 類型找到的任何構造函數都EddiTools.Data.CalcRepository不能使用可用的服務和參數來調用:

無法解析EddiTools.Data.ICalcContext context構造函數的參數Void .ctor(EddiTools.Data.ICalcContext)。

這意味著 Autofac 嘗試創建一個CalcRepository,但沒有ICalcContext可用的。

如果你查看你的注冊,你只注冊了類型,而沒有表明它是一個ICalcContext

    bldr.RegisterType<CalcContext>()
        .InstancePerRequest();

如果你將其更改為

    bldr.RegisterType<CalcContext>()
        .As<ICalcContext>()
        .InstancePerRequest();

您的CalcContext類型將被注冊為 a,ICalcContext這將讓 Autofac 創建您的CalcRepository


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號