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

时间:2013-04-27 15:28:09

标签: c# servicestack

我需要在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,如何从该位置访问它?我怎么能以线程安全的方式做到这一点? 在这种情况下,功能是存储接收的特定值,如果此值已经在列表中,则检查另一个实例。 这是在实例之间共享数据的合适方式,还是应该遵循另一条路径?

谢谢! 马蒂亚

1 个答案:

答案 0 :(得分:2)

这与ServiceStack没有任何关系,因为ServiceStack Services只是每次都使用已注册的依赖项自动装配的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;
    }   
}