Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8153087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:54:19+00:00 2026-06-06T15:54:19+00:00

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

  • 0

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

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

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:54:21+00:00Added an answer on June 6, 2026 at 3:54 pm

    You could use a static collection:

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

    and of course since this is a multithreaded application where potentially you could have concurrent access to this collection, you must ensure to synchronize the access to it. Or if you are using .NET 4.0 simply use a thread safe ConcurrentBag<T>:

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

    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.

    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, …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following in one solution: Core Project (data access, biz logic,
I have a ASP.Net create user wizard. One of the additional steps I included
Are there any good technical solutions for extremely long term archiving of data, for
My application is a client/server solution using an ADO.NET Data Service. The Data Service
I have this persistence unit: <?xml version=1.0 encoding=UTF-8?> <persistence version=1.0 xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd>
One of our Java project needs a redundant data store system which is highly
(Only managed to find one other question on this - Object persistence strategy for
Can Silverlight store data on a user's local machine if correct permissions are enabled?
I have a requirement for implementing a data-store. Basically, I just need to be
Im working with a workflow solution. In this workflow i have an Argument that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.