Migrating to WebClientPrint 4.0 for ASP.NET MVC & WebForms
Product WebClientPrint for ASP.NET Published 12/22/2017 Updated 03/07/2018 Author Neodynamic
Overview
The brand new WebClientPrint 4.0 for ASP.NET is compatible with Version 3.0 but a few modifications must be done to get v4.0 working on an existing project using v3.0. Please use this article as a general guide to upgrading your server-side code to v4.0
In this article:
- WebClientPrint Assembly Update
- WebClientPrint Processor (WCPP) Update
- WebClientPrintAPI or WebClientPrintAPIController Update
• WebClientPrint Assembly Update
In your project where old v3 is being used and you want to upgrade to v4, then UPDATE the WebClientPrint dll assembly to latest v4. Do it by downloading the package installer from our website's Download section or from Nuget
• WebClientPrint Processor (WCPP) Update
Any project using WebClientPrint assembly v4 will require that ANY Client that wants to print from them, to install WebClientPrint Processor (WCPP) v4 which can be downloaded and installed from our website's Download section
• WebClientPrintAPI or WebClientPrintAPIController Update
In v4.0, WebClientPrint brings new features that require you to update the code you used to have to v3.0 in the WebClientPrintAPI.ashx handler (for WebForms) or in the WebClientPrintAPIController.cs/vb (for MVC)
C#
public class WebClientPrintAPIController : Controller
{
[AllowAnonymous]
public void ProcessRequest()
{
//get session ID
string sessionID = (HttpContext.Request["sid"] != null ? HttpContext.Request["sid"] : null);
//get Query String
string queryString = HttpContext.Request.Url.Query;
try
{
//Determine and get the Type of Request
RequestType prType = WebClientPrint.GetProcessRequestType(queryString);
if (prType == RequestType.GenPrintScript ||
prType == RequestType.GenWcppDetectScript)
{
//Let WebClientPrint to generate the requested script
byte[] script = WebClientPrint.GenerateScript(Url.Action("ProcessRequest", "WebClientPrintAPI", null, HttpContext.Request.Url.Scheme), queryString);
HttpContext.Response.ContentType = "text/javascript";
HttpContext.Response.BinaryWrite(script);
HttpContext.Response.End();
}
else if (prType == RequestType.ClientSetWcppVersion)
{
//This request is a ping from the WCPP utility
//so store the session ID indicating it has the WCPP installed
//also store the WCPP Version if available
string wcppVersion = HttpContext.Request["wcppVer"];
if (string.IsNullOrEmpty(wcppVersion))
wcppVersion = "1.0.0.0";
HttpContext.Application.Set(sessionID + "wcppInstalled", wcppVersion);
}
else if (prType == RequestType.ClientSetInstalledPrinters)
{
//WCPP Utility is sending the installed printers at client side
//so store this info with the specified session ID
string printers = HttpContext.Request["printers"];
if (string.IsNullOrEmpty(printers) == false)
printers = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printers));
HttpContext.Application.Set(sessionID + "printers", printers);
}
else if (prType == RequestType.ClientSetInstalledPrintersInfo)
{
//WCPP Utility is sending the client installed printers with detailed info
//so store this info with the specified session ID
//Printers Info is in JSON format
string printersInfo = HttpContext.Request.Form["printersInfoContent"];
if (string.IsNullOrEmpty(printersInfo) == false)
printersInfo = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printersInfo));
HttpContext.Application.Set(sessionID + "printersInfo", printersInfo);
}
else if (prType == RequestType.ClientGetWcppVersion)
{
//return the WCPP version for the specified sid if any
bool sidWcppVersion = (HttpContext.Application.Get(sessionID + "wcppInstalled") != null);
HttpContext.Response.ContentType = "text/plain";
HttpContext.Response.Write((sidWcppVersion ? HttpContext.Application.Get(sessionID + "wcppInstalled") : ""));
HttpContext.Response.End();
}
else if (prType == RequestType.ClientGetInstalledPrinters)
{
//return the installed printers for the specified sid if any
bool sidHasPrinters = (HttpContext.Application.Get(sessionID + "printers") != null);
HttpContext.Response.ContentType = "text/plain";
HttpContext.Response.Write((sidHasPrinters ? HttpContext.Application.Get(sessionID + "printers") : ""));
HttpContext.Response.End();
}
else if (prType == RequestType.ClientGetInstalledPrintersInfo)
{
//return the installed printers with detailed info for the specified Session ID (sid) if any
bool sidHasPrinters = (HttpContext.Application[sessionID + "printersInfo"] != null);
HttpContext.Response.ContentType = "text/plain";
HttpContext.Response.Write(sidHasPrinters ? HttpContext.Application[sessionID + "printersInfo"] : "");
}
}
catch (Exception ex)
{
HttpContext.Response.StatusCode = 500;
HttpContext.Response.ContentType = "text/plain";
HttpContext.Response.Write(ex.Message + " - StackTrace: " + ex.StackTrace);
HttpContext.Response.End();
}
}
}
VB
Public Class WebClientPrintAPIController
Inherits Controller
' GET: WebClientPrintAPI
Function Index() As ActionResult
Return View()
End Function
'*********************************
' IMPORTANT NOTE
' In this sample we store users related stuff (like
' the list of printers and whether they have the WCPP
' client utility installed) in the Application cache
' object part of ASP.NET BUT you can change it to
' another different storage (like a DB or file server)!
' which will be required in Load Balacing scenarios
'*********************************
<AllowAnonymous>
Public Sub ProcessRequest()
'get session ID
Dim sessionID As String = (If(HttpContext.Request("sid") IsNot Nothing, HttpContext.Request("sid"), Nothing))
'get Query String
Dim queryString As String = HttpContext.Request.Url.Query
Try
'Determine and get the Type of Request
Dim prType As RequestType = WebClientPrint.GetProcessRequestType(queryString)
If prType = RequestType.GenPrintScript OrElse prType = RequestType.GenWcppDetectScript Then
'Let WebClientPrint to generate the requested script
Dim script As Byte() = WebClientPrint.GenerateScript(Url.Action("ProcessRequest", "WebClientPrintAPI", Nothing, HttpContext.Request.Url.Scheme), queryString)
HttpContext.Response.ContentType = "text/javascript"
HttpContext.Response.BinaryWrite(script)
HttpContext.Response.End()
ElseIf prType = RequestType.ClientSetWcppVersion Then
'This request is a ping from the WCPP utility
'so store the session ID indicating it has the WCPP installed
'also store the WCPP Version if available
Dim wcppVersion As String = HttpContext.Request("wcppVer")
If String.IsNullOrEmpty(wcppVersion) Then
wcppVersion = "1.0.0.0"
End If
HttpContext.Application.Set(sessionID & "wcppInstalled", wcppVersion)
ElseIf prType = RequestType.ClientSetInstalledPrinters Then
'WCPP Utility is sending the installed printers at client side
'so store this info with the specified session ID
Dim printers As String = HttpContext.Request("printers")
If String.IsNullOrEmpty(printers) = False Then
printers = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printers))
End If
HttpContext.Application.Set(sessionID & "printers", printers)
ElseIf prType = RequestType.ClientSetInstalledPrintersInfo Then
'WCPP Utility is sending the installed printers at client side
'so store this info with the specified session ID
'Printers Info is in JSON format
Dim printersInfo As String = HttpContext.Request.Form("printersInfoContent")
If Not String.IsNullOrEmpty(printersInfo) Then
printersInfo = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printersInfo))
End If
HttpContext.Application.Set(sessionID & "printersInfo", printersInfo)
ElseIf prType = RequestType.ClientGetWcppVersion Then
'return the WCPP version for the specified sid if any
Dim sidWcppVersion As Boolean = (HttpContext.Application(sessionID & "wcppInstalled") IsNot Nothing)
HttpContext.Response.ContentType = "text/plain"
If (sidWcppVersion) Then
HttpContext.Response.Write(HttpContext.Application(sessionID & "wcppInstalled").ToString())
End If
HttpContext.Response.End()
ElseIf prType = RequestType.ClientGetInstalledPrinters Then
'return the installed printers for the specified sid if any
Dim sidHasPrinters As Boolean = (HttpContext.Application(sessionID & "printers") IsNot Nothing)
HttpContext.Response.ContentType = "text/plain"
If (sidHasPrinters) Then
HttpContext.Response.Write(HttpContext.Application(sessionID & "printers").ToString())
End If
HttpContext.Response.End()
ElseIf prType = RequestType.ClientGetInstalledPrintersInfo Then
'return the installed printers with detailed info for the specified Session ID (sid) if any
Dim sidHasPrinters As Boolean = (HttpContext.Application(sessionID & "printersInfo") IsNot Nothing)
HttpContext.Response.ContentType = "text/plain"
If (sidHasPrinters) Then
HttpContext.Response.Write(HttpContext.Application(sessionID & "printersInfo").ToString())
End If
HttpContext.Response.End()
End If
Catch ex As Exception
HttpContext.Response.StatusCode = 500
HttpContext.Response.ContentType = "text/plain"
HttpContext.Response.Write(ex.Message + " - StackTrace: " + ex.StackTrace)
HttpContext.Response.End()
End Try
End Sub
End Class
C#
using System;
using System.Web;
using System.IO;
using Neodynamic.SDK.Web;
public class WebClientPrintAPI : IHttpHandler {
//*********************************
// IMPORTANT NOTE
// In this sample we store users related stuff (like
// the list of printers and whether they have the WCPP
// client utility installed) in the Application cache
// object part of ASP.NET BUT you can change it to
// another different storage (like a DB or file server)!
// which will be required in Load Balacing scenarios
//*********************************
public void ProcessRequest (HttpContext context) {
//get session ID
string sessionID = (context.Request["sid"] != null) ? context.Request["sid"].ToString() : null;
//get Query String
string queryString = context.Request.Url.Query;
try
{
//Determine and get the Type of Request
RequestType prType = WebClientPrint.GetProcessRequestType(queryString);
if (prType == RequestType.GenPrintScript ||
prType == RequestType.GenWcppDetectScript)
{
//Let WebClientPrint to generate the requested script
byte[] script = WebClientPrint.GenerateScript(context.Request.Url.AbsoluteUri.Replace(queryString, ""), queryString);
context.Response.ContentType = "text/javascript";
context.Response.BinaryWrite(script);
}
else if (prType == RequestType.ClientSetWcppVersion)
{
//This request is a ping from the WCPP utility
//so store the session ID indicating this user has the WCPP installed
//also store the WCPP Version if available
string wcppVersion = context.Request["wcppVer"];
if (string.IsNullOrEmpty(wcppVersion))
wcppVersion = "1.0.0.0";
context.Application.Set(sessionID + "wcppInstalled", wcppVersion);
}
else if (prType == RequestType.ClientSetInstalledPrinters)
{
//WCPP Utility is sending the installed printers at client side
//so store this info with the specified session ID
string printers = context.Request["printers"];
if (string.IsNullOrEmpty(printers) == false)
printers = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printers));
context.Application.Set(sessionID + "printers", printers);
}
else if (prType == RequestType.ClientSetInstalledPrintersInfo)
{
//WCPP Utility is sending the client installed printers with detailed info
//so store this info with the specified session ID
//Printers Info is in JSON format
string printersInfo = context.Request.Form["printersInfoContent"];
if (string.IsNullOrEmpty(printersInfo) == false)
printersInfo = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printersInfo));
context.Application.Set(sessionID + "printersInfo", printersInfo);
}
else if (prType == RequestType.ClientGetWcppVersion)
{
//return the WCPP version for the specified Session ID (sid) if any
bool sidWcppVersion = (context.Application[sessionID + "wcppInstalled"] != null);
context.Response.ContentType = "text/plain";
context.Response.Write(sidWcppVersion ? context.Application[sessionID + "wcppInstalled"] : "");
}
else if (prType == RequestType.ClientGetInstalledPrinters)
{
//return the installed printers for the specified Session ID (sid) if any
bool sidHasPrinters = (context.Application[sessionID + "printers"] != null);
context.Response.ContentType = "text/plain";
context.Response.Write(sidHasPrinters ? context.Application[sessionID + "printers"] : "");
}
else if (prType == RequestType.ClientGetInstalledPrintersInfo)
{
//return the installed printers with detailed info for the specified Session ID (sid) if any
bool sidHasPrinters = (context.Application[sessionID + "printersInfo"] != null);
context.Response.ContentType = "text/plain";
context.Response.Write(sidHasPrinters ? context.Application[sessionID + "printersInfo"] : "");
}
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.ContentType = "text/plain";
context.Response.Write(ex.Message + " - " + ex.StackTrace);
}
}
public bool IsReusable {
get {
return false;
}
}
}
VB
Imports System
Imports System.Web
Imports Neodynamic.SDK.Web
Public Class WebClientPrintAPI : Implements IHttpHandler
'*********************************
' IMPORTANT NOTE
' In this sample we store users related stuff (like
' the list of printers and whether they have the WCPP
' client utility installed) in the Application cache
' object part of ASP.NET BUT you can change it to
' another different storage (like a DB or file server)!
' which will be required in Load Balacing scenarios
'*********************************
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
'get session ID
Dim sessionID As String = ""
If (context.Request("sid") IsNot Nothing) Then
sessionID = context.Request("sid")
End If
'get Query String
Dim queryString As String = context.Request.Url.Query
Try
'Determine and get the Type of Request
Dim prType As RequestType = WebClientPrint.GetProcessRequestType(queryString)
If prType = RequestType.GenPrintScript OrElse prType = RequestType.GenWcppDetectScript Then
'Let WebClientPrint to generate the requested script
Dim script As Byte() = WebClientPrint.GenerateScript(context.Request.Url.AbsoluteUri.Replace(queryString, ""), queryString)
context.Response.ContentType = "text/javascript"
context.Response.BinaryWrite(script)
ElseIf prType = RequestType.ClientSetWcppVersion Then
'This request is a ping from the WCPP utility
'so store the session ID indicating this user has the WCPP installed
'also store the WCPP Version if available
Dim wcppVersion As String = context.Request("wcppVer")
If String.IsNullOrEmpty(wcppVersion) Then
wcppVersion = "1.0.0.0"
End If
context.Application.Set(sessionID & "wcppInstalled", wcppVersion)
ElseIf prType = RequestType.ClientSetInstalledPrinters Then
'WCPP Utility is sending the installed printers at client side
'so store this info with the specified session ID
Dim printers As String = context.Request("printers")
If Not String.IsNullOrEmpty(printers) Then
printers = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printers))
End If
context.Application.Set(sessionID & "printers", printers)
ElseIf prType = RequestType.ClientSetInstalledPrintersInfo Then
'WCPP Utility is sending the installed printers at client side
'so store this info with the specified session ID
'Printers Info is in JSON format
Dim printersInfo As String = context.Request.Form("printersInfoContent")
If Not String.IsNullOrEmpty(printersInfo) Then
printersInfo = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printersInfo))
End If
context.Application.Set(sessionID & "printersInfo", printersInfo)
ElseIf prType = RequestType.ClientGetWcppVersion Then
'return the WCPP version for the specified Session ID (sid) if any
Dim sidWcppVersion As Boolean = (context.Application(sessionID & "wcppInstalled") IsNot Nothing)
context.Response.ContentType = "text/plain"
If (sidWcppVersion) Then
context.Response.Write(context.Application(sessionID & "wcppInstalled").ToString())
End If
ElseIf prType = RequestType.ClientGetInstalledPrinters Then
'return the installed printers for the specified Session ID (sid) if any
Dim sidHasPrinters As Boolean = (context.Application(sessionID & "printers") IsNot Nothing)
context.Response.ContentType = "text/plain"
If (sidHasPrinters) Then
context.Response.Write(context.Application(sessionID & "printers").ToString())
End If
ElseIf prType = RequestType.ClientGetInstalledPrintersInfo Then
'return the installed printers with detailed info for the specified Session ID (sid) if any
Dim sidHasPrinters As Boolean = (context.Application(sessionID & "printersInfo") IsNot Nothing)
context.Response.ContentType = "text/plain"
If (sidHasPrinters) Then
context.Response.Write(context.Application(sessionID & "printersInfo").ToString())
End If
End If
Catch ex As Exception
context.Response.StatusCode = 500
context.Response.ContentType = "text/plain"
context.Response.Write(ex.Message + " - " + ex.StackTrace)
End Try
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
That's all. Just be sure that your project is now referencing new v4.0 of WebClientPrint assembly.
As always, do not hesitate to contact our dev team at http://neodynamic.com/support