bnl
bnl

Reputation: 43

creating pools for iis

I'm new to the world of .net but been programming for ~15 years. Today, we have a system (ie inhouse developed) that is client/server based. A thin client connects via socket to a business logic process (BLP).

The client sends xml messages to the BLP which replies via the same socket with xml

So far so good. The BLP is written in either unmanaged c, or in Ada (we have two systems, same design)

These BLPs is now to be published as a web service, (or at least some services they provide are)

We've decided on .net/c#/IIS7 as the application server.

The web service request comes in to the IIS, which passes it on to our c# code. Now, spawning these BLPs are quite heavy, so I wrote a pool for them, that spawns say 10 connections, and some functions that aps.net c# code can call to require a connection from the pool, and return a connection to the pool. This is basically a connected socket.

My question is then, how to make this pool static in IIS? I think it is kind of a 'database' pool, and optimally, I would have an entry in web.config that describes the connection pool.

Most of my searches describe application pools, which I think is not what I want.

Grateful for insightful comments

edit:

hmm, the poll is thread safe, so I tried with the first option first. there was a global.asax and a global.asax.cs alreay, so I put code like

public class WebApiApplication : System.Web.HttpApplication
{
    //bnl start
    private  Pool myPool = null;
    public Pool MyPool
    {
        get { return myPool; }
    }
    //bnl stop

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        //bnl start
        myPool = new Pool(3, 0, "localhost", 58031, "user", "pwd", "proj");
        myPool.Initialize();
        //bnl stop

    }
}

but then in the controller, I cannot see the pool ..

    namespace Resttest.Controllers
    {
        public class ValuesController : ApiController
        {
          // GET api/values
           public IEnumerable<string> Get()
           {  
              // the pool has method getPoolItem to return a free item/connection
              PoolItem pi = Resttest.WebApiApplication.MyPool.getPoolItem();
              return new string[] { "value1", "value2" };
           }

However the Resttest.WebApiApplication.MyPool is not seen from the controller. Being totally novice in this world, is there somthing obviously wrong? /Björn

Upvotes: 3

Views: 321

Answers (1)

Eilistraee
Eilistraee

Reputation: 8290

an ASP.NET application doesn't work like a PHP one for instance: Each ASP.NET web app runs without interruption regardless of the requests lifecycle. It's however recycled every 20 minutes by default. You can change this value in the application pool parameters.

To create static resources during application startup, Create a global.asax and (its sister the global.asax.cs) and put your code in the ApplicationStart event. There is a visual studio Template to automatically create these files.

As the heap is shared between requests, a singleton will be shared too. However, pay special intention to thread safety, as several requests can run in parallel.

The other solution (and best one althoug a lot more complicated) is to use an IoC container like Autofac and register a global implementation for your service (See the doc for more explanation, explaining how to setup and use Autofac isn't really in the scope of this answer)

Upvotes: 2

Related Questions