Red Gate forums :: View topic - Silverlight - runtime error after obfuscation
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SmartAssembly 5
SmartAssembly 5 forum

Silverlight - runtime error after obfuscation

Search in SmartAssembly 5 forum
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.
Jump to:  
Author Message
farreachchris



Joined: 19 Jan 2011
Posts: 6

PostPosted: Wed Jan 19, 2011 8:46 pm    Post subject: Silverlight - runtime error after obfuscation Reply with quote

I am encountering and error whenever I obfuscate a Silverlight project with SmartAssembly 5.5. The obfuscation process completes successfully, but when I try to view my Silverlight application I receive the following exception:

An unhandled exception('Unhandled Error in Silverlight Application Code: 4004
Category: ManagedRuntimeError
Message: System.Windows.Markup.XamlParseException: Failed to assign to propery 'System.Windows.Controls.Primitives.ButtonBase.Click'.

The weird thing is that I was that this was working about a week ago.

My project contains Telerik DLLs and I am using WCF RIA Services here.

Thanks for your help!
Chris
Back to top
View user's profile Send private message
Chris.Allen



Joined: 12 Mar 2009
Posts: 547

PostPosted: Fri Jan 21, 2011 1:12 pm    Post subject: Reply with quote

This is some general advice about using {smartassembly} with Silverlight.

You need to integrate directly the protection in the build process, so you dont have to re-create the .XAP: The DLL stored by Visual Studio in the .XAP will already be obfuscated.

When creating your project with the GUI mode of {smartassembly}, the main assembly must be the assembly in the \obj\Release folder, and NOT the one in \bin\Release.
The destination assembly can be anywhere as its just for the testing; we will overwrite the assembly in the batch mode.

When the project is saved (e.g. in c:\temp\TestSilverlight.{sa}proj), you can either process it through the GUI or from the prompt using {smartassembly}.com, the command line version of {smartassembly}.

Then, you can modify the .csproj (or .vbproj) file in order to directly add the protection in the build process.

Basically, it:
* Adds a reference to the assembly in the GAC (The "UsingTask" node)
* Adds a flag in the "BeforeBuild" event to ensure that the Assembly is obfuscated only in Release mode (and not each time an assembly is compiled, which is done very often in the background in VS IDE)
* Runs {smartassembly} in the "AfterCompile" to build the project and overwrite the assembly generated by Visual Studio with the one protected by {smartassembly}.

If you run the project in Debug mode, nothing happens.
If you run the project in Release mode, it will automatically obfuscated the assembly.

If you need help on the edition your C# project, you can have a look here: (Its about ClickOnce but its very similar) http://www.smartassembly.com/download/HowToUseClickOnce.pdf

You will need to do it for each DLL in your XAP which you want to protect.

You also need to be aware that, as the format for baml files is not public, {smartassembly} cant re-create a new baml containing an obfuscated xaml file and thus need to keep the xaml files unchanged.
To ensure that your xaml file will still work with the obfuscated assembly, {smartassembly} will exclude some members from the obfuscation/pruning but, in some cases, you may need to manually exclude other members called by Reflection from the xaml files.

This can be done in {smartassembly} project or by using custom attributes directly in your code:
http://www.smartassembly.com/download/HowToUseCustomAttributes.pdf

The protected assembly must be signed with the same Strong Name Key (SNK) in {smartassembly} than in Visual Studio, if any.

If you want to merge a dependency using WPF with your main assembly, you need to be aware that, if the assembly-qualified name of a type in use in an xaml file, the WPF engine may no longer be able to bind the types from the xaml files with the types in your code (As, obviously, the assembly-qualified name of the types will change after the merging).
Back to top
View user's profile Send private message
farreachchris



Joined: 19 Jan 2011
Posts: 6

PostPosted: Mon Jan 24, 2011 6:45 pm    Post subject: Reply with quote

I found the following support article, which I used previously to obfuscate my Silverlight projects. This was working before!!!

http://www.red-gate.com/supportcenter/Content?p=SmartAssembly&c=SmartAssembly/help/5.5/sa_silverlight.htm&toc=SmartAssembly/help/5.5/toc1210451.htm

Based on the article I should be able to select the XAP file to obfuscate. Are you suggesting that I select the DLL instead?

Thanks
Back to top
View user's profile Send private message
farreachchris



Joined: 19 Jan 2011
Posts: 6

PostPosted: Mon Jan 24, 2011 8:48 pm    Post subject: Reply with quote

Update - I tried creating some different projects to see if the obfuscation process would work. Here is what I tried and the outcome. Each time I selected the XAP file to obfuscate.

1. Silverlight app; no WCF RIA Services; no Telerik controls. GOOD
2. Silverlight app; no WCF RIA Services; Telerik controls. GOOD
3. Silverlight business app with WCF RIA Services; no Telerik controls. GOOD
4. Silverlight business app with WCF RIA Services; Telerik controls. GOOD
5. Existing Silverlight business app with WCF RIA Services Library; Telerik controls. FAILED - Several project all failed.

So, the process works, but it seems as if my existing Biz apps are contain something that causes the error.

Hope this helps. Thanks!
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6582

PostPosted: Tue Jan 25, 2011 2:00 pm    Post subject: Reply with quote

Hi,

What I find is that Microsoft Silverlight will create some private methods and then reflect them, which is pretty much "against the rules" of CLS-Compliant assemblies. When this happens, we have to debug the assembly output and exclude some private member from obfuscation and/or pruning.

I suspect the remark about processing the dll from the OBJ folder was applicable to v4 because Silverlight support was enhanced in v5 to unzip the XAP, process the DLL inside, and zip it all back up again. It certainly works on my computer®.
Back to top
View user's profile Send private message
farreachchris



Joined: 19 Jan 2011
Posts: 6

PostPosted: Tue Jan 25, 2011 2:23 pm    Post subject: Reply with quote

Brian,

Every Silverlight project which uses WCF RIA Services is building but throwing the same error at runtime. Projects without WCF RIA Services are fine.

Can I provide an example for you to try? I would be happy to zip up a project and send it over to you for debugging.

Thanks
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6582

PostPosted: Tue Jan 25, 2011 4:25 pm    Post subject: Reply with quote

Hi,

I would send the example in to support at red-gate.com and we can have a look. Simply Silverlight+RIA is not a problem in itself.
Back to top
View user's profile Send private message
Chris.Allen



Joined: 12 Mar 2009
Posts: 547

PostPosted: Fri Jan 28, 2011 2:54 pm    Post subject: Reply with quote

Would you be able to send us the project?

support@red-gate.com (please strip all exes and dlls out before sending). Please quote F43562 in the subject.
Back to top
View user's profile Send private message
farreachchris



Joined: 19 Jan 2011
Posts: 6

PostPosted: Fri Jan 28, 2011 3:01 pm    Post subject: Reply with quote

I will send the project today. thanks!
Back to top
View user's profile Send private message
farreachchris



Joined: 19 Jan 2011
Posts: 6

PostPosted: Mon Jan 31, 2011 11:15 pm    Post subject: Issue resolved Reply with quote

Based on feedback from Chris on the support team I was able to resolve this issue by doing the following:

- Pruning: When I had this turned on I received the runtime error mentioned in my original forum post. Turning this off resolved the issue.

- Exclusions on Obfuscation: I excluded some of the classes to make this work.

Thank you!
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies. All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group