Is it possible to recycle a WCF web service hosted on IIS from code?
When a certain error code is received from an Oracle connection I am using, I want to recycle the web service.
If you jus want to recycle your current application then try
HttpRuntime.UnloadAppDomain();
it will unload your app from host's memory and will load again on next request.
from MSDN
UnloadAppDomain is useful for servers that have a large number of
applications that infrequently receive requests. Rather than keep
application resources alive for the lifetime of the process,
UnloadAppDomain allows programmatic shutdown of unused applications.
Related
Our web application (hosted in a Web App in Azure) experiences spikes in HTTP Queue Length. Each time there is a spike in HTTP Queue Length, the web application crashes and we either have to wait for Azure to restart the web app itself, or we restart the web app ourselves. This happens very often.
The web application does use SignalR, and a Web Job is running that calls a method on the Hub which then broadcasts data to connected clients. There is only ever a handful of users at this stage, so we have not implemented a SignalR backplane.
Here is an example of the spikes in HTTP Queue Length
Note, we tried having the web application in its very own Application Service Plan (P3) and it still exhibited the same behaviour. The memory percentage was much lower than that shown here though, around the 20-40 percent, but still crashed with regular spikes in HTTP Queue Length. Thus, I don't believe it's a memory issue that is causing the issue.
After a while of trying to diagnose this issue, we decided to then host the application (same code) into a VM (still in Azure) and change the URL to point to the VM instead of the web app. The new VM is only very basic, with only 3.5GB Memory.
Since moving to a VM, the application is performing great, no crashes and performs much better than in a Web App with a large dedicated service plan.
So it is difficult to say it is the code, when we running perfmon and other indicators, all memory and queue lengths seem to quickly drop down after serving requests. Whereas in a WebApp, this seemed to continually grow until it crashed.
Just wondering if anyone else has experienced this behaviour with Web Apps? We are going to continue hosting in a VM, but originally preferred hosting within a Web App as PaaS is more appealing.
In case it helps, more information on the tech stack is:
HTML5, C#, Web API 2, Kendo MVVM, SignalR, Azure SQL Server, Web Jobs processing Service Bus Topics.
Kind regards,
Stefan
Is it a good idea to initialize a timer to do a periodic task in a WCF service host or will that mess with the lifecycle or performance of the service being hosted?
I have a custom service host that announces it's availability (with the goal of creating a registry for clients) upon starting and stopping. I want to create a timer to do this periodically but my concern is that this will cause problems (I haven't ran into any yet but maybe I haven't stressed it enough or something) or maybe it will eat resources and kill the performance of the service. The idea is to hookup the timer on the OnOpened and OnClosing events, of course.
For what is worth, this service is hosted in IIS so it is IIS the one instantiating and managing the service host.
Is it a good idea to initialize a timer to do a periodic task in a WCF
service host or will that mess with the lifecycle or performance of
the service being hosted?
That will depend on where your WCF service is hosted. If it is hosted inside IIS it might be problematic. The reason for this is because IIS can decide to recycle the ASP.NET application pool at any time and your timer will simply stop working. It is not something you can rely upon. If on the other hand you have hosted your WCF service inside a Windows Service self host, it is fine to use a timer. You may take a look at the following blog post about the dangers of implementing recurring background tasks in ASP.NET hosts.
i have thoroughly searched the internet (most of the links sent me to stackoverflow ;)) to try to come up with a solution how to keep a WCF Service alive under IIS (7.5).
Many of the responses here were suggesting to write an application that will periodically send dummy requests to the WCF service in order to keep it alive.
My question is:
what if I create a thread in the WCF which will start when a service is first called (in a static constructor) that will periodically consume the WCF itself?
I mean for example in c#:
while (true)
{
WebClient client = new WebClient();
string returnString = client.DownloadString("http://...");
Thread.Sleep(1000 * 5);
}
assuming that "http://..." is an URI to a provided WebMethod which for example returns some integer.
Would that work?
Basically I need some kind of web service (not particulary a WCF but not a Windows Service) that is running on a server that performs some operations and updates something in a SQL Server database. So if the described approach will not work, what might be the best way to achieve this?
Go to your IIS -> Application Pool (or create new one) -> Advanced settings and set Regular Time Interval=0
See related thread here.
AppFabric allows you to create wcf services which can autostart and be long living - this might be worth checking out as a hosting option (it's just a plugin for IIS)
Auto Start
What you are doing is basically wrong from the outset.
The problem is this:
IIS is basically a stateless request broker for http requests (basic IIS) and a request broker for service requests (IIS w. AppFabric).
What you are asking for is how to turn the inherently stateless IIS into a stateful server, with eternal threads running.
That is not what IIS does, IIS handles requests and its AppDomain is subject to AT ALL TIMES be torn down (destorying all threads).
Which makes the most upvoted answer dangerous, as it teaches you how to affect the recycle process, without controlling the tear-downs (off app-domains and threads) that IIS itself will intermittenly perform.
The requester is "foreign" to the IIS itself.
The internal lifetime of the service though, is entirely managed by IIS (and the configuration of its applications) itself.
So if with "keep alive" you mean: to constantly request some service, then do as Andreas suggest further up (create a schedueled job).
If with "keep alive" you mean: to make sure the same instance of the class handles requests, then you need to look into WCF lifetimes.
If with "keep alive" you mean: to make the code you have created "stateful" and keep f.eks static variables alive and so on, well you are not accepting that IIS is basically a stateless pr. request broker with internal lifetime management.
I suggest you create a small program (console app) that calls the web service. The program should take as arguments the url of the web service. Then you create a windows scheduled task that runs the program. In this way you have a lot of flexibility as compared to the embedded approach you are querying about as the program is just another client to the web service.
Try to avoid using while loop. Maybe http://quartznet.sourceforge.net/ is something you are looking for. On WCF start create Task every 10 minutes which will cal WCF itself.
How can I keep a WCF service alive between requests?
The service is creating a simple cache in memory on initialisation and I dont want the service to have to re-initialise between requests.
The cache is built up using data extracted from a database and so for efficiency i only need this to be done rarely.
The database is not going to be updated often and so recycling the app pool every night will suffice in terms of updating the cache.
You are asking two questions. If you need service instance alive between requests you should check InstanceContextMode set to PerSession or Single. If you need application initialization to prepare your shared cache you should check IIS 7.5 Warm-up module and place your initialization to Application_Start in Global.asax. For older versions of IIS you have to write some application (console is enough) which will be scheduled to call your web application / service and warm it up.
I have a Windows Service that is exposing a WCF service thru a net.tcp channel.
Now I want this service to be exposed thru IIS, without being hosted in it. By doing that I will be able to maintain the state in the Windows Service, and I will benefit of the underlying IIS authentication and security.
Is it possible to do that just by using some configurations? Maybe a kind of proxy or passthrough?
UPDATE
Why am I doing that? A good question:
Some processes are running at a scheduled interval, asynchronously.
IIS is recycling AppPools and to trigger it, usually a web request should be issued, so that the AppPool is started.
I can't expose directly the service as a Web service in the Windows Service, because IIS is installed and binded to the IP Address that I want to use.
If I want to expose the service for many clients, using their own TLD, I don't want to have the same process running on each website (maybe for exclusive locks, or just for memory/CPU usage)
Perhaps this clarifies a little the need...
No you have to implement whole new layer in IIS. You will expose new WCF service which will call your WCF service hosted in Windows service. Is it really needed? Why don't you host the service in IIS directly or why don't you expose HTTP endpoint on your Windows service? What state do you maintain in Windows service?