开发者

StructureMap: How to supply System.Web.HttpRequestBase to a method?

开发者 https://www.devze.com 2023-02-14 14:02 出处:网络
I\'m trying to implement a wrapper for Cookies, as outlined here: http://chwe.at/blog/post/2009/01/28/Testable-and-reusable-cookie-access-with-ASPNet-MVC-RC.aspx

I'm trying to implement a wrapper for Cookies, as outlined here:

http://chwe.at/blog/post/2009/01/28/Testable-and-reusable-cookie-access-with-ASPNet-MVC-RC.aspx

But StructureMap is failing with the error:

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily System.开发者_运维问答Web.HttpRequestBase,
System.Web.Abstractions, Version=3.5.0.0, Culture=neutral,     
PublicKeyToken=31bf3856ad364e35

I think this is because I'm trying to get StructureMap to supply parameters for the following method:

    public CookieContainer(HttpRequestBase request, HttpResponseBase response)
    {
        Check.IsNotNull(request, "request");
        Check.IsNotNull(response, "response");

        _request = request;
        _response = response;
    }

I've tried adding the following to the StructureMap registry:

        ForRequestedType<HttpRequestBase>()
            .TheDefaultIsConcreteType<HttpRequestBase>();

        ForRequestedType<HttpResponseBase>()
            .TheDefaultIsConcreteType<HttpResponseBase>();

but this fails with the error:

StructureMap Exception Code:  180
StructureMap cannot construct objects of Class System.Web.HttpRequestBase, 
System.Web.Abstractions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35 because there is no public constructor found. 

How can I get StructureMap to supply the HttpRequestBase and HttpResponseBase objects to the method?


This is because HttpRequestBase is abstract and SM can do nothing about it.

Use the factory Methode

ForRequestedType<HttpRequestBase>().TheDefault.Is
  .ConstructedBy(() => HttpContext.Request);


It turns out I don't need the overloaded constructor.. so all i had to do was delete this:

public CookieContainer(HttpRequestBase request, HttpResponseBase response)
{
    Check.IsNotNull(request, "request");
    Check.IsNotNull(response, "response");

    _request = request;
    _response = response;
}

This constructor is only used to create a mock cookie container, into which you pass mock HttpRequestBase and HttpResponseBase objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消