Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Fix for publishing issue with RIDs #3182

Merged
merged 4 commits into from
Nov 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions misc/PublishRuntimeDependency/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Data.SqlClient;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Reflection;
using System.Text;
using System.Threading;

namespace PublishRuntimeDependency
{
public class Program
{
public void Main(string[] args)
{
RunTest("System.Net.Security", TestSslStream);
RunTest("System.Net.NetworkInformation", TestNetworkInformation);
RunTest("System.Net.WebSockets.Client", TestWebSockets);
RunTest("System.Data.SqlClient", TestSqlClient);
RunTest("System.Reflection.DispatchProxy", TestDispatchProxy);
}

public interface ITestInterface
{
void DoTheThing();
}
public class TestProxy : DispatchProxy
{
public bool WasCalled { get; private set; }

protected override object Invoke(MethodInfo targetMethod, object[] args)
{
WasCalled = true;
Console.WriteLine($"Intercepted: {targetMethod.Name} with dispatch proxy");
return null;
}
}

private void TestDispatchProxy()
{
var proxy = DispatchProxy.Create<ITestInterface, TestProxy>();
proxy.DoTheThing();

if (!((TestProxy)proxy).WasCalled)
{
throw new Exception("The proxy was not called!");
}
Console.WriteLine("The proxy was called");
}

private void TestSqlClient()
{
// Don't want to take a dependency on Sql Server so we just force loading by constructing a type
var con = new SqlConnection();
Console.WriteLine("Successfully loaded System.Data.SqlClient");
}

private void RunTest(string name, Action act)
{
Console.WriteLine();
Console.WriteLine($"=== Testing {name} ===");
try
{
act();
}
catch (Exception ex)
{
Console.WriteLine("FAILED:");
Console.WriteLine(ex.ToString());
}
}

private void TestWebSockets()
{
var socket = new ClientWebSocket();
Console.WriteLine("Connecting");
socket.ConnectAsync(new Uri("wss://echo.websocket.org"), CancellationToken.None).Wait();

Console.WriteLine("Sending");
socket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes("Hello")), WebSocketMessageType.Text, true, CancellationToken.None).Wait();

var buffer = new byte[1024];
Console.WriteLine("Receiving");
var result = socket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).Result;

Console.WriteLine($"Recieved: {Encoding.UTF8.GetString(buffer, 0, result.Count)}");
}

private void TestNetworkInformation()
{
foreach (var iface in NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine($"Network Interface: {iface.Id} {iface.Name} ({iface.NetworkInterfaceType})");
}
}

public void TestSslStream()
{
// Make a simple HTTP request
var url = new Uri("https://www.microsoft.com");

var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
socket.Connect(url.Host, url.Port);

using (var stream = GetStream(socket, url))
{
// Send the request
var request = $"GET {url.PathAndQuery} HTTP/1.1\r\nHost: {url.Host}\r\nConnection: close\r\n\r\n";
var buffer = Encoding.UTF8.GetBytes(request);
stream.Write(buffer, 0, buffer.Length);

// Read the response
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}

private static Stream GetStream(Socket socket, Uri uri)
{
var stream = new NetworkStream(socket);
if (uri.Scheme.Equals("https"))
{
var ssl = new SslStream(stream);
ssl.AuthenticateAsClient(uri.Host);
return ssl;
}
return stream;
}
}
}
19 changes: 19 additions & 0 deletions misc/PublishRuntimeDependency/PublishRuntimeDependency.xproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>ca12dd80-9170-43b6-925a-1b5f94094cca</ProjectGuid>
<RootNamespace>PublishRuntimeDependency</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>

