Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,7 @@ private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref SniConsum

#endregion


#if NETFRAMEWORK
static AppDomain GetDefaultAppDomainInternal()
{
return AppDomain.CurrentDomain;
}

internal static _AppDomain GetDefaultAppDomain()
{
return GetDefaultAppDomainInternal();
}

[ResourceExposure(ResourceScope.Process)] // SxS: there is no way to set scope = Instance, using Process which is wider
[ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
internal static unsafe byte[] GetData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,47 +470,38 @@ private static void ObtainProcessDispatcher()

#if DEBUG // Possibly expensive, limit to debug.
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.ObtainProcessDispatcher|DEP> AppDomain.CurrentDomain.FriendlyName: {0}", AppDomain.CurrentDomain.FriendlyName);

#endif // DEBUG
_AppDomain masterDomain = SniNativeWrapper.GetDefaultAppDomain();

if (masterDomain != null)

_AppDomain masterDomain = AppDomain.CurrentDomain;

ObjectHandle handle = CreateProcessDispatcher(masterDomain);
if (handle != null)
{
ObjectHandle handle = CreateProcessDispatcher(masterDomain);
SqlDependencyProcessDispatcher dependency = (SqlDependencyProcessDispatcher)handle.Unwrap();

if (handle != null)
if (dependency != null)
{
SqlDependencyProcessDispatcher dependency = (SqlDependencyProcessDispatcher)handle.Unwrap();

if (dependency != null)
{
s_processDispatcher = SqlDependencyProcessDispatcher.SingletonProcessDispatcher; // Set to static instance.
s_processDispatcher = SqlDependencyProcessDispatcher.SingletonProcessDispatcher; // Set to static instance.

// Serialize and set in native.
using (MemoryStream stream = new())
{
SqlClientObjRef objRef = new(s_processDispatcher);
DataContractSerializer serializer = new(objRef.GetType());
GetSerializedObject(objRef, serializer, stream);
SniNativeWrapper.SetData(stream.ToArray()); // Native will be forced to synchronize and not overwrite.
}
}
else
// Serialize and set in native.
using (MemoryStream stream = new())
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - ObjectHandle.Unwrap returned null!");
throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyObtainProcessDispatcherFailureObjectHandle);
SqlClientObjRef objRef = new(s_processDispatcher);
DataContractSerializer serializer = new(objRef.GetType());
GetSerializedObject(objRef, serializer, stream);
SniNativeWrapper.SetData(stream.ToArray()); // Native will be forced to synchronize and not overwrite.
}
}
else
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - AppDomain.CreateInstance returned null!");
throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyProcessDispatcherFailureCreateInstance);
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - ObjectHandle.Unwrap returned null!");
throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyObtainProcessDispatcherFailureObjectHandle);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The net change here will result in a different error code (and trace message), but I think the change makes sense

}
}
else
{
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - unable to obtain default AppDomain!");
throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyProcessDispatcherFailureAppDomain);
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - AppDomain.CreateInstance returned null!");
throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyProcessDispatcherFailureCreateInstance);
}
}
else
Expand Down