Silverlight uses the file generation time to force the client to load the latest xap File

Source: Internet
Author: User
Like most projects Program After delivery, we will modify the xap file based on the customer's requirements and send it to the customer. After testing, the customer will deploy the file in the production environment.

In this way, we often face a problem. The customer does not see any changes after updating the xap, even because the changes to the relevant files such as the WCF Service will cause exceptions in program execution. This is obvious because our xap file is cached. When the new xap file is replaced on the server, it is not downloaded from the server again. At this time, we certainly need to tell the customer how to clean up the cache and so on, and then encounter a computer blind (in most cases) is even more miserable. The customer complained that the project manager was dumb to eat Huanglian...

Is there any way to solve the problem mentioned above? Of course, listen to the decomposition.

Let's take a look at the code for checking in the Silverlight application in HTML.CodeExample

         <  Object  Data  = "Data: Application/x-silverlight-2 ,"  Type  = "Application/x-silverlight-2"  Width = "100%"  Height  = "100%"> <  Param  Name  = "Source"  Value  = "Clientbin/mefsample. xap"/> <  Param  Name  = "Onerror"  Value  = "Onsilverlighterror"/> <  Param  Name  = "Background" Value  = "White"/> <  Param  Name  = "Minruntimeversion"  Value  = "4.0.50826.0"/> <  Param  Name  = "Autoupgrade"  Value  = "True"/> <  A  Href  = "Http://go.microsoft.com/fwlink? Linkid = 149156 & V = 4.0.50826.0"  Style ="  Text-Decoration  : None "> <  IMG  SRC  = "Http://go.microsoft.com/fwlink? Linkid = 161376"  ALT  = "Get Microsoft Silverlight"  Style  ="  Border-style  : None "/> </  A  > </  Object  > 

Pay attention to the following line, and we will start from here.

 
<ParamName= "Source"Value= "Clientbin/mefsample. xap"/>

We know that to prevent the browser from automatically caching files, a common practice is to add a random string (such as a verification code refresh) after the file name to be loaded, such as guid and datetime. now. ticks.

Therefore, the idea here is to determine whether to re-load the xap package by judging the xap file generation time.

The code for the final view is as follows:

 <  Object  Data  = "Data: Application/x-silverlight-2 ,"  Type  = "Application/x-silverlight-2" Width  = "100%"  Height  = "100%">  @{          String Xapfile = "Clientbin/mefsample. xap" ; String Xappath = Httpcontext . Current. server. mappath ( "\\" ) + Xapfile; Datetime Xapcreateddate = system. Io. File . Getlastwritetime (xappath); xapfile + = "? V =" + Xapcreateddate; }      <  Param  Name  = "Source"  Value  ="  @  Xapfile "/> <  Param  Name  = "Onerror"  Value  = "Onsilverlighterror"/> <  Param  Name  = "Background" Value  = "White"/> <  Param  Name  = "Minruntimeversion"  Value  = "4.0.50826.0"/> <  Param  Name  = "Autoupgrade"  Value  = "True"/> <  A  Href  = "Http://go.microsoft.com/fwlink? Linkid = 149156 & V = 4.0.50826.0"  Style ="  Text-Decoration  : None "> <  IMG  SRC  = "Http://go.microsoft.com/fwlink? Linkid = 161376"  ALT  = "Get Microsoft Silverlight"  Style  ="  Border-style  : None "/> </  A  > </  Object  > 

Note: Here I use ASP. NET MVC (razor syntax) to host xap, which is also applicable to webform or webform view. Please change it on your own.

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.