<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
19 changes: 19 additions & 0 deletions misc/PublishRuntimeDependency/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"dependencies": {
"Microsoft.NETCore.Platforms": "1.0.1-beta-23516",
"System.Console": "4.0.0-beta-23516",
"System.Net.Sockets": "4.1.0-beta-23516",
"System.Net.Security": "4.0.0-beta-23516",
"System.Runtime": "4.0.21-beta-23516",
"System.Net.NetworkInformation": "4.1.0-beta-23516",
"System.Net.WebSockets.Client": "4.0.0-beta-23516",
"System.Data.SqlClient": "4.0.0-beta-23516",
"System.Reflection.DispatchProxy": "4.0.1-beta-23516"
},
"commands": {
"PublishRuntimeDependency": "PublishRuntimeDependency"
},
"frameworks": {
"dnxcore50": { }
}
}
7 changes: 5 additions & 2 deletions src/Microsoft.Dnx.Tooling/Publish/DependencyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ namespace Microsoft.Dnx.Tooling.Publish
{
public class DependencyContext
{
public DependencyContext(Runtime.Project project, FrameworkName targetFramework)
public DependencyContext(Runtime.Project project, FrameworkName targetFramework, IEnumerable<string> runtimeIdentifiers)
{
var applicationHostContext = new ApplicationHostContext
{
Project = project,
TargetFramework = targetFramework
TargetFramework = targetFramework,
RuntimeIdentifiers = runtimeIdentifiers
};

ApplicationHostContext.Initialize(applicationHostContext);

FrameworkName = targetFramework;
LibraryManager = applicationHostContext.LibraryManager;
PackagesDirectory = applicationHostContext.PackagesDirectory;
RuntimeIdentifiers = runtimeIdentifiers;
}

public LibraryManager LibraryManager { get; set; }
public FrameworkName FrameworkName { get; set; }
public IEnumerable<string> RuntimeIdentifiers { get; set; }
public string PackagesDirectory { get; private set; }

public static FrameworkName SelectFrameworkNameForRuntime(IEnumerable<FrameworkName> availableFrameworks, FrameworkName currentFramework, string runtime)
Expand Down
39 changes: 26 additions & 13 deletions src/Microsoft.Dnx.Tooling/Publish/PublishManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public bool Publish()

var projectDir = project.ProjectDirectory;

var frameworkContexts = new Dictionary<FrameworkName, DependencyContext>();
var frameworkContexts = new Dictionary<Tuple<FrameworkName, string>, DependencyContext>();

var root = new PublishRoot(project, outputPath, _options.Reports)
{
Expand Down Expand Up @@ -134,11 +134,6 @@ public bool Publish()
var frameworkName = DependencyContext.SelectFrameworkNameForRuntime(
project.GetTargetFrameworks().Select(x => x.FrameworkName),
runtimeName);
foreach (var rid in DependencyContext.GetRuntimeIdentifiers(runtimeName))
{
root.RuntimeIdentifiers.Add(rid);
}

if (frameworkName == null)
{
_options.Reports.Error.WriteLine(
Expand Down Expand Up @@ -190,9 +185,18 @@ public bool Publish()
return false;
}

if (!frameworkContexts.ContainsKey(frameworkName))
if (!frameworkContexts.ContainsKey(Tuple.Create(frameworkName, string.Empty)))
{
frameworkContexts[frameworkName] = CreateDependencyContext(project, frameworkName);
frameworkContexts[Tuple.Create(frameworkName, string.Empty)] = CreateDependencyContext(project, frameworkName, Enumerable.Empty<string>());
}

foreach (var rid in DependencyContext.GetRuntimeIdentifiers(runtimeName))
{
root.RuntimeIdentifiers.Add(rid);
if (!frameworkContexts.ContainsKey(Tuple.Create(frameworkName, rid)))
{
frameworkContexts[Tuple.Create(frameworkName, rid)] = CreateDependencyContext(project, frameworkName, new[] { rid });
}
}
}

Expand Down Expand Up @@ -235,9 +239,18 @@ public bool Publish()

foreach (var framework in frameworksToPublish)
{
if (!frameworkContexts.ContainsKey(framework))
if (!frameworkContexts.ContainsKey(Tuple.Create(framework, string.Empty)))
{
frameworkContexts[Tuple.Create(framework, string.Empty)] = CreateDependencyContext(project, framework, Enumerable.Empty<string>());
}

foreach (var rid in RuntimeEnvironmentHelper.RuntimeEnvironment.GetDefaultRestoreRuntimes())
{
frameworkContexts[framework] = CreateDependencyContext(project, framework);
root.RuntimeIdentifiers.Add(rid);
if (!frameworkContexts.ContainsKey(Tuple.Create(framework, rid)))
{
frameworkContexts[Tuple.Create(framework, rid)] = CreateDependencyContext(project, framework, new[] { rid });
}
}
}
}
Expand Down Expand Up @@ -281,7 +294,7 @@ public bool Publish()
}
else if (library.Type == LibraryTypes.Package)
{
if (!root.Packages.Any(p => p.Library.Name == library.Identity.Name))
if (!root.Packages.Any(p => p.Library.Name == library.Identity.Name && p.Library.Version == library.Identity.Version))
{
root.Packages.Add(new PublishPackage((PackageDescription)library));
}
Expand Down Expand Up @@ -333,9 +346,9 @@ private bool TryAddRuntime(PublishRoot root, FrameworkName frameworkName, string
return true;
}

private DependencyContext CreateDependencyContext(Runtime.Project project, FrameworkName frameworkName)
private DependencyContext CreateDependencyContext(Runtime.Project project, FrameworkName frameworkName, IEnumerable<string> runtimeIdentifiers)
{
var dependencyContext = new DependencyContext(project, frameworkName);
var dependencyContext = new DependencyContext(project, frameworkName, runtimeIdentifiers);
return dependencyContext;
}

Expand Down