Servicestack CorsFeature Global Options Handler Not Firing on Certain Routes;

后端 未结 3 764
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 23:51

I\'ve got a service setup using the CorsFeature, and am using the approach that mythz suggested in other answers, collected in a function used in the appHost file:

3条回答
  •  一个人的身影
    2020-12-18 00:28

    Following steps worked for me within ServiceStackV3.

    1. Added a new class CustomActionHandler

    using ServiceStack.ServiceHost;
    using ServiceStack.WebHost.Endpoints.Extensions;
    using ServiceStack.WebHost.Endpoints.Support;
    
    public class CustomActionHandler : IServiceStackHttpHandler, IHttpHandler 
    {
        public Action Action { get; set; }
    
        public CustomActionHandler(Action action)
        {
            if (action == null)
                throw new Exception("Action was not supplied to ActionHandler");
    
            Action = action;
        }
    
        public void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {            
            Action(httpReq, httpRes);
        }
    
        public void ProcessRequest(HttpContext context)
        {
            ProcessRequest(context.Request.ToRequest(GetType().Name), 
                context.Response.ToResponse(),
                GetType().Name);
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    }
    

    2. Add the CustomHandler within AppHostBase.Config.RawHttpHandlers collection (this statements can be written inside the Configure(Container container) method).

    // Handles Request and closes Response after emitting global HTTP Headers 
    var emitGlobalHeadersHandler = new CustomActionHandler((httpReq, httpRes) => httpRes.EndRequest());
    Config.RawHttpHandlers.Add(httpReq => httpReq.HttpMethod == HttpMethods.Options ? emitGlobalHeadersHandler : null);
    

提交回复
热议问题