Skip to content

Commit

Permalink
fixed issue dotnet#401
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEshtehari committed Feb 6, 2020
1 parent 5ed506e commit 5b4487f
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,4 @@ MigrationBackup/

# Config Json file
src/Microsoft.Data.SqlClient/tests/ManualTests/config.json
/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlDependencyTest.cs
67 changes: 59 additions & 8 deletions src/Microsoft.Data.SqlClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "add-ons", "add-ons", "{C972
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.XUnitExtensions", "Microsoft.Data.SqlClient\tests\tools\Microsoft.DotNet.XUnitExtensions\Microsoft.DotNet.XUnitExtensions.csproj", "{FDA6971D-9F57-4DA4-B10A-261C91684CFC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyTestNet461", "Microsoft.Data.SqlClient\tests\MyTestNet461\MyTestNet461.csproj", "{56D93690-DAF8-4391-8168-DB25CB106B77}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -767,16 +769,16 @@ Global
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp2.1-Release|x86.Build.0 = netcoreapp2.1-Release|Any CPU
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|Any CPU.Build.0 = Debug|Any CPU
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x64.ActiveCfg = netcoreapp3.1-Debug|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x64.Build.0 = netcoreapp3.1-Debug|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x86.ActiveCfg = netcoreapp3.1-Debug|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x86.Build.0 = netcoreapp3.1-Debug|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x64.ActiveCfg = netcoreapp2.1-Debug|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x64.Build.0 = netcoreapp2.1-Debug|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x86.ActiveCfg = netcoreapp2.1-Debug|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Debug|x86.Build.0 = netcoreapp2.1-Debug|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|Any CPU.ActiveCfg = Release|Any CPU
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|Any CPU.Build.0 = Release|Any CPU
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x64.ActiveCfg = netcoreapp3.1-Release|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x64.Build.0 = netcoreapp3.1-Release|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x86.ActiveCfg = netcoreapp3.1-Release|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x86.Build.0 = netcoreapp3.1-Release|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x64.ActiveCfg = netcoreapp2.1-Debug|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x64.Build.0 = netcoreapp2.1-Debug|x64
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x86.ActiveCfg = netcoreapp2.1-Debug|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.netcoreapp3.1-Release|x86.Build.0 = netcoreapp2.1-Debug|x86
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.Release|Any CPU.Build.0 = Release|Any CPU
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7}.Release|x64.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -831,6 +833,54 @@ Global
{FDA6971D-9F57-4DA4-B10A-261C91684CFC}.Release|x64.Build.0 = Release|Any CPU
{FDA6971D-9F57-4DA4-B10A-261C91684CFC}.Release|x86.ActiveCfg = Release|Any CPU
{FDA6971D-9F57-4DA4-B10A-261C91684CFC}.Release|x86.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Debug|x64.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Debug|x64.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Debug|x86.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Debug|x86.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Debug|Any CPU.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Debug|x64.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Debug|x64.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Debug|x86.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Debug|x86.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Release|Any CPU.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Release|Any CPU.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Release|x64.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Release|x64.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Release|x86.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.net46-Release|x86.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Debug|Any CPU.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Debug|x64.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Debug|x64.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Debug|x86.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Debug|x86.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Release|Any CPU.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Release|Any CPU.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Release|x64.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Release|x64.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Release|x86.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp2.1-Release|x86.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Debug|Any CPU.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Debug|x64.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Debug|x64.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Debug|x86.ActiveCfg = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Debug|x86.Build.0 = Debug|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Release|Any CPU.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Release|Any CPU.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Release|x64.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Release|x64.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Release|x86.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.netcoreapp3.1-Release|x86.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Release|Any CPU.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Release|x64.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Release|x64.Build.0 = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Release|x86.ActiveCfg = Release|Any CPU
{56D93690-DAF8-4391-8168-DB25CB106B77}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -854,6 +904,7 @@ Global
{412BCCC8-19F6-489A-B594-E9A506816155} = {771F3F1E-7A68-4A9D-ADA8-A24F1D5BE71D}
{9073ABEF-92E0-4702-BB23-2C99CEF9BDD7} = {C9726AED-D6A3-4AAC-BA04-92DD1F079594}
{FDA6971D-9F57-4DA4-B10A-261C91684CFC} = {0CC4817A-12F3-4357-912C-09315FAAD008}
{56D93690-DAF8-4391-8168-DB25CB106B77} = {0CC4817A-12F3-4357-912C-09315FAAD008}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {01D48116-37A2-4D33-B9EC-94793C702431}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ internal static int SniMaxComposedSpnLength

static AppDomain GetDefaultAppDomainInternal()
{
var host = new mscoree.CorRuntimeHost();
host.GetDefaultDomain(out object temp);
AppDomain defaultAppDomain = temp as AppDomain;
return defaultAppDomain;
//var host = new mscoree.CorRuntimeHost();
//host.GetDefaultDomain(out object temp);
//AppDomain defaultAppDomain = temp as AppDomain;
//return defaultAppDomain;
return AppDomain.CurrentDomain;
}

internal static _AppDomain GetDefaultAppDomain()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\netfx\src\Microsoft.Data.SqlClient.csproj" />
</ItemGroup>

</Project>
143 changes: 143 additions & 0 deletions src/Microsoft.Data.SqlClient/tests/MyTestNet461/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using Xunit;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;


