2

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

谢谢!马蒂亚

4

1 回答 1

2

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

应用主机

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;
    }   
}
于 2013-04-27T15:45:38.933 回答