Simplifying configuration of WCF4 RESTful service

2019-08-29 20:41发布

问题:

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:

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.



标签: c# wcf rest c#-4.0