我需要在我的 servicestack 自托管服务器中有一些“全局”变量,例如此处的 myList:
public partial class Main : Form
{
AppHost appHost;
public Main()
{
InitializeComponent();
appHost = new AppHost();
appHost.Init();
appHost.Start(ListeningOn);
appHost.Plugins.Add(new ProtoBufFormat());
appHost.ContentTypeFilters.Register(ServiceStack.Common.Web.ContentType.ProtoBuf, (reqCtx, res, stream) => ProtoBuf.Serializer.NonGeneric.Serialize(stream, res), ProtoBuf.Serializer.NonGeneric.Deserialize);
}
/// <summary>
/// Create your ServiceStack http listener application with a singleton AppHost.
/// </summary>
public class AppHost : AppHostHttpListenerBase
{
public int intAppHost;
/// <summary>
/// Initializes a new instance of your ServiceStack application, with the specified name and assembly containing the services.
/// </summary>
public AppHost() : base("CTServer HttpListener", typeof(MainService).Assembly) { }
/// <summary>
/// Configure the container with th e necessary routes for your ServiceStack application.
/// </summary>
/// <param name="container">The built-in IoC used with ServiceStack.</param>
public override void Configure(Funq.Container container)
{
Routes
.Add<ReqPing>("/ping");
}
}
}
public class MainService : Service
{
public RespPing Any(ReqPing request)
{
// Add a value to a global list here
myList.Add(myData);
RespPing response = new RespPing();
return response;
}
}
我应该在哪里定义 myList,以及如何从该位置访问它?我怎样才能以线程安全的方式做到这一点?在这种情况下,功能是存储接收到的某个值并在另一个实例中检查该值是否已经在列表中。这是在实例之间共享数据的适当方式,还是我应该遵循另一条路径?
谢谢!马蒂亚