Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5930233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:31:42+00:00 2026-05-22T14:31:42+00:00

Let’s say I am using the new WCF Web API to build a RESTful

  • 0

Let’s say I am using the new WCF Web API to build a RESTful service and, in my service, I have a section of the URI that will describe the target resource, but is used on (nearly) all methods of the contract. For example, if I have a User service that deals with eCommerce and may look like:

[ServiceContract]
public class MyUserService
{
    private MyUserRepository _UserRepo;
    private MyOrganizationRepository _OrgRepo;

    [WebGet (UriTemplate = "{OrganizationName}/Users")]
    public IEnumerable<User> GetUsers (string OrganizationName)
    {
        IEnumerable<User> Users = null;
        var Organization = _OrgRepo.GetOrgByName (OrganizationName);

        if (Organization != null)
        {
            Users = Organization.GetUsers ();
        }
        else
        {
            throw new WebFaultException<string> ("Organization not found.", HttpStatusCode.NotFound);
        }

        return Users;
    }

    [WebInvoke (UriTemplate = "{OrganizationName}/Users", /*yada...yada...yada*/)]
    public User AddNewUser (string OrganizationName, User User)
    {
        // Find the organization, like above, and throw if null.
    }
}

If I have to continually load the organization and test for null, this will bog down my code and is not very DRY. (So tempted to spell out DRY…) What I would like to do is load up a property in the MyUserService class that is populated when {OrganizationName} is included in the URI and throw a WebFaultException otherwise. Because this is apart of the URI, what would be the best way to accomplish this?

EDIT:

For those that may be interested, here is an example of the HttpOperationHandler I came up with. There doesn’t seem to be a whole lot of information out there covering this. I also found more information about Processors that will be coming with the WCF Web Api suite and it looks like they will handle this sort of thing better replace HttpOperationHandlers and it seems they may be easier to use. (This is just a for-instance to cover some things I found hard to find. I wrote it up a bit differently in my application.)

using Microsoft.ApplicationServer.Http.Dispatcher;   // For HttpOperationHandler
using Microsoft.ApplicationServer.Http.Description;  // For HttpOperationHandlerFactory

public class OrganizationHandler : HttpOperationHandler<string, Organization>
{
    private Repository<Organization> _OrganizationRepository;

    public OrganizationHandler (UnitOfWork Work)
        : base ("OrganizationName")
    {
        _OrganizationRepository = Work.Organizations;
    }

    public override Organization OnHandle (string OrganizationName)
    {
        var Result = _OrganizationRepository
                        .Get (O => O.UrlSafeName.Equals (OrganizationName,
                                                StringComparison.InvariantCultureIgnoreCase));

        if (Result == null)
        {
            throw new WebFaultException<string> ("Organization not found.");
        }

        return Result;
    }
}

public class OrganizationHandlerFactory : HttpOperationHandlerFactory
{
    private UnitOfWork _Work;

    public OrganizationHandlerFactory (UnitOfWork Work)
    {
        _Work = Work;
    }

    protected override Collection<HttpOperationHandler> OnCreateRequestHandlers
        (ServiceEndpoint endpoint, HttpOperationDescription operation)
    {
        var Collection = base.OnCreateRequestHandlers (endpoint, operation);

        if (operation.InputParameters.Any (IP => IP.Type.Equals (typeof (Organization))))
        {
            var Binding = endpoint.Binding as HttpBinding;

            if (Binding != null)
            {
                Collection.Add (new OrganizationHandler (_Work));
            }
        }

        return Collection;
    }
}

And then to wire it up in Global.asax (I am using Ninject for IoC):

// Add this reference to get the MapServiceRoute<T> extension
using Microsoft.ApplicationServer.Http.Activation;

public class Global : HttpApplication
{
    protected void Application_Start (object sender, EventArgs e)
    {
        var Kernel = BuildKernel ();

        var Config = HttpHostConfiguration.Create ()
            .SetOperationHandlerFactory
                (Kernel.Get (typeof (OrganizationHandlerFactory)) as OrganizationHandlerFactory)
            .SetResourceFactory (new NinjectResourceFactory (Kernel));


        RouteTable.Routes.MapServiceRoute<OrganizationService> ("Organizations", Config);
    }

    protected IKernel BuildKernel ()
    {
        IKernel Kernel = new Ninject.StandardKernel ();

        // Load up the Kernel

        return Kernel;
    }
}

public class NinjectResourceFactory : IResourceFactory
{
    private readonly IKernel _Kernel;

    public NinjectResourceFactory (IKernel Kernel)
    {
        _Kernel = Kernel;
    }

    public object GetInstance (Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
    {
        return Resolve (serviceType);
    }

    public void ReleaseInstance (InstanceContext instanceContext, object service)
    {
        throw new NotImplementedException ();
    }

    private object Resolve (Type type)
    {
        return _Kernel.Get (type);
    }
}

And here it is in my Service:

[ServiceContract]
[ServiceBehavior (InstanceContextMode = InstanceContextMode.PerCall)]
public class OrganizationService
{
    [WebGet (UriTemplate = "{OrganizationName}/Products")]
    public IEnumerable<Product> GetProducts (Organization Organization)
    {
        return Organization.Products;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:31:43+00:00Added an answer on May 22, 2026 at 2:31 pm

    This is exactly what OperationHandlers are for. You create a single OperationHandler that converts the URI parameter into a strongly typed object that you can just accept as a parameter on the operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a simple Login servlet that checks the passed name and
Let's say I have an facebook application running using the JS SDK. First user
Let's say that I have a set of relations that looks like this: relations
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say that I have a binary that I am building, and I include
Let's say that I have classes like this: public class Parent { public int
Let's say I'm creating an OpenGL game in C++ that will have many objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.