相关疑难解决方法(0)

使用Castle Windsor在WebAPI中进行依赖注入

我想使用Castle Windsor在WebApi应用程序中实现依赖注入.我有以下示例代码 -

界面 -

public interface IWatch
{
    {
        DateTime GetTime();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下Watch类实现了IWatch接口 -

public class Watch:IWatch
{
        public DateTime GetTime()
        {
            return DateTime.Now;
        }
}
Run Code Online (Sandbox Code Playgroud)

WebApi控制器 - WatchController如下 -

public class WatchController : ApiController
{
        private readonly IWatch _watch;

        public WatchController()
        {
            _watch = new Watch();
        }

        //http://localhost:48036/api/Watch
        public string Get()
        {
            var message = string.Format("The current time on the server is: {0}", _watch.GetTime());
            return message;
        }
}
Run Code Online (Sandbox Code Playgroud)

目前我在WatchController构造函数中使用Watch启动IWatch对象.我想删除使用Windsor Castle依赖注入原理在构造函数中初始化IWatch的依赖性.

在这种WebApi的情况下,任何人都可以为我提供实现依赖注入的步骤吗?提前致谢!

c# castle-windsor wcf-web-api asp.net-web-api

21
推荐指数
2
解决办法
2万
查看次数

标签 统计

asp.net-web-api ×1

c# ×1

castle-windsor ×1

wcf-web-api ×1