自托管ServiceStack服务器中的全局变量

本文关键字:全局变量 服务器 ServiceStack | 更新日期: 2023-09-27 18:11:04

我需要在我的servicestack selfhost服务器中有一些"全局"变量,比如这里的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,我如何从那个位置访问它?我如何以线程安全的方式做到这一点?在这种情况下,功能是存储接收到的某个值,并在另一个实例中检查该值是否已经在列表中。这是在实例之间共享数据的合适方法吗,还是应该遵循另一条路径?

谢谢!Mattia

自托管ServiceStack服务器中的全局变量

这与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;
    }   
}

另一种选择是注册一个单例依赖,并让它每次自动连接到所有需要它的服务,例如:

正常依赖

public class GlobalState
{
    List<MyData> myList = new List<MyData>();
    public void AddData(MyData myData)
    {
        lock(myList) myList.Add(myData);
    }
}

AppHost

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;
    }   
}