C#: Security accessibility of the overriding method must match the security accessibility of the method being overriden

I was porting a .Net 2.0 assembly to .Net 4.0 plugin assembly and faced this particular challenge.

The project had a test app(A WinForm app) and it worked fine on the desktop but when I deployed the logic on server to run as a plugin it gave the following error

xyz..GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext) Security accessibility of the overriding method must match the security accessibility of the method being overriden.
Just notice, the spelling for “overridden” is also not correct. But this is what .Net framework threw.
After some research I found that this behavior is “By Design” i.e. as per security transparency model defined in .Net v4.0. It basically has two models:
1. Level1 (High Level) for v2.0
2. Level2 for v4.0
Here is a a list of critical operations:

1. Call native code

2. Contain unverifiable code

3. Call critical code

4. Perform security asserts

5. Satisfy link demands

6. Derive from non-transparent types

7. Override security critical virtuals

And only full trust code can be security critical.

Hence an overridden member should have the same security accessibility (Critical, Safe Critical or Transparent).
To resolve this error take the following step:
Go to the AssemblyInfo.cs class to your project and add following attributes to it. And this should resolve the exception.

[assembly: SecurityRules(SecurityRuleSet.Level1)] 
[assembly: AllowPartiallyTrustedCallers]

I believe it might be helpful to many as there are lot of Dynamics CRM implementations being upgraded.

Reference links to the differences between both and transparency rules:
http://blogs.msdn.com/b/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx
http://blogs.msdn.com/b/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx

Advertisements

C#: How to modify web.config file – Winforms

Use this method to modify/add keys/values to the web.config file from windows application.

private void ModifyWebConfig(string path, string key, string value)
{
    var config = ConfigurationManager.OpenMappedExeConfiguration(
        new ExeConfigurationFileMap() 
        { 
            ExeConfigFilename = path 
        }, 
        ConfigurationUserLevel.None);
    var element = config.AppSettings.Settings[key];

    // Check if element exists in web.config. if exists, simply update the value
    // if not, add a new key and its value
    if (element == null)
    {
        config.AppSettings.Settings.Add(key, value);
    }
    else
    {
        element.Value = value;
    }

    config.Save(ConfigurationSaveMode.Minimal);
}

C# Winforms TableLayoutPanel middle align the control

TableLayoutPanel control represents a panel that dynamically lays out its contents in a table format. You want to use a TableLayoutPanel in complex and sophisticated applications where you need to create dynamic layouts.TableLayoutPanel

By default when you add controls to the TableLayoutPanel, it sets them to the top left corner. When you want to align these controls (for exam, Button) to be middle, Set the value of the Button control’s Anchor property to None. The Button control moves to the center of the cell.

C# ComboBox: Prevent/Stop SelectedIndexChanged event automatically from firing when DataSource is used

Hmmm…

So many people are facing the same issue… The issue is whenever a list is assigned to ComboBox through DataSource in C# winforms, ComboBox’s SelectedIndexChanged event automatically fires up.

How to stop/prevent it? Simple… Just use SelectionChangeCommitted instead of SelectedIndexChanged event.

That’s it! Give it a try!!