Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource and references to System.Windows.Forms in web applications

I use a resource file in my ASP.NET MVC application and ReSharper warns me about not finding System.Windows.Forms reference when I analyze the project's code issues.

Should I really be referencing System.Windows.Forms in my web application? Why is it needed in the resx file anyway?

When I open the resx file in XML editor, I see that it references the assembly:

<resheader name="reader">
  <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
  <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="..." type="System.Resources.ResXFileRef, System.Windows.Forms">
  <value>...;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>

I really don't like that my web application is linked to anything System.Windows.Forms related.

Why is that? Is there a better way of managing resources on web applications then using these resx files?

like image 773
Tom Pažourek Avatar asked Aug 27 '15 12:08

Tom Pažourek


1 Answers

Resources file formats are documented here: Creating Resource Files

The .resources format is a binary format that allows resources to be embedded in runtime .dll or .exe. It can be read and written using classes that exist in the mscorlib assembly: ResourceReader and ResourceWriter

The .ResX file format is an XML-based format that can be read and written using classes that exist in the System.Windows.Forms assembly: ResXResourceReader and ResXResourceWriter. (the XML schema is in fact explained in the comment put in the file when you create a new ResX file).

Only .resources should be embedded in runtime executable. That's why the .ResX format can be considered as a "source file format" for the .resources format.

Now, in these formats, you can embed resources of virtually any type, but you have to tell the system how, since the data stored in these files is in fact a serialized version of the data. For storing strings, you don't need any extra dependencies on external deserializer class, but for other types (like Icon, Bitmap, etc.), you do. The Visual Studio editor uses the System.Windows.Forms (de)serializers classes that you see in your files. So you need to implicitely load System.Windows.Forms for deserialization of these non-string data to work.

So, if you have created non-string resources using Visual Studio, you will have an implicit reference on System.Windows.Forms, yes. If you don't want this, remove all references to these non-string resources (you can also tweak the file manually pretty easily) and store your assets using other means ('Embedded Resource' on Build Action for example, or plain "assets" directory in your web site).

Note you can also theoretically use your own classes for storing your custom data as shown here: Resources in .resx File Format

The following example shows an Int32 object saved in a .resx file, and the beginning of a bitmap object, which holds the binary information from an actual .gif file.

<data name="i1" type="System.Int32, mscorlib">
  <value>20</value>
</data>

<data name="flag" type="System.Drawing.Bitmap, System.Drawing,   
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
mimetype="application/x-microsoft.net.object.bytearray.base64">
  <value>
    AAEAAAD/////AQAAAAAAAAAMAgAAADtTeX…
  </value>
</data>

To my knowledge, the Visual Studio editor does not support custom classes like this for edition of .ResX files.

like image 150
Simon Mourier Avatar answered Nov 11 '22 08:11

Simon Mourier