簡體   English   中英

簡化WCF4 RESTful服務路由的配置

[英]Simplifying configuration of WCF4 RESTful service routes

“ WCF REST模板40(CS)”項目模板中的默認Global.asax.cs文件以及我在網上看到的每個教程都包含以下方法的一種變體:

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)));
}

WebApplication本身應該能夠發現哪些服務可用並根據約定或元數據應用路由時,以這種方式管理服務路由似乎很麻煩。

問題

  1. 除了默認值以外,還有沒有一種內置的方法來定義服務路由(可以在web.config中配置,也可以編譯到服務本身)?
  2. 使用此模板的其他人是否始終遵循提供的模型,還是其他人提出了更好的方法?

擬議的解決方案

遷移了我對答案的建議解決方案

我想我必須假設沉默就是接受。 這是我的解決方案(最初來自我的問題):

假設沒有更好的內置或其他可用的方法(因為我什么也沒找到),我嘗試執行此操作涉及定義一個屬性:

[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;
    }
}

用於裝飾應公開的每個服務協定,並將默認的RegisterRoutes更改為:

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));
        }
    }
}

這似乎可行,並且不太麻煩,因為它只發生在Application_Start ,但是我是第一次使用WCF4構建RESTful Web服務,所以我不知道它可能引起什么問題。

如果有人想出一種更優雅的解決方法,我很樂意考慮其他選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM