在Web服务中的数据持久性? [英] Data persistance in Web Services?

查看:122
本文介绍了在Web服务中的数据持久性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个.NET Web服务的数据持久性解决方案?

What are the solutions for data persistance in a .NET webservice?

我有一个web服务。我举一个ID为我的web服务,这一次返回正确的客体。

I have a webservice. I give an id to my webservice and this one return the correct objet.

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/GetMyObject?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        MyObject GetMyObject(string id);

我不希望使用一个数据库。我想继续我的收藏对象的记忆。所以我决定在我的web这样创建一个简单的对象

I don't want to use a database. I would like to keep my collection of objects in "memory". So I decided to create a simple object in my webservice like this

    public class Service : IService
    {
        List<MyObject> list = new List<MyObjec>();

        public Service()
        {
            list.Add(new MyObject() { Id = 1, Data = ...} );
            list.Add(new MyObject() { Id = 2, Data = ...} );
            list.Add(new MyObject() { Id = 3, Data = ...} );
            list.Add(new MyObject() { Id = 4, Data = ...} )
            ...
        }

        public MyObject GetMyObject(string id)
        {
            // code to get my object from the list
            return myObject;
        }
}

它的工作原理,但构造函数被调用各次我打电话给我的web服务,我想初始化这个列表一次,以后应用就可以了修改。我应该如何初始化我的清单,并坚持呢?

It works but the constructor is called each times I call my webservice and I would like to initialize this list once and apply modification on it later. How should I initialize my list and persists it?

推荐答案

您可以使用一个静态的集合:

You could use a static collection:

private static List<MyObject> list = new List<MyObjec>();

,当然因为这是一个多线程的应用程序,其中可能,你可以同时访问这个集合,你必须确保同步访问。或者,如果您使用的是.NET 4.0只使用一个线程安全的 ConcurrentBag&LT; T&GT;

private static ConcurrentBag<MyObject> list = new ConcurrentBag<MyObjec>();

当然,你应该完全正常意识到,通过使用内存结构来存储你的数据的数据生命基本上依赖于Web应用程序的生命。而且,由于IIS可以回收在任何时刻的应用领域(不活动一定时期,一定的CPU /内存达到阈值)已存储到内存中一切顺利的进入空间。

Of course you should perfectly fine be aware that by using an in-memory structure to store your data your data life is basically tied to the life of the web application. And since IIS could recycle the application domain at any moment (a certain period of inactivity, certain CPU/memory thresholds are reached) everything you have stored into memory goes into the void.

顺便说一句,如果你走这条路,是prepared这在每次重新编译你的web服务时经常发生,因为通过重新编译你基本上修改的bin文件夹中的组件和Web服务器将简单地回收应用程序。

By the way if you go that route, be prepared this to happen very often, every time you recompile your web service, because by recompiling you are basically modifying the assemblies in the bin folder and the web server will simply recycle the application.

所以是的,这一切墙上的文字来告诉你要坚持你的数据在其他地方比在内存:-)你有这么多的可能性,从不同的格式,数据库,嵌入式数据库,文件...

So yeah, all this wall of text to tell you to persist your data somewhere else than in-memory :-) You've got so many possibilities ranging from files in different formats, databases, embedded databases, ...

这篇关于在Web服务中的数据持久性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