H
h_schmieder
Guest
Hello,
we implemnted an assembly in C# compiled as dll and a shim which loads this assembly in a new appdomain
created by this shim. This created appdomain has normally the ID 2.
This assembly implements (besides other things) a callback which is called from an external source inside the same process.
Normally the external source calls the callback in the conetxt of appdomain 2,
but in some cases the callback is called in the context od the defaultdomain (Id ==1).
So It thought I use DoCallback from Appdomain class to switch to appdomain 2.
But In my test this doesn't work as expected.
Here's my code
namespace MyNameSpace
{
public static class MyHelper
{
private const string ConstTransfer = "transferstring";
// setting thisdomain to the domain with ID 2
public static readonly AppDomain thisDomain = AppDomain.CurrentDomain;
public static bool Testlogging(string prefix)
{
try
{
// Expected that curid is always 2
int curid = AppDomain.CurrentDomain.Id;
bool access = thisDomain != null;
int orgid = thisDomain?.Id ?? -1;
bool sameid = curid == orgid;
ErrorHandler.WriteDebugLog(prefix + " : " + curid + "|" + access + "|" + orgid + "|" + sameid);
return sameid;
}
catch (Exception e)
{
// ...
}
return false;
}
public static string MyCallbackCore(string instring)
{
/*
do the magic with help of a third party control.
Must be executed in the context of Appdomain with ID 2
*/
....
}
public static void MyCallBackWrapper()
{
int curid = AppDomain.CurrentDomain.Id;
string prefix = "Wrapper";
Testlogging(prefix);
string instring = thisDomain.GetData(ConstTransfer) as string;
ErrorHandler.WriteDebugLog(prefix + " : " + thisDomain.Id + "|" + curid);
ErrorHandler.WriteDebugLog(prefix + " : " + idpUrl);
string response = MyCallbackCore(instring);
thisDomain.SetData(ConstTransfer, response);
}
public static string MytCallBack(string incstring)
{
try
{
string prefix = "Callback";
bool sameid = Testlogging(prefix);
ErrorHandler.WriteDebugLog(prefix + " : " + incstring);
if (sameid)
{
return MyCallbackCore(idpUrl);
}
else
{
ErrorHandler.WriteDebugLog(prefix + " in else : " + thisDomain.Id);
thisDomain.SetData(ConstTransfer, incstring);
thisDomain.DoCallBack(new CrossAppDomainDelegate(MyCallBackWrapper));
string response = thisDomain.GetData(ConstTransfer) as string;
return response;
}
}
catch
{
return string.Empty;
}
}
}
}
In the log I see that inside MyCallBackWrapper AppDomain.CurrentDomain.Id is still 1 instead of 2 as expected.
What Do I miss ?
tia
Hendrik Schmieder
Continue reading...
we implemnted an assembly in C# compiled as dll and a shim which loads this assembly in a new appdomain
created by this shim. This created appdomain has normally the ID 2.
This assembly implements (besides other things) a callback which is called from an external source inside the same process.
Normally the external source calls the callback in the conetxt of appdomain 2,
but in some cases the callback is called in the context od the defaultdomain (Id ==1).
So It thought I use DoCallback from Appdomain class to switch to appdomain 2.
But In my test this doesn't work as expected.
Here's my code
namespace MyNameSpace
{
public static class MyHelper
{
private const string ConstTransfer = "transferstring";
// setting thisdomain to the domain with ID 2
public static readonly AppDomain thisDomain = AppDomain.CurrentDomain;
public static bool Testlogging(string prefix)
{
try
{
// Expected that curid is always 2
int curid = AppDomain.CurrentDomain.Id;
bool access = thisDomain != null;
int orgid = thisDomain?.Id ?? -1;
bool sameid = curid == orgid;
ErrorHandler.WriteDebugLog(prefix + " : " + curid + "|" + access + "|" + orgid + "|" + sameid);
return sameid;
}
catch (Exception e)
{
// ...
}
return false;
}
public static string MyCallbackCore(string instring)
{
/*
do the magic with help of a third party control.
Must be executed in the context of Appdomain with ID 2
*/
....
}
public static void MyCallBackWrapper()
{
int curid = AppDomain.CurrentDomain.Id;
string prefix = "Wrapper";
Testlogging(prefix);
string instring = thisDomain.GetData(ConstTransfer) as string;
ErrorHandler.WriteDebugLog(prefix + " : " + thisDomain.Id + "|" + curid);
ErrorHandler.WriteDebugLog(prefix + " : " + idpUrl);
string response = MyCallbackCore(instring);
thisDomain.SetData(ConstTransfer, response);
}
public static string MytCallBack(string incstring)
{
try
{
string prefix = "Callback";
bool sameid = Testlogging(prefix);
ErrorHandler.WriteDebugLog(prefix + " : " + incstring);
if (sameid)
{
return MyCallbackCore(idpUrl);
}
else
{
ErrorHandler.WriteDebugLog(prefix + " in else : " + thisDomain.Id);
thisDomain.SetData(ConstTransfer, incstring);
thisDomain.DoCallBack(new CrossAppDomainDelegate(MyCallBackWrapper));
string response = thisDomain.GetData(ConstTransfer) as string;
return response;
}
}
catch
{
return string.Empty;
}
}
}
}
In the log I see that inside MyCallBackWrapper AppDomain.CurrentDomain.Id is still 1 instead of 2 as expected.
What Do I miss ?
tia
Hendrik Schmieder
Continue reading...