How to calculate social share count efficiently using C# - c#

I am working on classified ads web application using asp.net Mvc-3 C#.
Every ad can share on social media like facebook,twitter,google plus ,linkedin.
I am calculating how many times ad is share on these website and saving this record in database.
Using this function
string jsonString = new System.Net.WebClient().DownloadString(URL-of-ad);
var serialize= new System.Web.Script.Serialization.JavaScriptSerializer();
var dictionary = serialize.Deserialize<Dictionary<string, string>>(jsonString);
var twitter = dict["count"];
var facebook= dict["share_count"];
// calculating for other website by this function
This is function is working fine but very time consuming for every ad .I have thousand of ads so i have to loop through thousand time and call this function for every ad.
I am calling this function twice in a day.
There is any other better and fast way to do this task?
Please give your suggestions.

You may think of improving your design
1 - Create a Windows Service and set it to run twice a day , most probably figure out when the traffic on your site is very minimal.
2- Move the Sharing count logic into windows service which will do it in another process and free up your main application.
3- Downloading something which is dependent upon the network speed so make yopur downloading Asynchronous rather than synchronous.

A few suggestions:
Run this in parallel, such as using Parrallel.ForEach, passing in the URL of the add, or an async version, such as DownloadStringAsync. Both will run multiple downloads at the same time.
Run this when the page is loaded that shows the add. Load the add then do a web service call to the server to get the count and share_count. It may be a bit more work but it will be more up to date and, if each add is shown rarely, this may be more efficient.
I assume you are not running this during page creation but, if you are, move it to a separate service or process as Saurabh suggests.

Related

Server side project C# asp.net

i have full access to server i want to run C# code on server automatically and every minute it should repeat. Is there any techniques or web services allow allow asp.net project regarding same?
I cant able to find correct reference. Post a link here if possible. I have ready code that allows my work on Page_Load. But I want to make such like , whole code should be Running every 5 minutes.
RFID #Attendance #RealTime
Similar question to what you have asked.
Best way to run scheduled tasks
All of my tasks (which need to be scheduled) for a website are kept
within the website and called from a special page. I then wrote a
simple Windows service which calls this page every so often. Once the
page runs it returns a value. If I know there is more work to be done,
I run the page again, right away, otherwise I run it in a little
while. This has worked really well for me and keeps all my task logic
with the web code. Before writing the simple Windows service, I used
Windows scheduler to call the page every x minutes.
Another convenient way to run this is to use a monitoring service like
Pingdom. Point their http check to the page which runs your service
code. Have the page return results which then can be used to trigger
Pingdom to send alert messages when something isn't right.
You have many options:
1- put the code in a web service, and create a consumer windows service to invoke it every N minutes.
2- put your code in a windows service and execute it from a timer.
3- Make a thread in the web-page (Not recommended) to execute your code.
Hope that helps you

Keep MSSQL database (application pool) responsive C#

Firstly let me apologise, as I don't really know how to phrase the question.
The issue I'm having is trying to keep my database 'alive' while users come to my site. An example being, if I build my c# asp.net application and publish it, then try and navigate to it, it takes a while to respond (which I get, I understand it, this isn't an issue for me) the problem is if some person hasn't been to the site for a while, it seems to take a while again, like a session timer has passed, I'm not sure if this is something to do with App Pool recycling?
I've tried to run a scheduled task to hit the database (trying to keep it responsive) every 15 minutes, but this doesn't seem to work, it works well every 15 minutes for say 5 hours, and then I receive a message on a random call that the request has taken over 4 seconds to respond and therefore fails.
My question then, how do I keep my connection to the database / the site responsive so that each time a person requests it, the site loads quickly, rather than having to 'start up'
Kind regards as always
I suggest to increase connection pool size in your connection string.
This looks like what you want:
Keep an ASP.NET IIS website responsive when time between visits is long: Keep an ASP.NET IIS website responsive when time between visits is long
You might consider IIS application auto-start?
Some web applications need to load large amounts of data, or perform expensive initialization processing, before they are ready to process requests. Developers using ASP.NET today often do this work using the “Application_Start” event handler within the Global.asax file of an application (which fires the first time a request executes). They then either devise custom scripts to send fake requests to the application to periodically “wake it up” and execute this code before a customer hits it, or simply cause the unfortunate first customer that accesses the application to wait while this logic finishes before processing the request (which can lead to a long delay for them).
ASP.NET 4 ships with a new feature called “auto-start” that better addresses this scenario, and is available when ASP.NET 4 runs on IIS 7.5

How to get the statistics of a winform app usage

