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

  • SEARCH
  • Home
  • 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 7435967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:09:50+00:00 2026-05-29T10:09:50+00:00

The default Global.asax.cs file from the WCF REST Template 40(CS) project template and every

  • 0

The default Global.asax.cs file from the “WCF REST Template 40(CS)” project template and every tutorial I’ve seen online include a variation of the following method:

private void RegisterRoutes()
{
    // Edit the base address of Service1 by replacing the "Service1" string below
    RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
}

Managing the service routing in this way seems needlessly cumbersome when the WebApplication itself should be able to discover which services should be available and apply routes based on convention or metadata.

QUESTIONS

  1. Is there a built-in way beyond the default to define the service routes (either configurable in the web.config, or compiled onto the service itself)?
  2. Do others that use this template always follow the model provided or has someone else come up with a better approach?

Proposed Solution

Migrated my proposed solution to an answer

  • 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-29T10:09:51+00:00Added an answer on May 29, 2026 at 10:09 am

    I guess I have to assume that silence is acceptance. Here is my solution (originally from my question):

    Assuming there is nothing better built in or otherwise available (because I didn’t find anything), my attempt at doing this involves defining an attribute:

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public class ServiceRouteAttribute : Attribute
    {
        public string RoutePrefix { get; set; }
        public Type ServiceFactoryType { get; set; }
    
        public ServiceHostFactoryBase ServiceFactory
        {
            get
            {
                if (ServiceFactoryType == null || !ServiceFactoryType.IsRelated(typeof(ServiceHostFactoryBase)))
                    return null;
    
                return Activator.CreateInstance(ServiceFactoryType) as ServiceHostFactoryBase;
            }
        }
    
        public ServiceRouteAttribute() : this(string.empty) { }
        public ServiceRouteAttribute(string routePrefix) : this(routePrefix, typeof(WebServiceHostFactory)) { }
        public ServiceRouteAttribute(string routePrefix, Type serviceFactoryType)
        {
            RoutePrefix = routePrefix;
            ServiceFactoryType = serviceFactoryType;
        }
    }
    

    which is used to decorate each service contract that should be exposed, and changing the default RegisterRoutes to:

    private void RegisterRoutes()
    {
        // `TypeHelper.GetTypes().FilterTypes<T>` will find all of the types in the
        // current AppDomain that:
        // - Implement T if T is an interface
        // - Are decorated with T if T is an attribute
        // - Are children of T if T is anything else
        foreach (var type in TypeHelper.GetTypes()
                                       .FilterTypes<ServiceRouteAttribute>())
        {
            // routeAttrs should never be null or empty because only types decorated
            // with `ServiceRouteAttribute` should ever get here.
            // `GetAttribute<T>` is my extension method for `MemberInfo` which returns all
            // decorations of `type` that are T or children of T
            var routeAttrs = type.GetAttributes<ServiceRouteAttribute>();
    
            foreach (var routeAttr in routeAttrs)
            {
                // Some dupe and error checking
    
                var routePrefix = routeAttr.RoutePrefix;
                if (string.IsNullOrEmpty(routePrefix))
                    routePrefix = type.Name;
    
                RouteTable.Routes.Add(new ServiceRoute(routePrefix, 
                                                       routeAttr.ServiceFactory,
                                                       type));
            }
        }
    }
    

    This seems to work and isn’t too intrusive because it only happens at Application_Start, but I’m new to building RESTful web services with WCF4 so I don’t know what sort of problems it could cause.

    If anyone comes up with a more elegant way of solving this, I’d gladly consider any alternative.

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

Sidebar

Related Questions

I have set up ninject on my project in the global.asax file... protected void
I'm currently overriding the default ModelMetadataProvider in the Global.asax file using this ModelMetadataProviders.Current =
here are two routes from my global.asax file. I'm trying to go to the
Here's the About.cshtml from the default MVC 3 template: @{ ViewBag.Title = About Us;
My English not wel, please bear with me a lot. routes in Global.asax file:
I implemented a following code in Global.asax file of my web application. void Application_BeginRequest()
Do i have to list all controller functions in Global.asax.cs file? Im creating a
I can't capture what is happening here. My global.asax file has nothing exotic in
In my Global.asax file I have the following; public static void RegisterRoutes(RouteCollection routes) {
What is the best way to implement a global default context menu for a

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.