Can IIS hosted WCF services perform background tasks? - asp.net

I know that probably this question would be asked to many times but...
The wcf services hosted on iis7 in an asp. Net Web site can do things automatically? Like post a message to a pre-configured wall on Facebook given the permission to the application in a pre scheduled time?
For this to happen a client must send a request or it can do it alone?

The lifetime of a WCF service is typically determined by requests from a client i.e. if there is no client making requests then there is no service running.
Possible solutions:
Create a custom WCF ServiceHost, override the OnStart OnStop methods and create a background task.
Create an ASP.NET background task (external to WCF), which you can do with a library such as WebBackgrounder
Use the Windows Task Scheduler to trigger a task which polls your WCF service periodically which can then post outstanding messages to Facebook.

Related

Run background task with console application from asp.net

My requirement is to send emails within the web application from an external server that takes around 4 to 5 seconds on average to process and send the confirmation email. I do not want the user to wait for this, so i trigger a console application with Process().StartInfo.Start() and it does it in background. The question is how much i can rely on this as during normal days the application sends around 10 thousand emails daily and in high traffic days it may surpass 80 thousand. What possible issues/problems the application or server may run into? Is there any better solution for this?
You can use following method to achieve this:
Create a .aspx page and write you business code here (e.g. email
send)
Where required, call this page using Javascript along with parameters in querystring. You can create an image element and set its source to that page.
var img = new Image();
img.scr = "perform-operation.aspx?[parametervalues]";
Thank you
Console application has drawback, if you plan to invoke console application for each email separately, any new process takes long to load and clean up, this is too much overhead on cpu. Instead, the best alternative is to host another website in IIS, this new website will have its own Application Pool, which will create and host process. IIS will shutdown the process if no request is served. You can setup Web Farm by running more worker process per application.
Other alternative is to run console or windows service all the time, let it be in the memory but has a web service host listening for email requests, if a request is received from your web application, this background service will send email on new thread and go back on waiting state. Basically it is a self hosted WCF service. This is quite same as hosting another website on IIS.
I prefer IIS based hosting as it is easy to setup, does not require any extra permissions and in future, to scale horizontally, you can easily move this mail service to other server.

SignalR in WCF service to update web site clients

Using SignalR, is it possible to update website clients from my WCF service if the service is not used by these clients directly?
I have a desktop application in .NET which has WCF service used internally using net.TCP protocol. This application changes one of the status fields in database table depending on certain user actions. I want to notify this change to end users who are accessing a different website hosted on the same web server.
I have tried one SignalR sample where notification works fine if it is sent from same website's host to its own client (stock ticker sample). But in my case, the message should go from WCF service to a website client.
IMO you should do an intermediate hop, for example having your website exposing an endpoint (you pick the technology) where you can post whenever you have a change to notify. Your WCF service would post there whenever there's a change, and the web app would process the post by broadcasting info to the target clients (can be all, or can be just some you filter with some logic behind the post). I use this pattern quite frequently, implementing it with HTTP POST. You would have no issues to implement the SignalR infrastructure in the web app, which is where your clients already connect to.

Use a Windows WCF Service in asp.net application

I am new to the WCF model, but I have created a service, that seems to work in VS2010
Now I would like to access this windows service from an asp.net application. The reason behind this is: I want to be able to call the service to connect via SSH to a server, and keep that connection open (as I did in a Windows Forms app)
Any ideas on how to go about doing this? I tried it with an IIS hosted service, but the connection is lost each time the request finishes.
At least in VS2010 with the tester, I can invoke my connect function, then invoke other functions to query data, then disconnect.
FYI, the local Windows Service and IIS are on the same box.
WCF services by default create a new instance for each request. There are other models, although I'm not sure they will manage as long running a process as you want. This article has some good info on managing sessions: http://msdn.microsoft.com/en-us/magazine/cc163590.aspx
Alternatively, why not use a technology like workflow foundation, that was designed to run / persist / re-awaken long-running processes? http://msdn.microsoft.com/en-us/library/ee342461.aspx

WCF service singleton with callback and hosted on IIS?

I have a WCF service hosted at IIS7 web application. It's created by a WebServiceHostFactory. The client connects to a service calls the Collect method, and data are stored to DB. All working fine.
Now I would like to refresh page every time the new data are "collected" (i.e. the service method Collect is called).
My question is: What is the best approach ?
I was considering the CallbackContract, but this would require a singleton pattern (service is now PerCall), or is it a wrong assumption ? Is this approach possible ?
My logic is:
ASP.NET page subscribes to WCF service
the service singleton is created from now on
when method is called the services calls subscribers (clients)
there should be therefore only one service instance in order to subscription to work (or is it ?)
the client page refreshes itself
regards,
Kate
You can't refresh the page in a user's browser from the sever. Browsers use HTTP, which is a request-response protocol, so if the browser hasn't issued a request, it won't be looking for a response from your server.
If you have a Silverlight application hosted in a browser, that's a different story, but you didn't mention Silverlight anywhere. You would also be able to do what you're asking using WebSockets in HTML5, but that's not fully standardized yet.

Application_BeginRequest doesn't fire In Web Service

I created a web service on localhost, and I tried to call it from a web app (also on the localhost) via HttpWebRequest, but Application_BeginRequest in Global.asax didn't fire. When I type in IE 'http://localhost:8010/Test/' (the web service) Application_BeginRequest fires. Where is the problem? How can I test a localhost web service from a page which is also on localhost?
Generally, the best way to test a web service is to write a client to consume it. In particular, you can use a unit test framework to write automated tests of the service.
WCF doesn't even offer the web pages that allow you to test a service, BTW.
Not sure if you have a WCF web service or not, but if you do check out, "WCF Services and ASP.NET". WCF services aren't handled by IIS in the same way as web sites are, which may be the reason BeginRequest doesn't get hit:
The ASP.NET HTTP runtime handles ASP.NET requests but does not participate in the processing of requests destined for WCF services, even though these services are hosted in the same AppDomain as is the ASP.NET content. Instead, the WCF Service Model intercepts messages addressed to WCF services and routes them through the WCF transport/channel stack.

Resources