System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetAssembly(typeof(Adapter));
PolicyLevel pl =
PolicyLevel.CreateAppDomainLevel();
// include a code group that gives permissions only to assemblies
// that are strong named with s_somePublicKey
StrongNameMembershipCondition snMC = CreateStrongNameMembershipCondition(myAssembly);UnionCodeGroup
snCG = new
UnionCodeGroup(snMC,
new
PolicyStatement(new
PermissionSet(PermissionState.Unrestricted)));
pl.RootCodeGroup.AddChild(snCG);
// set our new policy as the domain-level policy
adHocDomain.SetAppDomainPolicy(pl);
// identify our assembly
string myFullName = myAssembly.FullName;
string stubFullTypeName =
typeof(crossDomainStub).ToString();
crossDomainStub crossDS =
null;
// load an instance of crossDomainStup into ad hoc AppDomain
System.Security.Policy.Evidence adEvidence = myAssembly.Evidence;
try
Recently I migrate the project to Visual studio 2008.
It's working fine if I am running it under .Net 2.0
But failing in .net 3.5.
The Exception is "Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
rajibsahu
Member
36 Points
18 Posts
Appdomain.CreateInstance is not working when the project is migrating to 3.5 from 2.0
Jul 16, 2008 08:04 AM|LINK
Hi,
I have a project which was develpoed in .Net 2.0, C#.
In that project a Appdomain is created dynamically and creating a instance of the Dll within that Appdomain.
It's working fine with .net 2.0
my code piece is looking like the below
try{
// initialize tempDir to something like <LocalUser\Application Data> ...tempDir = CreateTempDir(adHocDomainName);
PutConfigAndPolicy(tempDir, adHocDomainName, wsePolicy);
// create ad hoc domainadHocDomain = createDomain(adHocDomainName, tempDir);
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetAssembly(typeof(Adapter)); PolicyLevel pl = PolicyLevel.CreateAppDomainLevel(); // include a code group that gives permissions only to assemblies // that are strong named with s_somePublicKey StrongNameMembershipCondition snMC = CreateStrongNameMembershipCondition(myAssembly);UnionCodeGroup snCG = new UnionCodeGroup(snMC, new PolicyStatement(new PermissionSet(PermissionState.Unrestricted)));pl.RootCodeGroup.AddChild(snCG);
// set our new policy as the domain-level policyadHocDomain.SetAppDomainPolicy(pl);
// identify our assembly string myFullName = myAssembly.FullName; string stubFullTypeName = typeof(crossDomainStub).ToString(); crossDomainStub crossDS = null; // load an instance of crossDomainStup into ad hoc AppDomain System.Security.Policy.Evidence adEvidence = myAssembly.Evidence; try{
System.Runtime.Remoting.ObjectHandle oh = adHocDomain.CreateInstance(myFullName,stubFullTypeName,false,System.Reflection.BindingFlags.CreateInstance,null,null,null,null,adEvidence);crossDS = (crossDomainStub) oh.Unwrap();}
catch(Exception ex){
}
Recently I migrate the project to Visual studio 2008.
It's working fine if I am running it under .Net 2.0
But failing in .net 3.5.
The Exception is "Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
and the stack trace is
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Object assemblyOrString, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at System.Security.CodeAccessSecurityEngine.CheckHelper(PermissionSet grantedSet, PermissionSet refusedSet, CodeAccessPermission demand, PermissionToken permToken, RuntimeMethodHandle rmh, Object assemblyOrString, SecurityAction action, Boolean throwException)
at System.Security.CodeAccessSecurityEngine.CheckHelper(CompressedStack cs, PermissionSet grantedSet, PermissionSet refusedSet, CodeAccessPermission demand, PermissionToken permToken, RuntimeMethodHandle rmh, Assembly asm, SecurityAction action)
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Security.Policy.Hash.GetObjectData(SerializationInfo info, StreamingContext context)
at System.Runtime.Serialization.ObjectCloneHelper.GetObjectData(Object serObj, String& typeName, String& assemName, String[]& fieldNames, Object[]& fieldValues)
at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
at Microsoft.MySalesComp.Adapter.SoapCallWSE(String targetURL, String soapAction, XmlDocument soapBody, XmlDocument wsePolicy, String certificateCaptionAndCacheKey, String userTokenCaptionAndCacheKey) in D:\POC\ExcelAddIn_MIC_2008\DashboardUI\WSE\Adaptor.cs:line 341
If I am trying to run it again in 2.0 than also it's not working.
If you have any information to reso;ve this problem please share with me.
Thanks & regards,
Rajib
.NET 3.5 3.5 Vistaisual Studio 2008 .net framework 3.5 Frameowrk 3.5