namespace MyTestNet461
{
public class UnitTest1
{
[Fact]
public void Test1()
{
string connectionStringNewDb = "Server=localhost; Integrated Security = true;Initial Catalog=testdb2";
SqlDependency.Start(connectionStringNewDb);
}
}

public static class CLRUtil
{
public static _AppDomain GetDefaultAppDomain()
{
// Obtain ICLRMetaHost interface
object objHost;
int hr = CLRCreateInstance(ref CLSID_CLRMetaHost, ref IID_CLRMetaHost, out objHost);
if (hr < 0)
throw new COMException("Cannot create meta host", hr);
var host = (ICLRMetaHost)objHost;

// Obtain ICLRRuntimeInfo interface
var vers = Environment.Version;
var versString = string.Format("v{0}.{1}.{2}", vers.Major, vers.Minor, vers.Build);
var objRuntime = host.GetRuntime(versString, ref IID_CLRRuntimeInfo);
var runtime = (ICLRRuntimeInfo)objRuntime;
bool started;
uint flags;
runtime.IsStarted(out started, out flags);
if (!started)
throw new COMException("CLR not started??");

// Obtain legacy ICorRuntimeHost interface and iterate appdomains
var V2Host = (ICorRuntimeHost)runtime.GetInterface(ref CLSID_CorRuntimeHost, ref IID_CorRuntimeHost);
IntPtr hDomainEnum;
V2Host.EnumDomains(out hDomainEnum);
V2Host.GetDefaultDomain(out _AppDomain temp);
V2Host.CloseEnum(hDomainEnum);

return temp;
}

public static IEnumerable<_AppDomain> EnumAppDomains()
{
// Obtain ICLRMetaHost interface
object objHost;
int hr = CLRCreateInstance(ref CLSID_CLRMetaHost, ref IID_CLRMetaHost, out objHost);
if (hr < 0)
throw new COMException("Cannot create meta host", hr);
var host = (ICLRMetaHost)objHost;

// Obtain ICLRRuntimeInfo interface
var vers = Environment.Version;
var versString = string.Format("v{0}.{1}.{2}", vers.Major, vers.Minor, vers.Build);
var objRuntime = host.GetRuntime(versString, ref IID_CLRRuntimeInfo);
var runtime = (ICLRRuntimeInfo)objRuntime;
bool started;
uint flags;
runtime.IsStarted(out started, out flags);
if (!started)
throw new COMException("CLR not started??");

// Obtain legacy ICorRuntimeHost interface and iterate appdomains
var V2Host = (ICorRuntimeHost)runtime.GetInterface(ref CLSID_CorRuntimeHost, ref IID_CorRuntimeHost);
IntPtr hDomainEnum;
V2Host.EnumDomains(out hDomainEnum);
for (; ; )
{
_AppDomain domain = null;
V2Host.NextDomain(hDomainEnum, out domain);
if (domain == null)
break;
yield return domain;
}
V2Host.CloseEnum(hDomainEnum);
}

private static Guid CLSID_CLRMetaHost = new Guid(0x9280188d, 0xe8e, 0x4867, 0xb3, 0xc, 0x7f, 0xa8, 0x38, 0x84, 0xe8, 0xde);
private static Guid IID_CLRMetaHost = new Guid(0xD332DB9E, 0xB9B3, 0x4125, 0x82, 0x07, 0xA1, 0x48, 0x84, 0xF5, 0x32, 0x16);
private static Guid IID_CLRRuntimeInfo = new Guid(0xBD39D1D2, 0xBA2F, 0x486a, 0x89, 0xB0, 0xB4, 0xB0, 0xCB, 0x46, 0x68, 0x91);
private static Guid CLSID_CorRuntimeHost = new Guid(0xcb2f6723, 0xab3a, 0x11d2, 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e);
private static Guid IID_CorRuntimeHost = new Guid(0xcb2f6722, 0xab3a, 0x11d2, 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e);

[DllImport("mscoree.dll")]
private static extern int CLRCreateInstance(ref Guid clsid, ref Guid iid,
[MarshalAs(UnmanagedType.Interface)] out object ptr);

[ComImport, Guid("D332DB9E-B9B3-4125-8207-A14884F53216"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ICLRMetaHost
{
[return: MarshalAs(UnmanagedType.Interface)]
object GetRuntime(string version, ref Guid iid);
// Rest omitted
}

[ComImport, Guid("BD39D1D2-BA2F-486a-89B0-B4B0CB466891"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ICLRRuntimeInfo
{
void GetVersionString(char[] buffer, int bufferLength);
void GetRuntimeDirectory(char[] buffer, int bufferLength);
bool IsLoaded(IntPtr hProcess);
void LoadErrorString(uint id, char[] buffer, int bufferLength, int lcid);
void LoadLibrary(string path, out IntPtr hMdodule);
void GetProcAddress(string name, out IntPtr addr);
[return: MarshalAs(UnmanagedType.Interface)]
object GetInterface(ref Guid clsid, ref Guid iid);
bool IsLoadable();
void SetDefaultStartupFlags(uint flags, string configFile);
void GetDefaultStartupFlags(out uint flags, char[] configFile, int configFileLength);
void BindAsLegacyV2Runtime();
void IsStarted(out bool started, out uint flags);
}

[ComImport, Guid("CB2F6722-AB3A-11d2-9C40-00C04FA30A3E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ICorRuntimeHost
{
void CreateLogicalThreadState();
void DeleteLogicalThreadState();
void SwitchinLogicalThreadState(IntPtr cookie);
void SwitchoutLogicalThreadState(out IntPtr cookie);
void LocksHeldByLogicalThread(out int count);
void MapFile(IntPtr hFile, out IntPtr address);
void GetConfiguration(out IntPtr config);
void Start();
void Stop();
void CreateDomain(string name, object identity, out _AppDomain domain);
void GetDefaultDomain(out _AppDomain domain);
void EnumDomains(out IntPtr hEnum);
void NextDomain(IntPtr hEnum, out _AppDomain domain);
void CloseEnum(IntPtr hEnum);
// rest omitted
}
}
}

0 comments on commit 5b4487f

Please sign in to comment.