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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.206.73