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.