How can I set WebHostUrl in ServiceStack?

2019-07-08 09:33发布

问题:

I'm using ServiceStack 4.0.17 and the Host.Instance.Config.WebHostUrl is always null.

I'm using ServiceStack as a standalone web application - no MVC or ASP.NET - that is being served by IIS Express (VS2013) in integrated mode.

Is there any required configuration to run ServiceStack as a standalone web application that I may be missing?

If someone is having the same issue I created a temporary workaround that can be used in the Application_BeginRequest:

 private void SetApplicationUrl()
        {
            if (_applicationUrl == null)
            {
                lock (_applicationUrlLock)
                {
                    if (ServiceStackHost.Instance.Config.WebHostUrl != null) return;

                    // Remove the page path information.
                    Regex regex = new Regex("(" + HttpContext.Current.Request.Url.AbsolutePath + ")$");

                    _applicationUrl = regex.Replace(HttpContext.Current.Request.Url.AbsoluteUri, string.Empty);

                    ServiceStackHost.Instance.Config.WebHostUrl = _applicationUrl;
                }
            }
        }

回答1:

All ServiceStack configuration should be set in the AppHost.Configure() with the SetConfig method, e.g:

SetConfig(new HostConfig {
    WebHostUrl = myBaseUrl,
});