Introduction
In this post, I will briefly review integrating the HSS Interlink UploadFileDialog feature with Microsoft’s application platform, LightSwitch for Visual Studio.
I must admit this is my first time using LightSwitch, so if anyone has a better idea, please share but after an hour or so of playing around with a fresh install of LightSwitch I was able to get HSS Interlink up and running.
For basic HSS Interlink implementation, you should read this first: HSS Interlink – Quickstart.
For the original blog post, you can read it here.
Background
The first hurdle was figuring out the Dispatcher processing in LightSwitch. Apparently LightSwitch button event handlers run inside a custom UI Dispatcher. So to create a new instance of the Upload dialog, it had to be created on the main Dispatcher. That was easy enough using the Deployment Dispatcher. But in order for the FileDialog to work, it has to be called from a user initiated event, which all user initiated events in LightSwitch run in this custom UI Dispatcher. So we call the Show
method from that dispatcher. I was not able to get the BrowseAndShow
method to work since the creation was being done on the main thread, so the only option is the Show
method.
On the server side, it was a little tricky figuring out how LightSwitch generated the xap package and where the Web App was running from. But after digging around in the solution folder, I discovered the ServerGenerated folder and in there is the source web config file. From there, I was able to integrate the required web config changes.
Note, I do not cover LightSwitch basics such as creating a button, etc., I assume you’re already familiar with LightSwitch and how to access the client and server projects and add/remove references.
Using the Code
Starting with the Client Project, do the following:
Note: This assumes you’re running as a Web application. If you’re running OOB, you will have to specify the absolute URI back to the web server.
- You have to add a reference to the HSS.Interlink.dll
- You have to add a reference to System.Windows.Controls.dll
- LightSwitch custom dispatcher hack:
UploadFileDialog d;
partial void UploadFile_Execute()
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
d = new UploadFileDialog();
d.AllowNewUpload = false;
d.MaxFileSizeKB = int.MaxValue;
d.AutoUpload = true;
d.AllowFileOverwrite = true;
d.Background = new System.Windows.Media.SolidColorBrush(Colors.Gray);
d.Closed += new EventHandler(d_Closed);
Microsoft.LightSwitch.Threading.Dispatchers.Current.BeginInvoke(() =>
{
d.Show();
});
});
}
void d_Closed(object sender, EventArgs e)
{
d.Closed -= new EventHandler(d_Closed);
d = null;
this.ShowMessageBox("Upload Dialog Closed.");
}
Then for the Server Project, do the following:
- Add a reference to the HSS.Interlink.Web DLL
- Implement your
UploadHandler
in the server project (I have included below)
- Find your web.config file. Mine was here (..\Application1\Application1\ServerGenerated) and add the appropriate entries. I’ve included examples below, but you will have to modify to reference your application.
Note: After you make changes to your web config and server, you will have to do a complete Compile/Rebuild of the Solution.
<add key="UploadHandler" value="LightSwitchApplication.UploadHandler, Application.Server"/>
<add verb="GET,POST" path="FileDownload.ashx" type="HSS.Interlink.Web.FileDownload, HSS.Interlink.Web" />
<add verb="GET,POST" path="FileUpload.ashx" type="HSS.Interlink.Web.FileUpload, HSS.Interlink.Web" />
namespace LightSwitchApplication
{
#region Using Directives
using System.IO;
using HSS.Interlink.Web;
#endregion
#region UploadHandler
public class UploadHandler : HSS.Interlink.Web.BaseUploadHandler
{
public static string FileStoreFolder = @"Interlink\Uploads";
public UploadHandler()
{
}
string GetFilePath()
{
return Path.Combine(this.GetFolder(FileStoreFolder), this.FileName);
}
#region BaseUploadHandler Members
public override bool CheckFileExists()
{
string file = GetFilePath();
return File.Exists(file);
}
public override Responses CreateNewFile()
{
#region Test Retry (see AppendToFile)
#endregion
string file = this.GetFilePath();
try
{
if (File.Exists(file))
File.Delete(file);
}
catch { }
File.Create(file).Close();
return HSS.Interlink.Web.Responses.Success;
}
public override Responses AppendToFile(byte[] buffer)
{
#region Test exception
#endregion
#region Test Retry (see CreateNewFile)
#endregion
string file = this.GetFilePath();
using (FileStream fs = File.Open(file, FileMode.Append))
fs.Write(buffer, 0, buffer.Length);
return HSS.Interlink.Web.Responses.Success;
}
public override void CancelUpload()
{
string file = this.GetFilePath();
try
{
if (File.Exists(file))
File.Delete(file);
}
catch { }
}
public override string UploadComplete()
{
string file = GetFilePath();
return "Hey we made it!";
}
public override bool IsAuthorized()
{
#region Test Authorization
#endregion
return true;
}
public override void OnError(System.Exception ex)
{
string file = this.GetFilePath();
try
{
if (File.Exists(file))
File.Delete(file);
}
catch { }
}
#endregion
}
#endregion
}
Build and run and it should work.
You can test this in an existing project or you can download the test solution at the top of this article.
Points of Interest
So learning how to manipulate the dispatchers in LightSwitch proved to be key to this exercise. But once that was resolved, everything worked as normal.