Enable statlight to support file downloads

Source: Internet
Author: User

Cause: When writing Silverlight functionality, You need to download some configuration files from the site, unit testing is OK when using Silverlight tooltik (http://silverlight.codeplex.com/), using statlight (http://statlight.codeplex.com /) an error occurs because statlight simulates a webserver, which does not support file download. Download statlightCodeIt has been improved.

Location: statlight. Core/webserver/responsefactory. CS

Modified code

 Using  System;
Using System. Globalization;
Using System. IO;
Using System. Threading;
Using Statlight. Core. configuration;
Using Statlight. Core. properties;
Using Statlight. Core. serialization;

Namespace Statlight. Core. Webserver
{
Public Class Responsefactory
{
Private Readonly Func < Byte [] > _ Xaptotestfactory;
Private Readonly Byte [] _ Hostxap;
Private Readonly String _ Serializedconfiguration;

Public Responsefactory (func < Byte [] > Xaptotestfactory, Byte [] Hostxap, clienttestrunconfiguration)
{
_ Xaptotestfactory = Xaptotestfactory;
_ Hostxap = Hostxap;
_ Serializedconfiguration = Clienttestrunconfiguration. serialize ();
}

Private Static Int _ Htmlpageinstanceid = 0 ;

Public Responsefile get ( String Localpath)
{
If (Isknown (localpath, statlightservicerestapi. crossdomain ))
Return New Responsefile {filedata = Resources. crossdomain. tobytearray (), contenttype = " Text/XML " };

If (Isknown (localpath, statlightservicerestapi. clientaccesspolicy ))
Return New Responsefile {filedata = Resources. clientaccesspolicy. tobytearray (), contenttype = " Text/XML " };

If (Isknown (localpath, statlightservicerestapi. gethtmltestpage ))
{
Return Gettesthtmlpage ();
}

If (Isknown (localpath, statlightservicerestapi. getxaptotest ))
Return New Responsefile {filedata = _ Xaptotestfactory (), contenttype = " Application/X-Silverlight-app " };

If (Isknown (localpath, statlightservicerestapi. gettestpagehostxap ))
Return New Responsefile {filedata = _ Hostxap, contenttype = " Application/X-Silverlight-app " };

If (Isknown (localpath, statlightservicerestapi. gettestrunconfiguration ))
Return New Responsefile {filedata = _ Serializedconfiguration. tobytearray (), contenttype = " Text/XML " };

If (Islocalfile (localpath ))
Return New Responsefile {filedata = File. readallbytes (localpath), contenttype = " Text " };

Throw New Notimplementedexception ();
}

[System. Diagnostics. codeanalysis. suppressmessage ( " Microsoft. Performance " , " Ca1822: markmembersasstatic " )]
Public Bool Isknownfile ( String Localpath)
{
If (Isknown (localpath, statlightservicerestapi. crossdomain ))
Return True ;

If (Isknown (localpath, statlightservicerestapi. clientaccesspolicy ))
Return True ;

If (Isknown (localpath, statlightservicerestapi. gethtmltestpage ))
Return True ;

If (Isknown (localpath, statlightservicerestapi. getxaptotest ))
Return True ;

If (Isknown (localpath, statlightservicerestapi. gettestpagehostxap ))
Return True ;

If (Isknown (localpath, statlightservicerestapi. gettestrunconfiguration ))
Return True ;

Return Islocalfile (localpath );
}


[System. Diagnostics. codeanalysis. suppressmessage ( " Microsoft. Design " , " Ca1024: usepropertieswhereappropriate " )]
Public Static Responsefile gettesthtmlpage ()
{
VaR page = Resources. testpage. Replace ( " BB86D193-AD39-494A-AEB7-58F948BA5D93 " , _ Htmlpageinstanceid. tostring (cultureinfo. invariantculture ));

Interlocked. increment ( Ref _ Htmlpageinstanceid );

Return New Responsefile {filedata = Page. tobytearray (), contenttype = " Text/html " };
}

Private Static Bool Isknown ( String Filea, String Fileb)
{
Return String . Equals (filea, fileb, stringcomparison. ordinalignorecase );
}

Public Static Void Reset ()
{
_ Htmlpageinstanceid = 0 ;
}

/// <Summary>
/// Whether the local file exists
/// </Summary>
/// <Param name = "path"> </param>
/// <Returns> </returns>
Public Static Bool Islocalfile ( String Path)
{
Return File. exists (PATH );
}
}
}

Note:

1. Pay attention to the project startup directory when running the test.

2. The Silverlight end can only use relative paths for access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.