Configure ServiceStack Base URI

2019-05-05 18:04发布

问题:

I'm creating a self-hosted REST service using service stack & AppHostHttpListenerBase. I'd like to use a base URI for my services (e.g. "api") like so:

http://myserver/api/service1/param
http://myserver/api/service2/param

How do I do this without defining "api" in each of my routes. In IIS, I can set a virtual directory to isolate the services, but how do I do this when self-hosting?

回答1:

ServiceStack's HttpListener hosts expects to be hosted a the root / path as the normal use-case is to have each self-hosted service available on different custom ports.

Since it doesn't currently support hosting at a /custompath, you would have to specify /api/ prefix on all your service routes.

Add an issue if you want to see support for hosting at custom paths.



回答2:

Here ya go.. (as a bonus this is how you put your service into a plugin.

using BlogEngineService;
using ServiceStack.WebHost.Endpoints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogEngineWinService
{
    public class AppHost : AppHostHttpListenerBase
    {
        public AppHost() : base("Self Host Service", typeof(AppHost).Assembly) { }
        public override void Configure(Funq.Container container)
        {
            Plugins.Add(new BlogEngine());
        }
    }
}

This is how you autowire it up

The call appHost.Routes.AddFromAssembly2(typeof(HelloService).Assembly); Is what calls the extension to auto wire.

using ServiceStack.WebHost.Endpoints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.ServiceInterface;

namespace BlogEngineService
{
    public class BlogEngine : IPlugin, IPreInitPlugin
    {
        public void Register(IAppHost appHost)
        {

            appHost.RegisterService<HelloService>();
            appHost.Routes.AddFromAssembly2(typeof(HelloService).Assembly);
        }

        public void Configure(IAppHost appHost)
        {

        }
    }
}

This is how you mark the Service Class to give it a prefix. Simply mark the class with this attribute

using ServiceStack.DataAnnotations;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogEngineService
{
    public class Hello
    {
        [PrimaryKey]
        public string Bob { get; set; }
    }

    public class HelloResponse
    {
        public string Result { get; set; }
    }

    [PrefixedRoute("/test")]
    public class HelloService : Service
    {

        public object Any(Hello request)
        {
            return new HelloResponse { Result = "Hello, " + request.Bob};
        }
    }
}

Create a CS file in your project for the extension..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ServiceStack.Common;
using ServiceStack.Common.Utils;
using ServiceStack.Common.Web;
using ServiceStack.Text;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.ServiceInterface;

namespace ServiceStack.ServiceInterface
{
    public static class ServiceRoutesExtensions
    {
        /// <summary>
        ///     Scans the supplied Assemblies to infer REST paths and HTTP verbs.
        /// </summary>
        ///<param name="routes">The <see cref="IServiceRoutes"/> instance.</param>
        ///<param name="assembliesWithServices">
        ///     The assemblies with REST services.
        /// </param>
        /// <returns>The same <see cref="IServiceRoutes"/> instance;
        ///     never <see langword="null"/>.</returns>
        public static IServiceRoutes AddFromAssembly2(this IServiceRoutes routes,
                                                     params Assembly[] assembliesWithServices)
        {
            foreach (Assembly assembly in assembliesWithServices)
            {

                AddNewApiRoutes(routes, assembly);
            }

            return routes;
        }

        private static void AddNewApiRoutes(IServiceRoutes routes, Assembly assembly)
        {
            var services = assembly.GetExportedTypes()
                .Where(t => !t.IsAbstract
                            && t.HasInterface(typeof(IService)));

            foreach (Type service in services)
            {
                var allServiceActions = service.GetActions();
                foreach (var requestDtoActions in allServiceActions.GroupBy(x => x.GetParameters()[0].ParameterType))
                {
                    var requestType = requestDtoActions.Key;
                    var hasWildcard = requestDtoActions.Any(x => x.Name.EqualsIgnoreCase(ActionContext.AnyAction));
                    string allowedVerbs = null; //null == All Routes
                    if (!hasWildcard)
                    {
                        var allowedMethods = new List<string>();
                        foreach (var action in requestDtoActions)
                        {
                            allowedMethods.Add(action.Name.ToUpper());
                        }

                        if (allowedMethods.Count == 0) continue;
                        allowedVerbs = string.Join(" ", allowedMethods.ToArray());
                    }
                    if (service.HasAttribute<PrefixedRouteAttribute>())
                    {
                        string prefix = "";
                        PrefixedRouteAttribute a = (PrefixedRouteAttribute)Attribute.GetCustomAttribute(service, typeof(PrefixedRouteAttribute));
                        if (a.HasPrefix())
                        {
                            prefix = a.GetPrefix();
                        }
                        routes.AddRoute(requestType, allowedVerbs, prefix);
                    }
                    else
                    {
                        routes.AddRoute(requestType, allowedVerbs);
                    }
                }
            }
        }

        private static void AddRoute(this IServiceRoutes routes, Type requestType, string allowedVerbs, string prefix = "")
        {
            var newRoutes = new ServiceStack.ServiceHost.ServiceRoutes();
            foreach (var strategy in EndpointHost.Config.RouteNamingConventions)
            {
                strategy(newRoutes, requestType, allowedVerbs);
            }
            foreach (var item in newRoutes.RestPaths)
            {

                string path = item.Path;
                if (!string.IsNullOrWhiteSpace(prefix))
                {
                    path = prefix + path;
                }
                routes.Add(requestType, restPath: path, verbs: allowedVerbs);
            }
        }
    }

    public class PrefixedRouteAttribute : Attribute
    {
        private string _prefix { get; set; }
        private bool _hasPrefix { get; set; }

        public PrefixedRouteAttribute(string path) 
        {
            if (!string.IsNullOrWhiteSpace(path))
            {
                this._hasPrefix = true;
                this._prefix = path;
                //this.Path = string.Format("/{0}{1}", Prefix, Path);
            }
        }
        public bool HasPrefix()
        {
            return this._hasPrefix;
        }
        public string GetPrefix()
        {
            return this._prefix;
        }
    }
}


回答3:

There is actually an easier solution. In your web.config, update your http-handler to:

<httpHandlers>
  <add path="api*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>

With the above, all of your service apis must be prefixed with a "/api/". If you have already used "/api/" in any of your routes, you must now remove them or have to specify it twice in your calls.

Reference: https://github.com/ServiceStack/SocialBootstrapApi



回答4:

I've found a workaround for this. I've only tested this under self hosting.

Create a 'PrefixedRouteAttribute' class that inherits from RouteAttribute

public class PrefixedRouteAttribute : RouteAttribute
{
  public static string Prefix { get; set; }

  public PrefixedRouteAttribute(string path) :
    base(path)
  {
    SetPrefix();
  }

  public PrefixedRouteAttribute(string path, string verbs)
    : base(path, verbs)
  {
    SetPrefix();
  }

  private void SetPrefix()
  {
    if (!string.IsNullOrWhiteSpace(Prefix))
    {
      this.Path = string.Format("/{0}{1}", Prefix, Path);
    }
  }   
}

When you create your AppHost you can set your Prefix

PrefixedRouteAttribute.Prefix = "api";

Then instead of using the [Route] attribute, use the [PrefixRoute] attribute on your classes

[PrefixedRoute("/echo")]
[PrefixedRoute("/echo/{Value*}")]
public class Echo
{
  [DataMember]
  public string Value { get; set; }
}

This will then work for requests to

/api/echo
/api/echo/1

This could possibly be improved. I don't really like the how I need to set the Prefix via the static property but I couldn't think of a better approach under my setup. The principle of creating the overriding attribute seems sound though, and that is the important part.