Operation Handler WCF Web Api malfunctionging?

532 Views Asked by At

I'm building a web api using WCF web api preview 6, currently I'm stuck with a little problem. I would like to have an operation handler to inject an IPrincipal to the operation in order to determine which user is making the request. I already have that Operation Handler and is already configured. But I noticed that when I decorate the operation with the WebInvoke attribute and simultaneously the operation receives an IPrincipal and other domain object, the system throws an exception telling me:

The HttpOperationHandlerFactory is unable to determine the input parameter that should be associated with the request message content for service operation 'NameOfTheOperation'. If the operation does not expect content in the request message use the HTTP GET method with the operation. Otherwise, ensure that one input parameter either has it's IsContentParameter property set to 'True' or is a type that is assignable to one of the following: HttpContent, ObjectContent1, HttpRequestMessage or HttpRequestMessage1.

I do not know what is happening here. To give you some background I'll post some of my code to let you know how am I doing things.

The operation:

[WebInvoke(UriTemplate = "", Method = "POST")]
    [Authorization(Roles = "")]
    public HttpResponseMessage<dto.Diagnostic> RegisterDiagnostic(dto.Diagnostic diagnostic, IPrincipal principal)
    {
       ......
    }

WCF web api knows when to inject the IPrincipal because I decorate the operation with a custom Authorization attribute.

The configuration in the Global file:

var config = new WebApiConfiguration() {EnableTestClient = true};
        config.RegisterOAuthHanlder(); //this is an extension method
        routes.SetDefaultHttpConfiguration(config);

        routes.MapServiceRoute<MeasurementResource>("Measurement");
        routes.MapServiceRoute<DiagnosticResource>("Diagnostic");

Then the RegisterOAuthHandler method adds an operation handler to the operation if it's been decorated with the custom authorization attibute. this is how it looks:

public static WebApiConfiguration RegisterOAuthHanlder(this WebApiConfiguration conf)
    {
        conf.AddRequestHandlers((coll, ep, desc) =>
        {
            var authorizeAttribute = desc.Attributes.OfType<AuthorizationAttribute>().FirstOrDefault();
            if (authorizeAttribute != null)
            {
                coll.Add(new OAuthOperationHandler(authorizeAttribute));
            }
        });
        return conf;
    }

    public static WebApiConfiguration AddRequestHandlers(
       this WebApiConfiguration conf,
       Action<Collection<HttpOperationHandler>, ServiceEndpoint, HttpOperationDescription> requestHandlerDelegate)
    {
        var old = conf.RequestHandlers;
        conf.RequestHandlers = old == null ? requestHandlerDelegate : (coll, ep, desc) =>
        {
            old(coll, ep, desc);
        };
        return conf;
    }

Can somebody help me with this? Thank you in advanced!

1

There are 1 best solutions below

1
On BEST ANSWER

Try wrapping your Diagnostic param in ObjectContent i.e. ObjectContent<Diagnostic>. Then you will use the ReadAs() method to pull out the object.

It should work.