Silverlight - runtime error after obfuscation

farreachchrisfarreachchris Posts: 7
edited January 31, 2011 5:15PM in SmartAssembly 5
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!


  • 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) ... ckOnce.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: ... ibutes.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).
  • I found the following support article, which I used previously to obfuscate my Silverlight projects. This was working before!!! ... 210451.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?

  • 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!
  • Brian DonahueBrian Donahue Posts: 6,590 New member

    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®.
  • 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.

  • Brian DonahueBrian Donahue Posts: 6,590 New member

    I would send the example in to support at and we can have a look. Simply Silverlight+RIA is not a problem in itself.
  • Would you be able to send us the project?

    [email protected] (please strip all exes and dlls out before sending). Please quote F43562 in the subject.
  • I will send the project today. thanks!
  • 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!
Sign In or Register to comment.