Web service instantiate object C#.NET
c#, constructor, instance, web-services
Solution
I know what you mean: You don't want to repeat the connection management code in every method. That would be ridiculous, indeed.
I do it like this: I use this technique to create a per-request connection. When the WCF call has ended I destroy the connection.
So during the call I can rely on this connection being present. It will be cleaned up correctly.
Unfortunately, WCF has no equivalent for HttpContext.Items builtin so we have to hack it in using an extension.
Problem
I am pretty new to web services, C#, and .NET. I want to use a connection object in my web method in order to write data on database but I don't where to instantiate this object, it's quite ridiculous to instantiate it and then destroy it in the web method. I want my connection instance to be accessible every time,so is there a kind of a constructor or something to make this properly.