C# 自托管ServiceStack服务器中的全局变量
我的servicestack自托管服务器中需要有一些“全局”变量,如此处的myList:C# 自托管ServiceStack服务器中的全局变量,c#,
servicestack,C#,
servicestack,我的servicestack自托管服务器中需要有一些“全局”变量,如此处的myList: public partial class Main : Form { AppHost appHost; public Main() { InitializeComponent(); appHost = new AppHost(); appHost.Init();
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;
}
}
public分部类Main:Form
{
AppHost-AppHost;
公用干管()
{
初始化组件();
appHost=新的appHost();
appHost.Init();
appHost.Start(ListingOn);
appHost.Plugins.Add(新的ProtoBufFormat());
appHost.ContentTypeFilters.Register(ServiceStack.Common.Web.ContentType.ProtoBuf,(reqCtx,res,stream)=>ProtoBuf.Serializer.NonGeneric.Serialize(stream,res),ProtoBuf.Serializer.NonGeneric.Deserialize);
}
///
///使用singleton AppHost创建ServiceStack http侦听器应用程序。
///
公共类AppHost:AppHostHttpListenerBase
{
公共int intAppHost;
///
///使用包含服务的指定名称和程序集初始化ServiceStack应用程序的新实例。
///
public AppHost():base(“CTServer-HttpListener”,typeof(MainService.Assembly){}
///
///为容器配置ServiceStack应用程序所需的路由。
///
///与ServiceStack一起使用的内置IoC。
公共覆盖无效配置(Funq.Container)
{
路线
.添加(“/ping”);
}
}
}
公共类主服务:服务
{
任何公共响应(请求请求)
{
//在此处向全局列表添加值
myList.Add(myData);
RespPing response=新RespPing();
返回响应;
}
}
我应该在哪里定义myList,如何从该位置访问它?我怎样才能以线程安全的方式完成它?
在这种情况下,功能是存储接收到的特定值,并在另一个实例中检查该值是否已在列表中。
这是在实例之间共享数据的适当方式,还是应该遵循其他路径
谢谢!
Mattia这与ServiceStack服务无关,因为ServiceStack服务只是C#类,每次都与您注册的依赖项自动关联 因此,一般的C#规则适用,如果它是全局的,您可以将其设置为静态的,但由于ASP.NET和HttpListener是多线程的,您需要保护对它的访问,例如:
public class MainService : Service
{
static List<MyData> myList = new List<MyData>();
public RespPing Any(ReqPing request)
{
// Add a value to a global list here
lock(myList) myList.Add(myData);
RespPing response = new RespPing();
return response;
}
}
服务
谢谢,我知道这不是与ServiceStack相关的问题,但我在使用ServiceStack时遇到的问题。。。有时一些基本的C#规则会被忽略。。。我尝试了你的第一个解决方案忘记了“静态”。。。第二个很有趣,我会尝试一下,以更好地了解Funq是如何工作的。。。谢谢你的及时答复!Mattia@MattiaDurli不用担心,但最好用C#标记,这样其他人可以帮助回答。
public class GlobalState
{
List<MyData> myList = new List<MyData>();
public void AddData(MyData myData)
{
lock(myList) myList.Add(myData);
}
}
public override void Configure(Funq.Container container)
{
//All Registrations and Instances are singleton by default in Funq
container.Register(new GlobalState());
}
public class MainService : Service
{
public GlobalState GlobalState { get; set; }
public RespPing Any(ReqPing request)
{
// Add a value to a global list here
GlobalState.AddData(myData);
RespPing response = new RespPing();
return response;
}
}