I have a webservice with a .asmx extension which points to a class in my web application. After adding some code to output a debug log on application startup, I can see a new debug log is being created every time a user visits the page.
I was hoping that I could configure this web service to only ever use a single instance.
Meaning, when the first user visits the site it creates an instance of my webservice, and then all requests after are routed through that same class, sharing the same state.
Is this even possible?
Web sites are stateless in nature. Meaning, each request is generally unrelated to any other request.
That said, you could set the log as a static variable in your global.asax file. Be aware of threading issues.
Alternatively you might look at Elmah.
You seem to have a number of problems.
First of all, the ASMX technology is all but obsolete, and should not be used for new development. WCF should be used instead.
Next, yes, you can have multiple calls to the service share the same instance of your class. However, you'll have to prevent simultaneous access to this class unless it is thread safe. You should expect multiple service calls to call into the same class instance at the same time.
Finally, ASMX does not support singleton web services. The best you can do is to have multiple service calls all share the same object (carefully locked).
Related
This question might sound silly but I never got a clear answer anywhere. I am trying to figure out how asp.net application actually works, in windows app (.net) when an application is deployed on one or more PC then user can click open the app and an instance of that application is created, if user again opens the app then another instance is created (opening multiple excel files could be an example). But on web, since the application is deployed on web server and there could be multiple user requesting same app so is there a mechanism to create an instance of asp.net web app for every unique browser request? In another words if A and B are two users and access same app then does two instance (inst A , inst B) are created on web server to process the request?
Webapplication itself has one instance. Each request is handled by different threads. Each request goes through application pipeline (different for old ASP.NET, different for ASP.NET Core )which in most common scenario creates new instance of class responsible for handling request (usually controller). Rest is defined in scopes (so if some of your classes are singletons or limited instances or you use IoC containers with defined scopes), then they can share instances between different requests.
Basically what you don't see is a main loop function provided by framework, which handles all requests, creates classes instances and passes control to your code.
Asp.net along with the web server and OS use threads to handle each request. Threads are much the same as processes you describe for separate instances of Excel. The threads allow each user session to be handled independently by the web server.
I have code that calls a web service. We'll say a "3rd party" web service. So I can get the wsdl for this service, in fact I've generated a proxy class using this wsdl.
The requirement that I'm trying to meet is this: Create a web service that LOOKS just like the above mentioned one, but does other stuff. So the web service URL will be changed in a config file/database, to allow switching between the two web services.
What I'm not sure about is how I can use that proxy class that I generated, or some other method, so that the namespacing and data contract look exactly the same. I don't know much about this and these are terms that colleagues have tossed out there. I only need to actually implement one of the web service's methods in my version.
Build site with any technology you know of and return responses that match that service.
Note that if you just need to return static enough responses for occasional testing you can use Fiddler as it allows to return arbitrary responses instead of real once.
I develop web and mobile applications for my customers. In my current architecture, many resources are shared between the web access and mobile access. An aspx page can be shown on web and be called to a web view in a mobile app. My question is :
What is the difference between WebOperationContext.Current and HttpContext.Current object?
From my understanding it is the same object, but I noticed that WebOperationContext.Current is null in some cases and I don't understand why.
WebOperationContext is typically used in a WCF REST method so that method can access the incoming request and outgoing response.
HttpContext is typically used in an ASP.NET WebForms page or web method for ASMX Web Service, when incoming request and outgoing response can be accessed.
They are designed for different project types (WCF REST/ASP.NET WebForms) so you should not use them in a wrong project type.
About when the value of .Current is null, that's even more complicated. Even if you are calling this property in the correct project type, you need to make sure the call is made on a proper thread. Only on the thread that handles the request (which also sends out the response) you can access the current context. On any other threads (background threads, or new threads created by you) you get null. This has been known for years but beginners still get it wrong sometimes.
In a self hosted ASP.NET Web Api, how can I:
Detect from a class if there's an "ambient" web api context. This is needed to avoid passing in metadata information on every service call. I'm looking for the equivalent of
System.Web.HttpRequest.Current != null
How can I attach metadata information associated with the current request. Again some of this metadata is just so prevalent that including them on every method and calls is way too painful. Think transaction, multi-tenant architecture and credentialings. I need a way to make this sort of information flow through between requests without cluttering the code.
In another word I also need the equivalent of this as explained here:
HttpContext.Current.Items["user" + X.ToString()]
I think I can still access them, as long as the WebApi is hosted on IIS, but I have got these self hosted and I need a way to keep track of the ambient UoW information - how can I do so?
A few notes:
I have also contemplated using per request DI and injecting a
request context into the managers, there are however a ton of legacy
code that wasn't set up for that (some of which are static) and I
don't have the guts to blow up production by doing such a major
refactor.
I have also used a thread static, static variable - the problem with such is that the thread gets recycled, and the process hosts multiple services, some of which aren't even WebApi... so sometimes my managers thought it's handling a WebApi request when in fact it's serving a WCF one.
The HttpRequestMessage instance has Properties dictionary that is intended for holding arbitrary per-request context.
Is there a possibility to initialize a WebService.
I'm searching for a method that runs only during first call to WebService. Is there something like it in .Net ?
If you require "initialisation" the first time a given client connects to the Web Service:
Have an Initialise method that returns a token, such a a GUID, that's then required on every call made to the actual "does the work" method of your web service. You can then ensure that for that client the service is always initialised.
If you require it the first time the web service is ever called:
Add some code to your service, as a private method, that is called at the top of each public method. Within it check for the existance of something, such as a registry entry, file, database record or other persistant value. If it doesn't exist, perform your initialisation and then create the "something".
If you require it on the first call since IIS last recycled/started the Application Pool:
Have a static constructor for the class so that when it's first instantiated the static constructor runs and performs your initialisation.
If you are trying to initialize a resource that is used by the web service and you only want to initialize it once per application, you can use the Application_Start event on the Global.asax. Be aware that IIS will recycle the application pool whenever the application pool is eating up too many resources.
If you need to initialize class level variable, you can do it in the constructor of the web service. I would recommend against that, because your web service should be stateless. If you need to initialize a static resource once in your web service, you can use a static constructor.
If you need a single resource that is available once throughout your application, I would recommend you look into the singleton pattern.
When you create a WebService application in Visual Studio, by default a class named "Service" will be added. When you look at the code for this class (Service.cs), you'll see a constructor ("public Service()") with two commented-out lines. You can either put your initialization code here, or call a private method that you've defined inside the Service class. This constructor will be called only when your client makes its first call to any WebMethod in the Service class.
Well, there's no equivalent to the J2EE initialization if that's what you're after... However each web app is hosted in an application domain. Every now and then an application may be recycled and a new application domain may be created...
You can use Application_Start in global.asax, it will run once if any webservice inside your project is called