Sai Stuff to Developers

September 3, 2014

Problem with ASP.NET using MySQL Connecting through ODBC Driver in some Hosting Servers

Filed under: DotNet — tosaik @ 9:48 am
Tags: , , , , , , ,

Recently i hosted one of my asp.net 4.0 application which is using MySQL as backend and i decided to use MySQL .NET connector to access the MySQL server. I hosted one of the local server and its works fine but i got some consistency issues with my local server and decided to shift my application to GoDaddy which they ensure our application will be 24 X 7 in active.

I moved the application successfully but when i try to run the application through browser i cannot able to connect to database when i dig in to the issue through my logs i came to know that GoDaddy doesn’t support MySQL .NET connector DLL because GoDaddy will run only components and application which are Fully Trusted. When i read the blogs regarding this DLL i came to know Oracle has built this component as partial trusted (medium trust) which means i cannot use anymore with my application to host in GoDaddy. But here i have 2 options

  1. Need to place my MySql .Net connector component to the GAC of GoDaddy server.
  2. Need to remove using MySql .Net connector component in my application.

In the above options, option-1 GoDaddy doesn’t allow me to do so. Now i have only option-2, i removed code related to MySql .Net connector component and removed the reference to it and i try to connect mySQL database through ODBC (Object Database Connector). Which i successfully implemented in my application and working fine on my local computer and i updated the same over the hosting server (in my case GoDaddy), Interestingly still i am unable to connect to the Database, Now when i log my application to find the root cause of the issue, following are the error its throwing:

Request for the permission of type ‘System.Data.Odbc.OdbcPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

OOPS, i realized that GoDaddy also need my application to be fully trusted which i need to configure in my application via. web.config in System.web section as shown below:

 

<trust level="Full"/>

Once i changed this and made my application full trusted with this simple configuration change, My Application working like a charm.

The reason behind on writing this post is, i worked nearly 5 hrs to fix this issue and i cannot find any direct help from any blogs so i decided to keep this information in my blog to make others time save.

Thank You,

Happy Coding…..

 

May 15, 2012

Making Our Component to be dynamically loading the Config File

Filed under: DotNet — tosaik @ 11:00 am
Tags: , , ,

Introduction
Generally there could be a requirement such that any independent component needs to be having its own configuration settings like for example, let say I have a vc++ application which acts as a container so that it can load any Active X Control in it. Now as a new technology I want to use some plugin to be built in Visual C# Language using .NET Framework. As we already know we can build the Active X Component using .NET Framework. Now let’s say my component plugin need to interact with Database so I need to provide it a Connection string and I can’t hard code it inside my Component so I decided to have application configuration file (i.e., App.Config file), but how can I map this file to my plugin when it is running in my VC++ container?
This article explains how we can solve this issue, and how can we map this config file even when it is converted to Active X Control and running in other environment.
Background
Need to have a basic knowledge in creating Active X Control or look for my next article “How to create an Active X Control using .NET Framework”.
Explanation
I don’t want to make it complex for you, as we are on path what we want now by reading the Introduction section.
We may have other options like maintaining separate xml file and have all the complex custom access through the xml file using xpath technology. But the way we access this configuration file using System. Configuration.ConfigurationManager Class is very Flexible and comfortable.
The solution is simple we need to create a class and name ig from my side I named as “ChangeMyAppConfig” and this class need to be Implement “AppConfig” Class.

public class ChangeAppConfig : AppConfig
{
private readonly string oldConfig =
AppDomain.CurrentDomain.GetData(“APP_CONFIG_FILE”).ToString();

private bool disposedValue;

public ChangeAppConfig(string path)
{
AppDomain.CurrentDomain.SetData(“APP_CONFIG_FILE”, path);
ResetConfigMechanism();
}

public override void Dispose()
{
if (!disposedValue)
{
AppDomain.CurrentDomain.SetData(“APP_CONFIG_FILE”, oldConfig);
ResetConfigMechanism();

disposedValue = true;
}
GC.SuppressFinalize(this);
}

private static void ResetConfigMechanism()
{
typeof(ConfigurationManager)
.GetField(“s_initState”, BindingFlags.NonPublic |
BindingFlags.Static)
.SetValue(null, 0);

typeof(ConfigurationManager)
.GetField(“s_configSystem”, BindingFlags.NonPublic |
BindingFlags.Static)
.SetValue(null, null);

typeof(ConfigurationManager)
.Assembly.GetTypes()
.Where(x => x.FullName ==
“System.Configuration.ClientConfigPaths”)
.First()
.GetField(“s_current”, BindingFlags.NonPublic |
BindingFlags.Static)
.SetValue(null, null);
}
}

The main AppDomain has the property called “APP_CONFIG_FILE” where it stores the configuration file information now we need to fetch this property using AppDomain.GetData() method and set our new configuration file path using AppDomain.SetData() Method. It’s not enough to change the value of the property with this we need to reset the configuration mechanism as we are doing the same in ResetConfigMechanism() method in the above code.

That’s it Now you need to call the ChangeAppConfig() Method from your component by providing the valid configuration path.
After calling this method you can now access the Configuration file settings using System. Configuration.ConfigurationManager Class as we do normally in every application in .NET Environment.
Example:
public string strCommonFolder = ConfigurationManager.AppSettings[“FolderPath”];

Happy Coding 🙂

Create a free website or blog at WordPress.com.

%d bloggers like this: