抱歉,我是IOC概念的新手。我一直在尝试在我的Web api项目中实现Unity.WebAPI(5.3.0。),但出现以下错误;
UnityResolver类
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IFService, FService>(new HierarchicalLifetimeManager());
container.RegisterType<IMService, MService>(new HierarchicalLifetimeManager());
container.RegisterType<ITransactionService, TransactionService>(new HierarchicalLifetimeManager());
container.RegisterType<IMRepository, MRepository>();
config.DependencyResolver = new UnityResolver(container);
}
}
Web Api配置
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Global.asax
public class TransactionController : ApiController
{
private readonly ITransactionService _transactionService;
private readonly IMService _mService;
/// <summary>
/// Public constructor to initialize transaction service instance
/// </summary>
public TransactionController(ITransactionService transactiontService, IMService mService)
{
_mService = mService;
_transactionService = transactiontService;
}
[HttpGet]
public IHttpActionResult IsApiAlive()
{
return Ok();
}
控制器
private readonly IMRepository _mRepository;
public TransactionService(IMRepository mRepository)
{
_mRepository = mRepository;
}
TransactionService类的构造函数
create or replace TRIGGER EXP_IMG AFTER INSERT OR UPDATE OF LAST_UPDATE
ON baeume
for each row
when (OLD.LAST_UPDATE != NEW.LAST_UPDATE OR NEW.LAST_UPDATE is not null)
declare
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
DECLARE
t_blob BLOB;
t_len NUMBER;
t_file_name VARCHAR2(1000);
t_output UTL_FILE.FILE_TYPE;
t_TotalSize NUMBER;
t_position NUMBER := 1;
t_chucklen NUMBER := 32767;
t_chuck RAW(32767);
t_remain NUMBER;
BEGIN
FOR var_a IN
(
SELECT GUID, last_update
FROM baeume
WHERE image IS NOT NULL
ORDER BY guid
)
LOOP
BEGIN
--t_blob BLOB;
t_len := 0;
t_file_name := '';
--t_output UTL_FILE.FILE_TYPE;
t_TotalSize := 0;
t_position := 1;
t_chucklen := 32767;
--t_chuck := (4096);
t_remain := 0;
SELECT DBMS_LOB.GETLENGTH (image), TO_CHAR (guid) || '.jpg'
INTO t_TotalSize, t_file_name
FROM baeume
WHERE guid = var_a.guid;
t_remain := t_TotalSize;
t_output := UTL_FILE.FOPEN ('DIR_IMG', t_file_name, 'wb', 32760);
SELECT image
INTO t_blob
FROM baeume
WHERE guid= var_a.guid;
WHILE t_position < t_TotalSize
LOOP
DBMS_LOB.READ (t_blob, t_chucklen, t_position, t_chuck);
UTL_FILE.PUT_RAW (t_output, t_chuck);
UTL_FILE.FFLUSH (t_output);
t_position := t_position + t_chucklen;
t_remain := t_remain - t_chucklen;
IF t_remain < 4096
THEN
t_chucklen := t_remain;
END IF;
END LOOP;
UTL_FILE.FCLOSE (t_output);
END;
END LOOP;
END;
END;
答案 0 :(得分:0)
检查并确保已注册对象图的所有依赖项,以便可以正确解析它们。
您很可能会丢失依赖项注册。
对于TransactionService
,您要在构造函数中传递/注入实现
public TransactionService(MRepository mRepository, FService fService) {
_mRepository = mRepository;
_fservice = fService;
}
仅注册接口时。
container.RegisterType<IFService, FService>(new HierarchicalLifetimeManager());
container.RegisterType<IMService, MService>(new HierarchicalLifetimeManager());
container.RegisterType<ITransactionService, TransactionService>(new HierarchicalLifetimeManager());
container.RegisterType<IMRepository, MRepository>();
解析器将基于已知的依赖项对实现进行实际的初始化和注入。
更改
public TransactionService(MRepository mRepository, FService fService)
依靠注册接口
private readonly IMRepository mRepository;
private readonly IFService fService;
public TransactionService(IMRepository mRepository, IFService fService) {
this.mRepository = mRepository;
this.fservice = fService;
}
需要检查每个与创建/解析控制器有关的类,以确保可以正确解决它们的依赖关系。