Polymorphic HttpOperationHandler

Topics: Web Api
Dec 4, 2011 at 6:35 AM
Edited Dec 4, 2011 at 6:35 AM

Hi there,

We have a base document class that all other documents are derived off of:

 
public abstract class BaseDocument
{
   protected abstract void DoValidation();

   public void Validate()
   {
      // Generic validation
      DoValidation();
   }
}


public class DerivedDocument : BaseDocument
{
   // Properties...

   protected override void DoValidation
   {
      // If some property != xyz throw...   
   }
}

We POST/PUT these documents to our webservices.
What I really would like to achieve is to register a single HttpOperationHandler that takes an input of BaseDocument and has an Output of BaseDocument and simply calls the documents validate method.

public class BaseDocumentOperationHandler : HttpOperationHandler<BaseDocument, BaseDocument>
    {
        public BaseDocumentOperationHandler(string outputParameterName) : base(outputParameterName)
        {
        }

        protected override BaseDocument OnHandle(BaseDocument input)
        {
            input.Validate();

            return input;
        }
    }

After registering this handler on appropriate operations the intention would be that it would be invoked whenever an operation takes a derived class as the parameter:

[WebInvoke(UriTemplate = "", Method = "POST")]
public void Post(DerivedDocument applicationDocument)
{
   throw new NotImplementedException();
}

With the code above however I get the following error:
WebHost failed to process a request. Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/40185776 Exception: System.ServiceModel.ServiceActivationException: The service '/applications' cannot be activated due to an exception during compilation.  The exception message is: The request HttpOperationHandler 'BaseDocumentOperationHandler[input, out applicationDocument]' of service operation 'Post' will never receive a value for input parameter 'input' of type 'BaseDocument'.

Any ideas that might work?

Cheers