I was working with one of my colleague on an issue. It was a weird one, where customer was using SPWebConfigModification class to add the safe control entry into the web.config. The sample code being used was adapted from the SPWebConfigModification page on MSDN.
Surprisingly when we were using this code to add the web.config entry, it was adding it properly but when we were trying to remove it, through web.config file was being modified but entry was not being removed.
This was really surprising, if we took the exact code from the MSDN page, it worked perfectly fine.
Then started the journey to find the root cause for this.
Digging into my older emails, I remember that something similar we had done earlier also but how it was resolved, was not known. Found that old email and check that code, that also worked perfectly fine.
That brought us to line by line comparison of the code and see what is happening and we found the culprit code!
In the following code at line 6, we are giving the assembly name with only the first part of the 4 part name of assembly.
But the entry for the SafeControl was done with full 4 part assembly name at line 12.
So when adding, it added the entry but when removing, SharePoint is not able to find any entry with only first part of the 4 part assembly name. That is why, it is not able to remove it.
Why the code at MSDN page is working fine?
Finally 1 more issue resolved and got to understand to look at the code more closely and not to miss the obvious.
As always… Happy Coding