问题描述
我怎样才能重新启动托管APPHOST一个ServiceStack自我?设置我的APPHOST实例为null,它的处置工作不正常,它抛出以下异常:
System.ArgumentException:一个具有相同的键条目已经存在。
我需要能够做到这一点重新加载设置并开始APPHOST无需重新启动Windows服务托管所述APPHOST
编辑:
斯科特和武果汁的建议,以在不同的应用程序域运行APPHOST是正确的解决方案。为了让过去的跨域调用重启APPHOST,我创建了运行在主AppDomain中,并呼吁从斯科特的解决方案重启方法的第二APPHOST。两个APPHOST情况下启用CORS允许简单$ Ajax调用重新启动该服务并重新加载页面,一旦该服务已启动,请求返回。
使用一个AppDomain:
武果汁对使用的AppDomain
的建议是正确的。我已经包括了如何用一个简单的例子,一个的AppDomain
隔离ServiceStack,并允许它被启动/重新启动/停止。
使用系统;
使用ServiceStack;
使用System.Runtime.Remoting;
命名空间测试
{
公共类ServiceStackConsoleHost:MarshalByRefObject的
{
公共静态无效的主要()
{
启动( );
}
静态对象句柄句柄;
静态的AppDomain ServiceStackAppDomain;
公共静态无效的开始()
{
//获得主机
VAR的AssemblyName大会= typeof运算(ServiceStackConsoleHost).Assembly.FullName;
//创建一个AppDomain
ServiceStackAppDomain = AppDomain.CreateDomain(ServiceStackAppDomain);
//我们的服务组装
ServiceStackAppDomain.Load(的AssemblyName)负载;
//创建我们ServiceStack应用
手柄= ServiceStackAppDomain.CreateInstance(的AssemblyNameTest.ServiceStackConsoleHost)的实例;
//显示,主要应用是在一个单独的AppDomain
Console.WriteLine(,AppDomain.CurrentDomain.FriendlyName主要应用是在AppDomain中{0}运行);
//等待输入
到Console.ReadLine();
//重新启动应用程序
重新启动();
}
公共静态无效停止()
{
如果(ServiceStackAppDomain == NULL)
的回报;
//通知ServiceStack该应用程序域将被卸载
变种宿主=(ServiceStackConsoleHost)Handle.Unwrap();
host.Shutdown();
//关闭的ServiceStack应用
AppDomain.Unload(ServiceStackAppDomain);
ServiceStackAppDomain = NULL;
}
公共静态无效重启()
{
停止();
Console.WriteLine(重新开始......);
开始();
}
只读APPHOST APPHOST;
公共ServiceStackConsoleHost()
{
APPHOST =新APPHOST();
appHost.Init();
appHost.Start(HTTP:// *:8090 /);
Console.WriteLine(,AppDomain.CurrentDomain.FriendlyNameServiceStack在AppDomain中{0}运行);
}
公共无效关机()
{
如果(APPHOST!= NULL)
{
Console.WriteLine(关闭ServiceStack主机);
如果(appHost.HasStarted)
appHost.Stop();
appHost.Dispose();
}
}
}
公共类APPHOST:AppSelfHostBase
{
公共APPHOST():基地(我的ServiceStack服务 typeof运算(APPHOST).Assembly)
{
}
公共覆盖无效配置(Funq.Container容器)
{
}
}
}
说明的找到。
How can I restart a ServiceStack self hosted Apphost? Setting my AppHost instance to null and disposing of it does not work correctly, it throws the following Exception:
System.ArgumentException: An entry with the same key already exists.
I need to be able to do this to reload settings and start the AppHost without restarting the Windows Service hosting the AppHost
EDIT:Scott and Moo-Juice's suggestions to run the AppHost in a different AppDomain is the correct solution. In order to get past the Cross Domain calls to restart the AppHost, I created a second AppHost which runs in the Main AppDomain and calls the Restart method from Scott's solution. Enabling CORS on both AppHost instances allows for a simple $ajax call to restart the service and reload the page once the service is started and the request returns.
Use an AppDomain:
Moo-Juice's suggestion to use an AppDomain
is correct. I have included a simple example of how to use an AppDomain
to isolate ServiceStack, and allow it to be Started/Restarted/Stopped.
using System;
using ServiceStack;
using System.Runtime.Remoting;
namespace Test
{
public class ServiceStackConsoleHost : MarshalByRefObject
{
public static void Main()
{
Start();
}
static ObjectHandle Handle;
static AppDomain ServiceStackAppDomain;
public static void Start()
{
// Get the assembly of our host
var assemblyName = typeof(ServiceStackConsoleHost).Assembly.FullName;
// Create an AppDomain
ServiceStackAppDomain = AppDomain.CreateDomain("ServiceStackAppDomain");
// Load in our service assembly
ServiceStackAppDomain.Load(assemblyName);
// Create instance of our ServiceStack application
Handle = ServiceStackAppDomain.CreateInstance(assemblyName, "Test.ServiceStackConsoleHost");
// Show that the main application is in a separate AppDomain
Console.WriteLine("Main Application is running in AppDomain '{0}'", AppDomain.CurrentDomain.FriendlyName);
// Wait for input
Console.ReadLine();
// Restart the application
Restart();
}
public static void Stop()
{
if(ServiceStackAppDomain == null)
return;
// Notify ServiceStack that the AppDomain is going to be unloaded
var host = (ServiceStackConsoleHost)Handle.Unwrap();
host.Shutdown();
// Shutdown the ServiceStack application
AppDomain.Unload(ServiceStackAppDomain);
ServiceStackAppDomain = null;
}
public static void Restart()
{
Stop();
Console.WriteLine("Restarting ...");
Start();
}
readonly AppHost appHost;
public ServiceStackConsoleHost()
{
appHost = new AppHost();
appHost.Init();
appHost.Start("http://*:8090/");
Console.WriteLine("ServiceStack is running in AppDomain '{0}'", AppDomain.CurrentDomain.FriendlyName);
}
public void Shutdown()
{
if(appHost != null)
{
Console.WriteLine("Shutting down ServiceStack host");
if(appHost.HasStarted)
appHost.Stop();
appHost.Dispose();
}
}
}
public class AppHost : AppSelfHostBase
{
public AppHost(): base("My ServiceStack Service", typeof(AppHost).Assembly)
{
}
public override void Configure(Funq.Container container)
{
}
}
}
Instructions for how to use an AppDomain
can be found here.
这篇关于ServiceStack Selfhosted重新启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!