October 31st, 2012, 10:45 PM
assembly reference not working in C# script
I'm trying to bring some assemblies into my bin folder for my website. I've got some C# script on the page that references and uses the assemblies. It looks like this:
I can create a new MySqlConnection object just fine but it's the System.Windows.Forms object (or namespace?) that's giving me trouble.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Windows.Forms" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="MySql.Data.MySqlClient" %>
void Page_Load(object sender, System.EventArgs e)
MySql.Data.MySqlClient.MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection();
Label1.Text = "hello world";
I found the dll in C:\Windows\Microsoft.NET\assembly\GAC_MSIL and copied it over to my wwwroot/bin/ folder.
The error I get when I try to load the page is:
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Could not load file or assembly 'System.Windows.Forms' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
Line 57: <add assembly="System.EnterpriseServices, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
Line 58: <add assembly="System.Web.Mobile, Version=188.8.131.52, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
Line 59: <add assembly="*"/>
Line 60: <add assembly="System.Runtime.Serialization, Version=184.108.40.206, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"/>
Line 61: <add assembly="System.IdentityModel, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"/>
Source File: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Config\web.config Line: 59
Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Windows.Forms' could not be loaded.
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
Version Information: Microsoft .NET Framework Version:2.0.50727.5456; ASP.NET Version:2.0.50727.5456
So it says that the assembly I copied over was built more recently than whatever's already loaded (that's how I'm interpreting it at least). It tells me where the exact location of the problem is (line 59 in web.config) and that's where you see the <add assembly="*"/> but it doesn't tell me what to replace it with. It also tells me how to turn assembly binding logging off (whatever assembly binding means), but I don't seem to have the registry path it gives me on my computer.
November 2nd, 2012, 08:11 AM
Re: assembly reference not working in C# script
If you are using an Integrated Development Environment (IDE) such as Visual Studio or SharpDevelop, you should not be copying core .Net Framework files into your bin folder manually. Use the AddReference dialog to add a reference, this will prompt the IDE to copy the file to your bin folder at compile time. The advantage to this is that the dialog will be famaliar with what is available in the specific .Net Framework version that you are working with, and present those choices as a list that you can select from. By adding a reference through the dialog, the IDE knows to copy the proper Framework dll into your bin folder for access by your application. The web.config file will also be updated by the IDE to reference the proper dll file.
If you are developing without an IDE, using a text editor and the commald line tools, you must be exceedingly careful that all of the dll files that you use are from the same version/build of the .Net Framework. However, with all of the free choices of IDE out there today for developing under .Net, it seems a bit foolish not to use one. (Visual Studio Express from Microsoft and SharpDevelop from icsharpcode to name the two most popular)
Now, on to your code sample:
You must realize that, when developing a web page, some of the code runs on the server and some of the code runs at the client in their browser.
When a page is requested by the client's browser, the server-side code performs any processing necessary and generates the markup to be rendered by the client's browser and any script that will be run by the client's browser. This markup and script is sent back to the requesting browser. This is called a request/response pair and is primarily how the web works (much simplified).
The server-side code has access to the resources of the server and the client-side script has access to any browser resources made available by the browser's implementation of the Domain Object Model (DOM). The two of them do not have any direct accesss to the resources exposed by the other.
The call you are making to System.Windows.Forms.MessageBox.Show, even if it worked (I don't believe it will work though, IIS should block it and may throw an Exception), would pop up a dialog at the server, not on the client. In order to pop up a dialog at the client, you must use the browser's resources through a call in script to alert("your message here") or window.open() (you should Google window.open if you want to use it as it has many parameters to control how the new window opens and the specifics of it's use are a bit beyond the depth of this discussion).
November 7th, 2012, 11:48 PM
Re: assembly reference not working in C# script
I downloaded VS 2012 Exp. Web and I'm developing in that now.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!