Configure WebapiHttpOperationHandlerFactory

Topics: Web Api
Feb 13, 2012 at 7:33 AM

I want to configure multiple operationHandler. For that i have created my CustomeOperationHandlerFActory Class. i.e

 

public class MyOperationHandlerFactory : WebApiHttpOperationHandlerFactory
{

protected override Collection<HttpOperationHandler> OnCreateRequestHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation)
 {
 switch (operation.Name)
            {
                case "QueryCustomerNumbers":
                    coll.Add(new QueryCustomerNumbersAsParameterHandler());
                   break;
                case "DeleteIdentity":
                    coll.Add(new DeleteIdentityAsParameterHandler());
                  break;
         }
 }
}

public class QueryCustomerNumbersAsParameterHandler : HttpOperationHandler<string, string, string, string, GetCustomerNumberRequest>
    {
        public QueryCustomerNumbersAsParameterHandler()
            : base("request")
        { }

        protected override GetCustomerNumberRequest OnHandle(string claimValue, string claim, string provider, string namedIdentity)
        {
            if (!string.IsNullOrEmpty(provider)) if (!Enum.IsDefined(typeof(IdentityProviders), provider))
                    provider = null;
            if (!string.IsNullOrEmpty(claim)) if (!Enum.IsDefined(typeof(Claim), claim))
                    claim = null;

            return new GetCustomerNumberRequest { NamedIdentity = namedIdentity, Value = claimValue, Claim = string.IsNullOrEmpty(claim) ? Claim.NotSet : (Claim)Enum.Parse(typeof(Claim), claim), IdentityProvider = string.IsNullOrEmpty(provider) ? IdentityProviders.NotSet : (IdentityProviders)Enum.Parse(typeof(IdentityProviders), provider) };
        }
    }
 public class DeleteIdentityAsParameterHandler : HttpOperationHandler<string, string, DeleteIdentityReq>
    {
        public DeleteIdentityAsParameterHandler()
            : base("request")
        {   }

        protected override DeleteIdentityReq OnHandle(string provider, string namedIdentity)
        {
            if (!string.IsNullOrEmpty(provider)) if (!Enum.IsDefined(typeof(IdentityProviders), provider))
                    provider = null;

            return new DeleteIdentityReq { NamedIdentity = namedIdentity, IdentityProvider = string.IsNullOrEmpty(provider) ? IdentityProviders.NotSet : (IdentityProviders)Enum.Parse(typeof(IdentityProviders), provider) };
        }
    }
    

 

Now i want to configure MyOperationHandlerFactory in Gloabal asax in application Start event.

 
 var config = new WebApiConfiguration()
            {
                EnableTestClient = true,
              
            ????
       }

Feb 13, 2012 at 8:12 AM

There are samples of this all over the configuration examples. Just download the examples under downloads, and look at all the examples.

Feb 13, 2012 at 8:12 AM

There are samples of this all over the configuration examples. Just download the examples under downloads, and look at all the examples.

Coordinator
Feb 13, 2012 at 9:16 PM

The easiest way to do this today is not to use a custom factory. Instead create a custom configuration object and set the RequestHandlers/ResponseHandlers properties in the ctor if you want to reuse the config, or just new up WebApiConfiguration and set RequestHandlers/ResponseHandlers directly in your global.asax.