Click here to Skip to main content
Click here to Skip to main content

SharpBITS.NET - A Wrapper for the BITS API

, 7 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A BITS wrapper library for .NET 2.0.

Sharpbits.NET - Winforms application based on the wrapper library

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 the BITS wrapper available already, also for .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

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 be met:

Client

On the 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 are 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. BitsManager is the single instance giving access to create a new job or list existing jobs.

To create a new job, the code looks as simple as that:

BitsManager manager = new BitsManager();
manager.EnumJobs(JobOwner.CurrentUser);

// populate current job list first
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);     
// list either all jobs or for current user only
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 the notification interface. In SharpBITS, this callback handling is wrapped to .NET events. There are three different kinds of events, raised if job is modified, completed, or if an error has 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.

Event handlers 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.

//Set flags to get notified on Job Completed or Job Error event
job.NotificationFlags = 
    NotificationFlags.JobTransferred | NotificationFlags.JobErrorOccured;
//never raised with the previous flags
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 the SharpBITS.NET homepage.

History

  • 07/09/2006: Posted to CodeProject.
  • 10/10/2006: Update posted to CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

xidar

Svalbard And Jan Mayen Svalbard And Jan Mayen
No Biography provided

Comments and Discussions

 
QuestionUsing like a service PinmemberErikIce5-Sep-11 7:39 
GeneralGreat wrapper and awesome support PinmemberStonkie10-Mar-11 11:14 
QuestionHow to upload file using credential in SharpBITS [modified] Pinmemberjep1tz4-Oct-10 20:01 
AnswerRe: How to upload file using credential in SharpBITS Pinmemberxidar4-Oct-10 20:04 
likely the authentication scheme would be NTLM if this is a normal Windows Server machine.
Comments in my code? Why do you think do they call it 'code' ??

GeneralRe: How to upload file using credential in SharpBITS Pinmemberjep1tz4-Oct-10 23:22 
GeneralWindows 7 x64 and VS2010 Debugger hang / program crash Pinmembermikey2229-Jun-10 14:20 
GeneralError when trying to user AddFile method PinmemberMember 23755779-Apr-10 14:59 
QuestionNew homepage for Sharpbits? PinmemberBBiales3-Mar-09 7:49 
QuestionHow to 'Delete after upload complete' Pinmembermikecarr30-Jan-09 20:13 
Questioncom visible? Pinmemberb002177521-Nov-08 3:59 
AnswerRe: com visible? Pinmemberxidar24-Nov-08 5:33 
QuestionUrgent: use sharp bits in windows service PinmemberMavei3-Nov-08 20:41 
AnswerRe: Urgent: use sharp bits in windows service Pinmemberxidar3-Nov-08 23:23 
GeneralRe: Urgent: use sharp bits in windows service PinmemberMavei4-Nov-08 0:20 
GeneralRe: Urgent: use sharp bits in windows service PinmemberBBiales3-Mar-09 7:32 
GeneralRe: Urgent: use sharp bits in windows service Pinmemberdeepthi121526-Jun-13 4:03 
QuestionHow Upload? Pinmemberrbarzallo31-Oct-08 14:47 
AnswerRe: How Upload? Pinmemberxidar3-Nov-08 23:24 
QuestionGet Job ID from OnJobCompleted? PinmemberNiteBeast18-Sep-08 13:48 
AnswerRe: Get Job ID from OnJobCompleted? Pinmemberxidar18-Sep-08 21:38 
QuestionRe: Get Job ID from OnJobCompleted? [modified] PinmemberNiteBeast19-Sep-08 5:45 
QuestionResume download from web client. Pinmemberdotnetgoodman17-Jun-08 3:18 
AnswerRe: Resume download from web client. Pinmemberxidar17-Jun-08 5:09 
GeneralBITS Server configuration PinmemberVincent DUVERNET (Nolmë Informatique)20-May-08 13:40 
Generalworking with bits over web application [modified] Pinmembercmartinez34521-Nov-07 12:51 
QuestionRe: working with bits over web application Pinmemberxidar3-Dec-07 14:02 
QuestionWrong LocalName Pinmembermsaponaro20-Sep-07 3:51 
AnswerRe: Wrong LocalName Pinmemberxidar20-Sep-07 5:10 
GeneralError while adding files for concurrent download. [modified] PinmemberSach7423-Nov-06 20:36 
GeneralRe: Error while adding files for concurrent download. Pinmemberxidar26-Nov-06 10:11 
QuestionAny idea on how to implement this on web application? PinmemberKhinLLK12-Nov-06 17:36 
AnswerRe: Any idea on how to implement this on web application? Pinmemberxidar13-Nov-06 9:15 
GeneralRe: Any idea on how to implement this on web application? PinmemberKhinLLK13-Nov-06 15:40 
GeneralRe: Any idea on how to implement this on web application? Pinmemberxidar26-Nov-06 10:13 
QuestionRe: Any idea on how to implement this on web application? PinmemberPadmaja B18-Sep-07 12:31 
AnswerRe: Any idea on how to implement this on web application? Pinmemberxidar18-Sep-07 23:36 
QuestionStable for file larger than 500MB? PinmemberDave Lenz11-Oct-06 21:43 
AnswerRe: Stable for file larger than 500MB? Pinmemberxidar12-Oct-06 4:01 
GeneralNice one PinmemberBo B18-Jul-06 23:09 
GeneralUI PinmemberDomenic11-Jul-06 4:56 
GeneralRe: UI Pinmemberxidar11-Jul-06 19:47 
GeneralRe: UI PinmemberVincent DUVERNET (Nolmë Informatique)19-May-08 12:14 
GeneralRe: UI Pinmemberxidar19-May-08 12:23 
GeneralRe: UI PinmemberNevering2-Nov-06 12:46 
GeneralProxy and Server NTLM Authentication Pinmemberandrear10-Jul-06 1:31 
GeneralRe: Proxy and Server NTLM Authentication Pinmemberxidar10-Jul-06 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Oct 2010
Article Copyright 2006 by xidar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid