标签: hyprlinkr

ASP Web Api - IoC - 解析HttpRequestMessage

我正在尝试使用ASP.NET WebAPI设置Castle Windsor.

我也使用Hyprlinkr包(https://github.com/ploeh/Hyprlinkr),因此需要将HttpRequestMessage的实例注入到我的控制器的一个依赖项中.

我正在关注Mark Seemann的这篇文章 - http://blog.ploeh.dk/2012/04/19/WiringHttpControllerContextWithCastleWindsor.aspx,但我发现虽然API运行,但当我调用它时,请求只是挂起.没有错误消息.就好像它处于无限循环中一样.它停留在我的Custom ControllerActivator调用Resolve上

我想我的一些城堡注册错了.如果我删除上面文章中提到的那些,那么我可以成功调用API(尽管没有我需要解决的依赖项)

有任何想法吗?

代码在下面

//Global.asax
public class WebApiApplication : HttpApplication
{
    private readonly IWindsorContainer container;

    public WebApiApplication()
    {
        container = 
            new WindsorContainer(
                new DefaultKernel(
                    new InlineDependenciesPropagatingDependencyResolver(), 
                    new DefaultProxyFactory()), 
                new DefaultComponentInstaller());

        container.Install(new DependencyInstaller());
    }

    protected void Application_Start()
    {        
        GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorCompositionRoot(this.container));
    }

// installer
public class DependencyInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<TypedFactoryFacility>();

        container.Register(
            Component.For<ValuesController>()
                .Named("ValuesController")
                .LifeStyle.PerWebRequest,

            Component.For<IResourceLinker>()
                .ImplementedBy<RouteLinker>()
                .LifeStyle.PerWebRequest,

            Component.For<IResourceModelBuilder>()
                .ImplementedBy<ResourceModelBuilder>()
                .LifeStyle.PerWebRequest,

                Component.For<HttpRequestMessage>()
                .Named("HttpRequestMessage") …
Run Code Online (Sandbox Code Playgroud)

c# castle-windsor inversion-of-control asp.net-web-api hyprlinkr

10
推荐指数
1
解决办法
7406
查看次数

当方法有返回值时,如何在F#中提供Expression <Action <T >>?

我正在尝试将一些C#代码转换为F#.具体来说,我正在尝试使用Hyprlinkr将一些代码转换为F#.

C#代码如下所示:

Href = this.linker.GetUri<ImagesController>(c =>
    c.Get("{file-name}")).ToString()
Run Code Online (Sandbox Code Playgroud)

其中GetUri方法被定义为

public Uri GetUri<T>(Expression<Action<T>> method);
Run Code Online (Sandbox Code Playgroud)

并被ImagesController.Get定义为

public HttpResponseMessage Get(string id)
Run Code Online (Sandbox Code Playgroud)

在F#中,我试图这样做:

Href = linker.GetUri<ImagesController>(
    fun c -> c.Get("{file-name}") |> ignore).ToString())
Run Code Online (Sandbox Code Playgroud)

这个编译,但在运行时抛出此异常:

System.ArgumentException未由用户代码处理
HResult = -2147024809
Message =类型'System.Void'的表达式不能用于返回类型'Microsoft.FSharp.Core.Unit'Source
= System.Core

据我所知,F#表达式是一个返回的表达式unit,但它应该是一个Expression<Action<T>>"返回" void.

我正在使用F#3.0(我想 - 我正在使用Visual Studio 2012).

我该如何解决这个问题?

f# expression hyprlinkr

7
推荐指数
1
解决办法
1017
查看次数

如何设置Ninject DI以创建Hyprlinkr RouteLinker实例

我有一个MVC4 Web API项目,我利用Mark Seemann的Hyprlinkr组件生成Uris链接资源.(客户 - >地址例如).

我已经遵循了Mark的Web依赖注入指南(适用于Ninject),我无法理解将IResourceLinker注入控制器应该做些什么.

按照Mark的指南,我的IHttpControllerActivator.Create create方法如下所示:

IHttpController IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
    var controller = (IHttpController) _kernel.GetService(controllerType);

    request.RegisterForDispose(new Release(() => _kernel.Release(controller)));

    return controller;
}
Run Code Online (Sandbox Code Playgroud)

正是在这种方法中,Hyprlinkr自述文件建议创建RouteLinker.不幸的是我不知道如何在Ninject注册这个.

我不能像下面那样绑定,因为这会导致多个绑定:

_kernel.Bind<IResourceLinker>()
    .ToMethod(context => new RouteLinker(request))
    .InRequestScope();
Run Code Online (Sandbox Code Playgroud)

我有这样的重新绑定:

_kernel.Rebind<IResourceLinker>()
    .ToMethod(context => new RouteLinker(request))
    .InRequestScope();
Run Code Online (Sandbox Code Playgroud)

但我担心更改ninject绑定图对每个请求都可能是一件坏事.

实现这一目标的最佳方法是什么?


根据Paige Cook的要求更新

我在这里使用重新绑定:

IHttpController IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
    _kernel.Rebind<IResourceLinker>()
        .ToMethod(context => new RouteLinker(request))
        .InRequestScope();

    var controller …
Run Code Online (Sandbox Code Playgroud)

dependency-injection ninject asp.net-web-api hyprlinkr

5
推荐指数
1
解决办法
701
查看次数