IT tutorials
 
Technology
 

Windows Phone 8 : Background File Transfer Sample Code - Using URL Rerouting with a WCF Service

3/23/2014 9:23:09 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Within the BackgroundAgents solution in the downloadable sample code is a project named WPUnleashed.BackgroundAgents.Web. This project exposes a WCF service named BackupService, which allows the phone app to save files to the Backups directory on the server by way of its SaveFile method (see Listing 1). The SaveFile method accepts a Stream, which is written to a file on the server. The unique ID of the user is also sent to the server to allow correct retrieval of the file at a later time.

LISTING 1. BackupService Class


[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(
    RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class BackupService : IBackupService
{
    public void SaveFile(string userId, string fileName, Stream fileStream)
    {
        string location = string.Format(
                             @"~\Backups\{0}_{1}", userId, fileName);
        var filepath = HttpContext.Current.Server.MapPath(location);
        using (Stream outputStream = File.OpenWrite(filepath))
        {
            CopyStream(fileStream, outputStream);
        }
    }

    static void CopyStream(Stream input, Stream output)
    {
        var buffer = new byte[8 * 1024];
        int streamLength;

        while ((streamLength = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, streamLength);
        }
    }
}


There is no retrieval method in the WCF service because an ordinary HTTP GET request is used to download the file.

The WPUnleashed.BackgroundAgents contains a web reference to the WPUnleashed.BackgroundAgents.Web project, and the service is consumed within the TodoListViewModel class.

1. Using URL Rerouting with a WCF Service

The Web Application project uses URL rerouting to allow the BackgroundTransferRequest to pass the user ID and filename to the service via the URL.

The routing system on the server is initialized in the RegisterRoutes method of the Global class in the Web Application project. The URL routing APIs reside in the System.Web.Routing namespace. A new ServiceRoute is added to the RouteTable, so that when a request for the URL BackupService arrives, the built-in WebServiceHostFactory creates an instance of the BackupService class to service the request. See the following excerpt:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    void RegisterRoutes()
    {
        RouteTable.Routes.Add(new ServiceRoute(
            "BackupService", new WebServiceHostFactory(),
             typeof(BackupService)));
    }
...
}

The service interface for the backup service defines a UriTemplate that maps the incoming request URL, which includes the user ID and filename, to the SaveFile method parameters. This means that we are able to translate the incoming URL and forward the call to the service method. See the following:

[ServiceContract(Namespace = "http://danielvaughan.org")]
public interface IBackupService
{
    [OperationContract, WebInvoke(
        Method = "POST", UriTemplate = "UploadFile/{userId}/{fileName}")]
    void SaveFile(string userId, string fileName, Stream fileStream);
}

URL rerouting is an elegant way of providing a WCF service with extra information, such as the user’s ID, while still remaining compatible with the BackgroundTransferRequest and its simple Uri UploadLocation property.

 
Others
 
- Windows Phone 8 : Performing Background File Transfers - Background Transfer Requests
- Microsoft Lync Server 2013 : Lync Server 2013 Mobility Technical Review - Push Notifications
- Microsoft Lync Server 2013 : Lync Server 2013 Mobility Technical Review - Sign-In
- Microsoft Lync Server 2013 : Lync Server 2013 Mobility Technical Review - Understanding the LyncDiscover Service, Understanding the Mobility Service
- Microsoft Lync Server 2013 : Mobile Clients Overview
- Active Directory 2008 Optimization and Reliability : Saving and Analyzing Data with Performance Logs and Alerts
- Active Directory 2008 Optimization and Reliability : Using Windows Server 2008 Performance Tools (part 3) - Managing Performance Monitor Properties
- Active Directory 2008 Optimization and Reliability : Using Windows Server 2008 Performance Tools (part 2) - Viewing Performance Information
- Active Directory 2008 Optimization and Reliability : Using Windows Server 2008 Performance Tools (part 1) - Deciding What to Monitor
- Active Directory 2008 Optimization and Reliability : Overview of Windows Server 2008 Performance Monitoring
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us