Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure ServiceStack Base URI

Tags:

servicestack

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?

like image 627
Fred Avatar asked Jan 16 '23 03:01

Fred


1 Answers

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;
        }
    }
}
like image 173
Dimentox Avatar answered Jan 21 '23 09:01

Dimentox