
Introduction
SharpBITS is a library to wrap the BITS SDK for use with managed code on the .NET platform. It was intended and first implemented for a project designed to transfer large files over a slow and unstable WAN link. While there are some other options like use of FTP, BITS had some real advantages that made it the favorite selection.
As it uses the HTTP/HTTPS protocol to transfer files, it is immune to firewall restrictions. Also it keeps a session (in default setting) for up to 14 days, which gives a reliable file transfer option in this case! The project where SharpBITS was developed consists of a small command line application, and the separate BITS wrapper library. Even if there are some custom implementations for BITS wrapper available already, also for the .NET managed code (see "Wrapping Bits" on MSDN), they are based on older versions of BITS and don't support some features like authentication, so the decision was to have a new C# based wrapper library.
Background
The Background Intelligent Transfer Service (BITS) can be used to transfer (large) files asynchronously between a client and a server. Background transfers are optimal in that BITS uses idle network bandwidth to transfer the files and can increase or decrease the rate at which files are transferred based on the amount of idle network bandwidth available. If a network application begins to consume more bandwidth, BITS decreases its transfer rate to preserve the user's interactive experience. BITS continues to transfer files after an application exits if the user who initiated the transfer remains logged on and a network connection is maintained. BITS suspends the transfer if a connection is lost or if the user logs off. BITS persists transfer information while the user is logged off, across network disconnects, and during computer restarts.
The complete description and documentation on BITS can be found on MSDN.
Requirements
For using BITS, the following requirements must been met:
Client
On client side, Windows 2000 SP3 or later is required and the BITS service must be running. BITS is also supported on Windows Vista and Longhorn Server (with upcoming new features like P2P-functionality).
Windows 95/98/ME/NT is not supported.
Server
For uploads, BITS requires IIS 5.0 on Windows 2000 Server and IIS 6.0 on Windows Server 2003 family; BITS does not support IIS 5.1 on Windows XP.
Using the code
All you have to do for getting started with SharpBITS, is to create an instance of BitsManager. The BitsManager is the single instance giving access to create a new job or listing existing jobs.
To create a new job, the code looks as simple like that:
BitsManager manager = new BitsManager();
manager.EnumJobs(JobOwner.CurrentUser);
BitsJob job = manager.CreateJob("TestJob", JobType.Download);
job.AddFile("http://localhost/bits/mydownload.cab", "C:\\temp\\download.cab");
job.Resume();
Also it is very simple to iterate through all existing jobs and list their properties. Each job has various properties like name, owner, priority, and of course a list of files to transfer. (While a download job can have multiple files to transfer, an upload job is restricted to one file per job!)
manager.EnumJobs(JobOwner.CurrentUser);
System.Console.WriteLine("Job Count for current user: {0}",
manager.Jobs.Count.ToString());
foreach (BitsJob job in manager.Jobs.Values)
{
System.Console.WriteLine("Displayname for job: {0}", job.DisplayName);
System.Console.WriteLine("Owner: {0}", job.Owner);
System.Console.WriteLine("Priority: {0}", job.Priority.ToString());
job.EnumFiles();
System.Console.WriteLine("File Count for current job: {0}",
job.Files.Count.ToString());
foreach (BitsFile file in job.Files)
{
System.Console.WriteLine("Local File Name: {0}", file.LocalName);
System.Console.WriteLine("Remote File Name: {0}", file.RemoteName);
System.Console.WriteLine("Bytes total: {0}",
file.Progress.BytesTotal.ToString());
System.Console.WriteLine("Bytes transfered: {0}",
file.Progress.BytesTransferred.ToString());
}
System.Console.WriteLine("Job State: {0}", job.State.ToString());
System.Console.WriteLine("Job Type: {0}", job.JobType.ToString());
}
Instead of polling for the status of a job all the time, BITS enables to set a callback pointer for notification interface. In SharpBITS, this callback handling is wrapped to .NET events. There are 3 different kinds of events, raised if job is modified, completed or if an error occurred. The JobModifiedEvent event will also be fired if the JobProgress or FileProgress for a job is updated, so this can be used to display a job progress.
Eventhandlers can be attached to either a job to get events for that particular job only, or to the BitsManager to catch all events. Even for those, you have to set the kind of events fired for each particular job.
job.NotificationFlags =
NotificationFlags.JobTransferred | NotificationFlags.JobErrorOccured;
job.OnJobModifiedEvent +=
new EventHandler<JOBNOTIFICATIONEVENTARGS>(job_OnJobModifiedEvent);
Credits
The interop handling in this library is based on some implementations from the "Microsoft Updater Application Block for .NET" (http://msdn2.microsoft.com/en-us/library/ms978574.aspx), which is part of the Enterprise Library.
Conclusion
As it seems BITS can be used to fulfill various requirements for transferring files in a reliable manner, and is somewhat underestimated currently, I started to develop a Windows forms application also (where the above screenshot was taken from). This application development is still in progress, but a running version and more details (source code as well) can be found on SharpBITS.NET homepage.
History
- 07/09/2006: Posted to CodeProject.
- 10/10/2006: Update posted to CodeProject.
| You must Sign In to use this message board. |
|
|
 |
|
|
 |
|
 |
I noticed in your GUI front-end that when you create an upload job you have an option to 'Delete after upload complete'. What is the best way to implement that? If I register for the OnJobTransferred event, what happens if the job completes after the app is exited?
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |
|
|
 |
|
 |
It should work from VB6 as well via COM, but I don't have a sample yet. However, I wonder if it's worth the overhead of encapsulating a COM to .NET to wrap into COM again... all of the functionality of BITS is available using the COM interfaces, should be pretty easy to use directly with VB
Comments in my code? Why do you think do they call it 'code' ??
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
i use sharp bits to download files from a web server, the server requires wondows authentication, it works good when i build the code as an application; but when i changed the code to ruan as a windows service(the service run as Local System), the bit job always reports some thing like that "...server resource requires auth..", my code as below: BitsManager manager = new BitsManager(); manager.EnumJobs(JobOwner.CurrentUser); BitsJob newJob = manager.CreateJob(jobName, JobType.Download); //set authentication if server require it BitsCredentials credit = new BitsCredentials(); credit.AuthenticationScheme = AuthenticationScheme.Negotiate; credit.AuthenticationTarget = AuthenticationTarget.Server; credit.UserName = "DownloadAuthenticationUserName"; credit.Password = "DownloadAuthenticationPassword"; newJob.AddCredentials(credit); .... newJob.AddFile(remoteFn, localFn); newJob.Resume(); ...
i checked msdn about BITS: http://msdn.microsoft.com/en-us/library/aa363152(VS.85).aspx[^], but i don't understand.
Can anyone tell me how to run BITS with authentication in windows service? thanks in advance.
Mavei mavei.ma@gmail.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
have you tried AuthenticationScheme.NTLM?
This should be the right setting for windows authentication.
Comments in my code? Why do you think do they call it 'code' ??
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Yes, i tried and got the same error. i event tried to let service running in an user acount, but all jobs are suspended(resume it and still suspended.).
any idea?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
it's same as for downloads, just set the JobType appropriate. Of course your server needs to be enabled for BITS uploads (IIS has some self explaining settings there).
Comments in my code? Why do you think do they call it 'code' ??
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
When I create my OnJobCompleted event handler the EventArgs are for JobNotificationEventArgs which doesn't expose any way to attain the Job name or ID. NotificationEventArgs does expose a Job object though.
Is this an oversight or is there a way to get a job ID from JobNotificationEventArgs?
Thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
as the event is tight to a particular job (actually you subscribe to myJob.OnJobCompleted event), you should know which job this was. If this is not sufficient, you can subscribe to similar event on the BitsManager instance, which passes an additional reference to the job the event was raised for.
Comments in my code? Why do you think do they call it 'code' ??
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Wow, quick reply... awesome.
I'm going to need to add some reference to the calling job in the handler so I can call the Complete() method and update my Database. Currently I'm doing it through a hacked up messaging system that really isn't what I'd like to do.
I think I'll look at the BitsManager object events and use that.
Thanks again for getting back to me so fast. 
Edit -
Well, I was having an issue and had written something here but I solved it. The BitsManager event was the solution.
modified on Friday, September 19, 2008 12:10 PM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
My requirement is that any authorised user can download relatively a large file from server through my web site. So, i have to provide a resume later option for downloading. Where in, if the user come again for the same page, i should alow him to download from the place where he left over.
Can i use BITS for this, or can somebody pour some light on this.
Warm Regards, Dinakaran.
"Commonsence solve always better than technicality"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
BITS has authentication built in. So the user not only has to authenticate to the webpage containing the URL, but also within BITS to authenticate with the download. The credentials are then stored with the job (encrypted, of course) so the user can resume any time, fully authenticated, without having to go to your website again. BITS will handle all those stuff for you, and SharpBITS adds as the .NET wrapper to this
Comments in my code? Why do you think do they call it 'code' ??
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I have a question about bits 2.5 and It's Arquitecture. The Company developed a web Application that centralizes all transfer form one site to another (diferent IP's) using the http protocol over a bandwith of 2 mb and works fine. I would like to know if during the transfer, the information is retrieved from the origin to the central server (web application) and then sent to the destination, or if Bits sends the información (makes some kind of reference) directly from the origin to the destination. My guess is that Bits makes some kind of reference.......
I would like to avoid a very slow functioning when 80 transfers arrive.
What is the best to do ......
-- modified at 12:07 Wednesday 28th November, 2007
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
I don't really got your point, what information are you talking about?
Comments in my code? Why do you think do they call it 'code' ??
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi, I have a problem with the LocalName I add a file to a job in this way newJob.AddFile("http://localhost/bits/sole.doc", "C:\\Bits\\Client\\sole.doc"); The transfer is OK but the name of the file is something like BIT5CA.tmp Can someone help me?
Thanks!!!!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
that's correct until you call Complete() on the Job after the transfer is finished (when JobState becomes "Transfered")
Comments in my code? Why do you think do they call it 'code' ??
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
For single download its working but if change for concurrent download then throws error, could you please help me to find out where i am making mistake. that is coming at job1.AddFiles(BitsFileList);
BitsManager manager1 = new BitsManager(); manager1.EnumJobs(JobOwner.CurrentUser);
BitsJob job1 = manager1.CreateJob("Download Xml", JobType.Download);
System.Collections.ObjectModel.Collection<BitsFileInfo> BitsFileList = new System.Collections.ObjectModel.Collection<BitsFileInfo>();
//====this block is in loop to add multiple files===== BitsFileInfo FileInfo = new BitsFileInfo(RemotePath, LocalPath); BitsFileList.Add(FileInfo); //======
job1.AddFiles(BitsFileList); job1.EnumFiles(); job1.Resume();
One more issue to is there any call to activate concurrent download ?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Are you using the latest version of SharpBits available from the Homepage (http://sharpbits.xidar.net)? There was a bug using the AddFiles which is fixed already some time ago, but not posted to CP.
It's not possible to have concurrent downloads with BITS or either SharpBITS, this is by design.
Good code is like art - at least the author has to understand what it should express.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Hi,
Nice work! Do you have any idea on how to impelemnt the same behaviour on web applications? I'm using VS.Net 2003 ( ASP.Net 1.1 ) I'm looking for the same feature for my web project. Any suggestion is very much appreciated! Thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
what behaviour do you mean? BITS is a windows service and can be used on any windows machine (>=Win2k). So even a web application on ASP.NET 1.1 should be able to use. Or please specify your requirements a bit more detailed, so we get a picture what you're trying to achieve.
Good code is like art - at least the author has to understand what it should express.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Actually, i'm trying to implement resume download/upload feature in web application. Some of people suggest to use BITS. So I was wondering is that possible to use in web application. Thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|