views:

36

answers:

3

First, I don't want to use a database or a local file.

Is it possible to store variables in memory so they don't have to be created each time the webservice is called? The problem is that

FullCompanyListCreateXML();

takes about 1 Minute to create the XDocument. So is there any way to prevent that the xml variable is dropped after the webservice call is finished?

    [WebMethod(CacheDuration = 120)]
    public String FullCooperationListChunkGet(int part, int chunksize)
    {
        StringBuilder output_xml = new StringBuilder();
        XDocument xml = FullCompanyListCreateXML();
        IEnumerable<XElement> xml_query = xml.Root.Elements("Cooperation").Skip(part * chunksize).Take(chunksize);

        foreach (XElement x in xml_query)
        {
            output_xml.Append(x.ToString());
        }
        output_xml.Insert(0, "<Cooperations>");
        output_xml.Append("</Cooperations>");
        return output_xml.ToString().Zip();
    }

Thanks,
Henrik

+1  A: 

Write the xml to a stream that actually points to a MemoryStream.

Then read it back using something such as a StreamReader

Ash
+1  A: 

2 ways:

1) You can add a global.asax class to the web service which will get created when the web application is first started, and will persist for as long as the worker process keeps it in memory. Override the

2) create a static class to hold the values

See this blog entry, or this post on the topic of adding the global.asax class to web services. You can create things when the session starts, or when the application starts by overriding the Session_Start or Application_Start methods

Oplopanax
+1  A: 

You could look into using the built-in caching mechanism in ASP.NET. It's rather easy to use.

Fredrik Mörk