Accessing embedded resources from MEF loaded XAPs - silverlight

Simple question.
So MEF doesn't support importing or exporting loose files (such as xml files) etc.
However, it should at least support embedded resources right?
I currently have a silverlight application that loads xaps dynamically. These dynamically loaded xaps each have an xml file attached as an embedded resource accesible via an instance method that looks something like this...
public XDocument MenuStructure
{
get
{
return XDocument.Load("myFile.xml");
}
}
However, this property fails after import with a message saying "Cannot find file 'myFile.xml' in the application xap package."
I'm not sure whether the problem is how I'm accessing the file now that it's BuildAction is set to EmbeddedResource or not.
Any ideas?
Thanks

Ok, according to http://msdn.microsoft.com/en-us/library/ms596994(VS.95).aspx I was supposed to use Application.GetResourceStream. Everything works great now.

You are correct that MEF does not support loading resources from secondary downloaded XAP's. You can however do embedded resources (embedded in the assembly not the XAP) but the way you are accessing it will not pull the file from the embedded resources.
For BuildAction EmbeddedResource you will need to get the stream from the Assembly.GetManifestResourceStream(...)(http://msdn.microsoft.com/en-us/library/xc4235zt.aspx).
For BuildAction Resource you will need to build a proper pack uri (see Resource File Pack URIs - Referenced Assembly in http://msdn.microsoft.com/en-us/library/aa970069(VS.85).aspx) and pass to Application.GetResourceStream (I'm actually not entirely sure if this approach works for dynamically loaded XAPs or not).

Related

Load html file from disk using CefGlue

I am playing with CefGlue (the .net wrapper around chromium embedded") I need to make some proof of concept so I create my own special html files that contains what I need to test. However I can't find a way to load those files.
The CefBrowserHost.CreateBrowser(cefWindowInfo, cefClient, cefBrowserSettings, url); requires an url and there is no overloaded method that accepts the content as string. So the question is: How to load html file from disk?
I've not used CefGlue - but in general you can use a custom cef resource handler to load the file. Briefly, when CEF sees your URL, they will call through a resource override handler you set up, which will then read that resource into a byte stream. In our case we read an html file compiled into our application resources. In your case I suppose you could also read the resource from disk at that time, though I've not done this. If you can compile resources into your C# app, you can add the html file into the Resources if you prefer.
We pass the url to CreateBrowserSync() in our case, and CEF ends up calling our ResourceHandler to load it. The CefClient c++ sample has an excellent example of this, see resource_util_win.cpp.
This is set up in a Handler override in CefResourceHandler. We overrode GetResourceHandler, their example overrides ProcessRequest in SchemeHandler. See scheme_test.cpp in the CefClient sample.
Most of the code from their samples is pretty boiler plate and you should be able to use what they have as the basis for your implementation - it's really too much code to list it all out here though.
I found a solution: just pass the full path to the file in the url parameter and everything works fine. It's just like chrome opening file from disk so I don't know why I did not tried that the first time.

Storing resources in XAP package

Is it possible to store resx files and access them from the application package without changing any code?
EDIT: I don't want to embed them into the assembly. The resource files should be able to be modified without compiling the project so I tought storing them in XAP and replace when needed.
Thanks.
It is possible to include any file in a XAP by specifying that its build action to be Content.
However I'm not sure that is useful to you in the way you are hoping. Silverlight can only utalise a resx file in the intended manner if it is embedded in a dll.
Instead of using .resx files consider using ResourceDictionary .xaml files.
a XAP file is encrypted. you could load resources into isolated storage and access them in a similar way to a resource.

How to optimize the size of silverlight XAP file by removing common files?

I have bunch of common thrid party dlls that I use in multiple silver light projects. Each produce its own XAP files with same copy of the thrid party dlls. I would like to know, is it possibl to seperate the common thrid party dlls out and load only if the local cache dll is different from the common dll used by some other XAP that was previously loaded?
In other words, what I was thinking is, first time when I enter a new silverlight page, it would load the application XAp and the Common XAP file, from that silverlight app, I go to another silverlight page, which in turn loads another XAP but this time, it will not load the common dll XAP since it was already loaded by the first XAP load.
Thanks,
If the third-party DLL's are well done, they'll come with an .extmap.xml file that allows for precisely this scenario. But you need to turn it on by checking the "Reduce XAP size by using application library caching" option in your Silverlight project(s). See http://msdn.microsoft.com/en-us/library/dd833069(VS.95).aspx for more details about how this works.
I think a more optimal solution would be to try to minify your XAP/Zip files using something like ComponentOne XAPOptimizer. The problem with your approach is that you are introducing dependencies between projects that in no way could be managed if one or more projects change. This is a good resource for strategies to lower XAP file size.
I believe that you have a main XAP which makes the calls to load the others XAPs, right?
All you have to do is, in the properties of each DLL, change the atribute "Copy to Local" to FALSE, except on your main XAP.
When the user download the main XAP, the DLL will be available to all dependents XAP.

Does silverlight code need protection?

I don't quite understand how Silverlight code works within the browser. Are the assemblies downloaded to the client machine? Is there any chance of the code getting decompiled using Reflector or some similar tool? If so, what options does one have to protect the code? Do .net obfuscators work with Silverlight?
Whenever you are in a web browser, all client side code is downloaded to the machine and can be examined by the user. This goes for Javascript, Flash, and Silverlight.
If you have proprietary code that absolutely must be hidden then you need to put it on the server and expose an API that the clients can call to show information to the user.
To view a Silverlight application the client download a .xap file that contains the dll and one configuration xml and optional resources. The dll contains compiled c# code that runs in a Silverlight runtime in client machine. Silverlight runtime is basically a subset of complete .net runtime. So the point is user gets the code in dll and then can use tools to get original source code. So at most you can do is obfuscation. Still for very critical code that should not be the option. You can use some other way (WCF or other webservices to hide some part of your code may be) if it shouts your need.
If you want to see just how easy it is to look at the code in a silverlight app just run SilverlightSpylink text by FirstFloor. As long as your have .NET Reflector installed you will be able to see (as you interact with the app) all the source code including the xaml files.
Since the code does get downloaded to the client (and even trying to prevent it with pragma no-cache won't work since they can hit the URL) you will need to protect your code by keeping important logic on the server.
If your afraid some one will steal your intellectual property and that law is not enough, Then you will need to obfuscate your code. But I would not call that protection per say but a deterrent to the casual reverse engineer.
Putting a pragma -No Cache- will prevent the .xap from being stored on the machine, instead it will be streamed by the Silverlight plugin. Without the pragma the .xap file is stored in the temp internet files.
Putting the application on a page on https will further protect the transmition of the .xap
If possible require authentication to view the web page / .xap file (thanks Joel)
Emrah,
Yes obfuscation is possible for SL application.
Yes, Silverlight xap files are nothing but zip files with your assemblies in them, so they do need protection via obfuscation. Give Crypto Obfuscator a try - it directly obfuscates xap files, it can also obfuscate XAML files in your assemblies by renaming class references, stripping comments, whitespace, newlines, etc

WinForms: Load localized help (chm) files

What is the best way to load a locale-specific (i.e. translated) compiled help file (.chm)? Our install will deploy them alongside the satellite assemblies containing resources. I'd like to re-use the same probing rules the .NET framework uses to load the satellite assemblies, and I'd definitely like to avoid writing my own searching algorithm, because for example, I'd have to handle specific cases such as "zh-CN/zh-Hans/zh-CHS".
I can't find anything with the System.Windows.Forms.Help class that indicates that you can provide a CultureInfo object to specify a particular culture. Anybody else run across this yet?
Thanks!
Off hand, I would suggest that instead of deploying your CHM file side by side with the satellite assembly, that you actually include the CHM as an embedded resource in the satellite assembly itself. Remember that an assembly can actually be contained across multiple files. What makes an assembly and assembly is the manifest.
By doing that, you can then request the resource for the current locale programatically, let the framework tell you the URL, and then supply that file location to the Help class.

Resources