Click here to Skip to main content
6,292,426 members and growing! (9,729 online)
Email Password   helpLost your password?
General Programming » Internet / Network » HTTP     Intermediate

SharpBITS.NET - wrapper for BITS API

By xidar

BITS wrapper library for .NET 2.0
C#, Windows, .NET 2.0, Visual Studio, COM, IIS 6, Dev
Posted:9 Jul 2006
Updated:10 Oct 2006
Views:39,566
Bookmarked:86 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
16 votes for this article.
Popularity: 5.71 Rating: 4.74 out of 5

1
1 vote, 6.3%
2

3
2 votes, 12.5%
4
13 votes, 81.3%
5

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 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);     
        // 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 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.

//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 SharpBITS.NET homepage.

History

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

xidar


Member

Occupation: Web Developer
Location: Norway Norway

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
GeneralNew homepage for Sharpbits? PinmemberBBiales7:49 3 Mar '09  
GeneralHow to 'Delete after upload complete' Pinmembermikecarr20:13 30 Jan '09  
Generalcom visible? Pinmemberb00217753:59 21 Nov '08  
GeneralRe: com visible? Pinmemberxidar5:33 24 Nov '08  
QuestionUrgent: use sharp bits in windows service PinmemberMavei20:41 3 Nov '08  
AnswerRe: Urgent: use sharp bits in windows service Pinmemberxidar23:23 3 Nov '08  
GeneralRe: Urgent: use sharp bits in windows service PinmemberMavei0:20 4 Nov '08  
GeneralRe: Urgent: use sharp bits in windows service PinmemberBBiales7:32 3 Mar '09  
GeneralHow Upload? Pinmemberrbarzallo14:47 31 Oct '08  
GeneralRe: How Upload? Pinmemberxidar23:24 3 Nov '08  
QuestionGet Job ID from OnJobCompleted? PinmemberNiteBeast13:48 18 Sep '08  
AnswerRe: Get Job ID from OnJobCompleted? Pinmemberxidar21:38 18 Sep '08  
QuestionRe: Get Job ID from OnJobCompleted? [modified] PinmemberNiteBeast5:45 19 Sep '08  
QuestionResume download from web client. Pinmemberdotnetgoodman3:18 17 Jun '08  
AnswerRe: Resume download from web client. Pinmemberxidar5:09 17 Jun '08  
GeneralBITS Server configuration PinmemberVincent DUVERNET (Nolmë Informatique)13:40 20 May '08  
Generalworking with bits over web application [modified] Pinmembercmartinez34512:51 21 Nov '07  
QuestionRe: working with bits over web application Pinmemberxidar14:02 3 Dec '07  
QuestionWrong LocalName Pinmembermsaponaro3:51 20 Sep '07  
AnswerRe: Wrong LocalName Pinmemberxidar5:10 20 Sep '07  
GeneralError while adding files for concurrent download. [modified] PinmemberSach7420:36 23 Nov '06  
GeneralRe: Error while adding files for concurrent download. Pinmemberxidar10:11 26 Nov '06  
GeneralAny idea on how to implement this on web application? PinmemberKhinLLK17:36 12 Nov '06  
GeneralRe: Any idea on how to implement this on web application? Pinmemberxidar9:15 13 Nov '06  
GeneralRe: Any idea on how to implement this on web application? PinmemberKhinLLK15:40 13 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Oct 2006
Editor: Deeksha Shenoy
Copyright 2006 by xidar
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project