Say I created a winform app and distributed to anonymous users, and I want to have a way to get the statistics of user opens the app, one way I can think of is opening a webpage (lightweight) on app startup then analyzing how many times the webpage is opened.
Any other ways to get the statistics?
The best way is to use the existing tools out there and implement them in your application for statistical usage and analytics for example as mentioned before: EQATEC.
you also have Preemptive analytics: http://www.preemptive.com/products/runtime-intelligence/overview
This is a most common used tool especially in the whole ALM lifecycle process and how applications and companies progress onwards with minimal effort story.
you also need to know exactly what kind of statistics are you monitoring here? number of times your app is run on a particular version of the software? particular screens being used within your app? memory/CPU usage? etc...
you also have trackerbird:
http://www.trackerbird.com/
There are many ways.
The initial idea of having your application "phone home" to count its usage isn't that bad, but it does not have to be an entire web page.
You could just post some data to a webservice.
If it can't connect to your web service, you could store the information locally and send it next time the app starts with a valid web connection.
If you do this asynchronously, it should be hardly noticeable when you start the app.
You can have your own tracking service, which you can consume every time when application starts. Alternative way you can use any third party application/service which provides this kind of functionality. Telerik eqatec analytics could be one of its kind.

Determining programmatically whether an ASP.NET/C# Website is down

I am having an ASP.NET/C# Web application hosted in IIS6. My requirement is to send a mail whenever the Website is down without using any third party tool. How can I accomplish this job programmatically (of course using C#)? Thanks in advance!!!!!
You will need a PC that is as independent as possible form the WebServer. Ideally on the other side of the world.
Then run a little program with a Timer and check every X minutes. Do a simple grab with WebClient. If it fails, send the mail.
For improved reliability, run more instances of the monitoring program at different locations.
Define "down". There are many reasons why a website might not be accessible or only partially working. Ultimately, it's really what the end user is seeing that's most important. A tool that is running outside of the website's network infrastructure that periodically queries the website's key pages and checks important factors such as the HTTP status code, the response time, the size of the page and even possibly checks that important chunks of HTML are present would achieve this.
Attempting to determine why the site is not responsing is an even more complex task that would involve checking for the presence of the IIS application pool, etc.
This is not a trivial tool to create so I would recommend using an off-the shelf solution if possible.

ASP.NET Threading - can I do Async methods or do I use threading?

My environment - C# 3.5 and ASP.NET 4.0 and VS 2010
Apologies - am a bit new to some of the concepts related to threading and Async methods.
My scenario is this:
My site will periodically make a couple of GET/POSTS to an external site and collect some data
This data will be cached in a central cache
The periodic action will happen once in about 5 minutes, and will happen for every new member who registers on my site. The querying for the member will stop based on certain conditions
The user does NOT need to be logged in for these periodic queries - they register on the site, and then off my async code goes - it keeps working 24/7 and messages the user once a while via email depending on certain trigger condition. So essentially it all should happen in the background regardless of whether the user is explicitly logged in or not.
Load Expected - I anticipate about 100 total running members a day (accounting for new members + old ones leaving/stopping).
the equation is ~ 100 visitors / day x 4 POST per fetch x 12 fetches / hour x 8 hours / day
In my mind - I'm running 100 threads a day, and each 'thread' wakes up once in 5 minutes and does some stuff. The threads will interact with a static central cache which is shared among all of them.
I've read some discussions on ThreadPools, AsyncPage etc - all a bit new territory. In my scenario what would you suggest? What's the best approach to doing this so it's efficient?
In your response I would appreciate if you mention specific classes/methods/links to use so I can chase this. Thanks a bunch!
You will not be able to do it with ASP.net as such, you will not be able to keep the "threads" running with any level of reliability. IIS could decide to restart the appication pool (I.E. the whole process) at any point in time. Really what you would need is some kind of windows service that runs and makes the requests. You could the use HttpWebRequest.BeginGetResponse method to make your calls. This will fire off the relevent delegate when the response comes back and .net will manage the threading.
Agreeing with Ben, I would not use threading in IIS with ASP.NET. It's not the same as using it in a desktop application.
If you're going to use some kind of polling or timed action, I recommend having a handler (.ashx) or asp.net page (aspx) that can take the request that you want to run in the background and return XML or JSON as a response. You can then set some javascript in your pages to do an AJAX request to that URI and get whatever data you need. That handler can do the server side operations that you need. This will let you run background processes and update the front-end for your users if need be, and will take advantage of the existing IIS thread pool, which you can scale to fit the traffic you're getting.
So, for instance
ajaxRequest.ashx : Processes "background" request, takes http POST/GET parameters.
myPage.aspx : your UI
someScript.js : javascript file with functions to call ajaxRequest.ashx from myPage.aspx (or any other page) when certain actions or intervals occur.
jQuery.js : No need to write all the AJAX code or event handlers yourself :)
You will need to create a separate Windows service(or a console app that runs using the Windows scheduler) to poll the remote server.
If you need to trigger requests based on user interation with your site, the best way is to use some kind of queuing system(eg MSMQ) that your service monitors.

Categories