R
Roger Martin
I have a web site under .NET 2.0 that renders videos using the Silverlight
media player. The <asp:MediaPlayer> control only works on .NET 3.5, but I
managed to get things working under .NET 2.0 by working with the javascript
rather than the server control.
The web page looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="video5.aspx.cs"
Inherits="GalleryServerPro.Web.video5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html id="html" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/script/Silverlight.js" />
<asp:ScriptReference Path="~/script/SilverlightControl.js" />
<asp:ScriptReference Path="~/script/SilverlightMedia.js" />
</Scripts>
</asp:ScriptManager>
<asp
laceHolder ID="phContent" runat="server" />
</form>
</body>
</html>
The code behind:
using System;
using System.Web.UI;
namespace GalleryServerPro.Web
{
public partial class video5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
phContent.Controls.Add(new LiteralControl("<div id=\"mp1p\" />"));
// Specify direct path to media file. This works.
string script = "Sys.UI.Silverlight.Control.createObject('mp1p', '<object
type=\"application/x-silverlight\" data=\"data:application/x-silverlight,\"
id=\"mp1\" style=\"height:400px;width:500px;\"><param name=\"Windowless\"
value=\"True\" /><a
href=\"http://go2.microsoft.com/fwlink/?LinkID=114576&v=1.0\"><img
src=\"http://go2.microsoft.com/fwlink/?LinkID=108181\" alt=\"Get Microsoft
Silverlight\" style=\"border-width:0;\"
/></a></object>');Sys.Application.initialize();Sys.Application.add_init(function()
{ $create(Sys.UI.Silverlight.MediaPlayer,
{\"mediaSource\":\"mediaobjects/3StrikesChipmunk_56.wmv\",\"scaleMode\":1,\"source\":\"skins/mediaplayer/Professional.xaml\"
}, null, null, $get(\"mp1p\")); });";
// Specify path to media file via ASHX handler. This does not work.
string scriptWithHandler =
"Sys.UI.Silverlight.Control.createObject('mp1p', '<object
type=\"application/x-silverlight\" data=\"data:application/x-silverlight,\"
id=\"mp1\" style=\"height:400px;width:500px;\"><param name=\"Windowless\"
value=\"True\" /><a
href=\"http://go2.microsoft.com/fwlink/?LinkID=114576&v=1.0\"><img
src=\"http://go2.microsoft.com/fwlink/?LinkID=108181\" alt=\"Get Microsoft
Silverlight\" style=\"border-width:0;\"
/></a></object>');Sys.Application.initialize();Sys.Application.add_init(function()
{ $create(Sys.UI.Silverlight.MediaPlayer,
{\"mediaSource\":\"/dev/gs/handler/getmediaobject.ashx?OG/v3eAwpo3M6STSva1cvgMqg4K8r1clwFvPuAJQ4YnG7Jcg/xqOru3otGjczWUCt1jB0uimLyDAnGyeGGKVqtng8JweBY9ZkrDcM3bfDbcP4tGfdq9QHP41oV6k5UBDncTxlr3iZTbyiVeDTAkfp9Ts7bFAHk+n\",\"scaleMode\":1,\"source\":\"skins/mediaplayer/Professional.xaml\"
}, null, null, $get(\"mp1p\")); });";
ScriptManager.RegisterStartupScript(this, typeof(GspPage),
"startupScript", scriptWithHandler, true);
}
}
}
The AXHX handler (I include only the relevant method as the rest of the code
in the handler deals with security and would distract):
private void ProcessMediaObject()
{
// Send the specified file to the client.
FileStream fileStream = null;
try
{
IMimeType mimeType = MimeType.LoadInstanceByFilePath(this._filename);
this._context.Response.Clear();
this._context.Response.ContentType = mimeType.FullType;
this._context.Response.Buffer = false;
HttpCachePolicy cachePolicy = this._context.Response.Cache;
cachePolicy.SetExpires(System.DateTime.Now.AddSeconds(2592000)); // 30 days
cachePolicy.SetCacheability(HttpCacheability.Public);
cachePolicy.SetValidUntilExpires(true);
int bufferSize =
GalleryServerPro.Web.WebsiteController.GetGalleryServerProConfigSection().Core.MediaObjectDownloadBufferSize;
byte[] buffer = new byte[bufferSize];
long byteCount;
fileStream = File.OpenRead(this._filepath);
while ((byteCount = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
if (this._context.Response.IsClientConnected)
{
this._context.Response.OutputStream.Write(buffer, 0, buffer.Length);
this._context.Response.Flush();
}
else
{
return;
}
}
}
catch (Exception ex)
{
GalleryServerPro.ErrorHandler.AppErrorHandler.RecordErrorInfo(ex);
}
finally
{
if (fileStream != null)
fileStream.Close();
this._context.Response.End();
}
}
The ContentType of the Response is being set to "video/x-ms-wmv".
As long as I specify the path to the video file directly, everything works.
But when I try to use the handler to send the video to the browser, all I get
is an empty media player - I can see the play/pause/other controls but there
is no video.
The really odd part is that I *can* use the handler on my dev PC - it only
fails when I publish the code to the hosting company (Discount ASP.NET). It
happens on both IIS 6 and IIS 7.
I know you are not in the business of providing support for hosting
companies, so my question is: What could possibly be different on the server
that would cause this to fail? I verified that XAML is a valid MIME type.
media player. The <asp:MediaPlayer> control only works on .NET 3.5, but I
managed to get things working under .NET 2.0 by working with the javascript
rather than the server control.
The web page looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="video5.aspx.cs"
Inherits="GalleryServerPro.Web.video5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html id="html" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/script/Silverlight.js" />
<asp:ScriptReference Path="~/script/SilverlightControl.js" />
<asp:ScriptReference Path="~/script/SilverlightMedia.js" />
</Scripts>
</asp:ScriptManager>
<asp
</form>
</body>
</html>
The code behind:
using System;
using System.Web.UI;
namespace GalleryServerPro.Web
{
public partial class video5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
phContent.Controls.Add(new LiteralControl("<div id=\"mp1p\" />"));
// Specify direct path to media file. This works.
string script = "Sys.UI.Silverlight.Control.createObject('mp1p', '<object
type=\"application/x-silverlight\" data=\"data:application/x-silverlight,\"
id=\"mp1\" style=\"height:400px;width:500px;\"><param name=\"Windowless\"
value=\"True\" /><a
href=\"http://go2.microsoft.com/fwlink/?LinkID=114576&v=1.0\"><img
src=\"http://go2.microsoft.com/fwlink/?LinkID=108181\" alt=\"Get Microsoft
Silverlight\" style=\"border-width:0;\"
/></a></object>');Sys.Application.initialize();Sys.Application.add_init(function()
{ $create(Sys.UI.Silverlight.MediaPlayer,
{\"mediaSource\":\"mediaobjects/3StrikesChipmunk_56.wmv\",\"scaleMode\":1,\"source\":\"skins/mediaplayer/Professional.xaml\"
}, null, null, $get(\"mp1p\")); });";
// Specify path to media file via ASHX handler. This does not work.
string scriptWithHandler =
"Sys.UI.Silverlight.Control.createObject('mp1p', '<object
type=\"application/x-silverlight\" data=\"data:application/x-silverlight,\"
id=\"mp1\" style=\"height:400px;width:500px;\"><param name=\"Windowless\"
value=\"True\" /><a
href=\"http://go2.microsoft.com/fwlink/?LinkID=114576&v=1.0\"><img
src=\"http://go2.microsoft.com/fwlink/?LinkID=108181\" alt=\"Get Microsoft
Silverlight\" style=\"border-width:0;\"
/></a></object>');Sys.Application.initialize();Sys.Application.add_init(function()
{ $create(Sys.UI.Silverlight.MediaPlayer,
{\"mediaSource\":\"/dev/gs/handler/getmediaobject.ashx?OG/v3eAwpo3M6STSva1cvgMqg4K8r1clwFvPuAJQ4YnG7Jcg/xqOru3otGjczWUCt1jB0uimLyDAnGyeGGKVqtng8JweBY9ZkrDcM3bfDbcP4tGfdq9QHP41oV6k5UBDncTxlr3iZTbyiVeDTAkfp9Ts7bFAHk+n\",\"scaleMode\":1,\"source\":\"skins/mediaplayer/Professional.xaml\"
}, null, null, $get(\"mp1p\")); });";
ScriptManager.RegisterStartupScript(this, typeof(GspPage),
"startupScript", scriptWithHandler, true);
}
}
}
The AXHX handler (I include only the relevant method as the rest of the code
in the handler deals with security and would distract):
private void ProcessMediaObject()
{
// Send the specified file to the client.
FileStream fileStream = null;
try
{
IMimeType mimeType = MimeType.LoadInstanceByFilePath(this._filename);
this._context.Response.Clear();
this._context.Response.ContentType = mimeType.FullType;
this._context.Response.Buffer = false;
HttpCachePolicy cachePolicy = this._context.Response.Cache;
cachePolicy.SetExpires(System.DateTime.Now.AddSeconds(2592000)); // 30 days
cachePolicy.SetCacheability(HttpCacheability.Public);
cachePolicy.SetValidUntilExpires(true);
int bufferSize =
GalleryServerPro.Web.WebsiteController.GetGalleryServerProConfigSection().Core.MediaObjectDownloadBufferSize;
byte[] buffer = new byte[bufferSize];
long byteCount;
fileStream = File.OpenRead(this._filepath);
while ((byteCount = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
if (this._context.Response.IsClientConnected)
{
this._context.Response.OutputStream.Write(buffer, 0, buffer.Length);
this._context.Response.Flush();
}
else
{
return;
}
}
}
catch (Exception ex)
{
GalleryServerPro.ErrorHandler.AppErrorHandler.RecordErrorInfo(ex);
}
finally
{
if (fileStream != null)
fileStream.Close();
this._context.Response.End();
}
}
The ContentType of the Response is being set to "video/x-ms-wmv".
As long as I specify the path to the video file directly, everything works.
But when I try to use the handler to send the video to the browser, all I get
is an empty media player - I can see the play/pause/other controls but there
is no video.
The really odd part is that I *can* use the handler on my dev PC - it only
fails when I publish the code to the hosting company (Discount ASP.NET). It
happens on both IIS 6 and IIS 7.
I know you are not in the business of providing support for hosting
companies, so my question is: What could possibly be different on the server
that would cause this to fail? I verified that XAML is a valid MIME type.