I know this is a very old question but I just ran into this issue and was able to fix it using a different method than the accepted answer and since this is the first result on google when searching for the error message I think it will be useful to others if I share my solution.
The issue I was running into came about when I attempted to integrate with a piece of hardware. The hardware had its own installer which would register a DLL into the GAC. The DLL it installed had 2 dependency DLLs but for some reason when the installer was run it was not registering the dependency DLLs.
Basically the scenario was that the Entry DLL was registered in the GAC and its two dependency DLLs were not registered in the GAC but did exist next to the executable.
When I ran my program and attempted to use the piece of hardware, the program was looking for the entry DLL next to the executable, which it could not find. The program would then go to the GAC, where it would find the entry DLL. Once inside the DLL for the hardware it would eventually try to use the dependency DLLs which were not in the GAC but were next to the executable. Calling out of the GAC to the DLL that was next to the executable was throwing the partially trusted caller error.
I resolved this by placing a copy of the entry DLL next to the executable.
I was curious as to what scenarios would work and what would cause the security error and I've found these are the scenarios that worked as expected:
- All three of the DLLs next to the executable.
- All three DLLs in the GAC.
The only scenario that consistently failed was when any layer was inside the GAC and any of the dependency DLLs were outside the GAC.
Failed Scenario #1:
- Entry DLL in the GAC
DLL #2 and DLL #3 next to the Exe
- Fails siting DLL #2 as the faulting DLL.
Failed Scenario #2:
- Entry DLL and DLL #2 in the GAC
DLL #3 next to the Exe
- Fails siting DLL #3 as the faulting DLL.
Failed Scenario #3:
- Entry DLL and DLL #3 in the GAC
DLL #2 next to the exe
- (Predictably) Fails siting DLL #2 as the faulting DLL.
I did not test it but I think its a safe assumption to say that if the entry DLL and DLL #3 had been next to the executable and DLL #2 had been in the GAC then it would have faulted with DLL #3 being sited as the problem.