Skip to content

Commit

Permalink
Bug correction in RootHandlerEmbeddedResource when Assembly default n…
Browse files Browse the repository at this point in the history
…amespace contained "."

See note : Assembly default namespace must always correspond to Assembly name in order to get the resources.
  • Loading branch information
P-H-Maillefer committed Oct 3, 2017
1 parent 0f337ec commit 30ae0ee
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 30 deletions.
21 changes: 20 additions & 1 deletion Sources/DotNet/Woopsa.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Woopsa", "Woopsa\Woopsa.csproj", "{B6D4DB14-CD89-4211-8238-A8988DFB9B81}"
EndProject
Expand All @@ -11,6 +11,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WoopsaDemoServer", "WoopsaD
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WoopsaDemoClient", "WoopsaDemoClient\WoopsaDemoClient.csproj", "{7DCE84B1-1A2D-44AE-A875-FB6DABEF55D8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{522A6CC9-D434-4902-980C-63BC310A18D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmbeddedResource", "WoopsaTest\TestResources\EmbeddedResource\EmbeddedResource.csproj", "{B30437A0-BA1C-42AA-A40D-60DC3689016A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Woopsa.EmbeddedddResource", "WoopsaTest\TestResources\Woopsa.EmbeddedResource\Woopsa.EmbeddedddResource.csproj", "{512E7B24-3D31-4DD5-A7E9-BF644A6DC06F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,8 +39,21 @@ Global
{7DCE84B1-1A2D-44AE-A875-FB6DABEF55D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DCE84B1-1A2D-44AE-A875-FB6DABEF55D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DCE84B1-1A2D-44AE-A875-FB6DABEF55D8}.Release|Any CPU.Build.0 = Release|Any CPU
{B30437A0-BA1C-42AA-A40D-60DC3689016A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B30437A0-BA1C-42AA-A40D-60DC3689016A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B30437A0-BA1C-42AA-A40D-60DC3689016A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B30437A0-BA1C-42AA-A40D-60DC3689016A}.Release|Any CPU.Build.0 = Release|Any CPU
{512E7B24-3D31-4DD5-A7E9-BF644A6DC06F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{512E7B24-3D31-4DD5-A7E9-BF644A6DC06F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{512E7B24-3D31-4DD5-A7E9-BF644A6DC06F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{512E7B24-3D31-4DD5-A7E9-BF644A6DC06F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8CB57ED5-1B97-4467-9887-AD34488DBBC6} = {522A6CC9-D434-4902-980C-63BC310A18D2}
{B30437A0-BA1C-42AA-A40D-60DC3689016A} = {522A6CC9-D434-4902-980C-63BC310A18D2}
{512E7B24-3D31-4DD5-A7E9-BF644A6DC06F} = {522A6CC9-D434-4902-980C-63BC310A18D2}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Woopsa
{
/// <summary>
/// Note : To use RouteHandlerEmbeddedResources the assembly default namespace must correspond to the assembly name.
/// </summary>
public class RouteHandlerEmbeddedResources : IHTTPRouteHandler
{
/// <summary>
Expand All @@ -27,8 +30,6 @@ public RouteHandlerEmbeddedResources(string resourcePathInAssembly = "", Assembl
else
_baseDirectory = "";
_assembly = assembly;
_resourceNameByStrippedResourceNameByAssembly =
new Dictionary<Assembly, Dictionary<string, string>>();
}

#region IHTTPRouteHandler
Expand Down Expand Up @@ -122,31 +123,7 @@ private bool ServeEmbeddedResource(HTTPResponse response, Assembly assembly,

private string FullResourceName(Assembly assembly, string strippedResourceName)
{
Dictionary<string, string> resourceNameByStrippedResourceName;
string result;
if (!_resourceNameByStrippedResourceNameByAssembly.TryGetValue(
assembly, out resourceNameByStrippedResourceName))
{
resourceNameByStrippedResourceName = new Dictionary<string, string>();
_resourceNameByStrippedResourceNameByAssembly[assembly] =
resourceNameByStrippedResourceName;
}
if (!resourceNameByStrippedResourceName.TryGetValue(strippedResourceName, out result))
{
if (resourceNameByStrippedResourceName.Count == 0)
// Populate the dictionary
foreach (var item in assembly.GetManifestResourceNames())
{
string[] resourceNameParts = item.Split(new char[] { ResourcePathSeparator }, 2);
if (resourceNameParts.Length == 2)
{
resourceNameByStrippedResourceName[resourceNameParts[1]] = item;
if (resourceNameParts[1] == strippedResourceName)
result = item;
}
}
}
return result;
return string.Format("{0}.{1}", assembly.GetName().Name, strippedResourceName);
}

private Assembly AssemblyByName(string name)
Expand All @@ -173,8 +150,6 @@ private Assembly AssemblyByName(string name)
private string _baseDirectory;
private Assembly _assembly;
private Dictionary<string, Assembly> _assemblyByName;
private Dictionary<Assembly, Dictionary<string, string>>
_resourceNameByStrippedResourceNameByAssembly;
}

}
12 changes: 12 additions & 0 deletions Sources/DotNet/WoopsaTest/TestResources/EmbeddedResource/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmbeddedResource
{
public class Class1
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B30437A0-BA1C-42AA-A40D-60DC3689016A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EmbeddedResource</RootNamespace>
<AssemblyName>EmbeddedResource</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Images\woopsa-logo.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Images\SubImages\woopsa-logo.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("EmbeddedResource")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("EmbeddedResource")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b30437a0-ba1c-42aa-a40d-60dc3689016a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Woopsa.EmbeddedResource
{
public class Class1
{
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Woopsa.EmbeddedResource")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Woopsa.EmbeddedResource")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("512e7b24-3d31-4dd5-a7e9-bf644a6dc06f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{512E7B24-3D31-4DD5-A7E9-BF644A6DC06F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Woopsa.EmbeddedResource</RootNamespace>
<AssemblyName>Woopsa.EmbeddedResource</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Images\SubImages\woopsa-logo.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Images\woopsa-logo.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
43 changes: 43 additions & 0 deletions Sources/DotNet/WoopsaTest/UnitTestHttpServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Http;
using Woopsa;

namespace WoopsaTest
{
[TestClass]
public class UnitTestHttpServer
{
[TestMethod]
public void TestRouteHandlerEmbeddedResource()
{
WoopsaRoot serverRoot = new WoopsaRoot();
TestObjectServer objectServer = new TestObjectServer();
WoopsaObjectAdapter adapter = new WoopsaObjectAdapter(serverRoot, "TestObject", objectServer);
using (WoopsaServer server = new WoopsaServer(serverRoot))
{
var test1 = new EmbeddedResource.Class1(); // Force to load assembly
var test2 = new Woopsa.EmbeddedResource.Class1(); // Force to load assembly
var routeHandlerEmbeddedResources = new RouteHandlerEmbeddedResources();
server.WebServer.Routes.Add("resources", HTTPMethod.GET,
routeHandlerEmbeddedResources);
using (HttpClient client = new HttpClient())
{
var request = client.GetAsync("http://localhost/resources/EmbeddedResource/Images/woopsa-logo.png");
request.Wait();
Assert.IsTrue(request.Result.IsSuccessStatusCode);
request = client.GetAsync("http://localhost/resources/EmbeddedResource/Images/SubImages/woopsa-logo.png");
request.Wait();
Assert.IsTrue(request.Result.IsSuccessStatusCode);

request = client.GetAsync("http://localhost/resources/Woopsa.EmbeddedResource/Images/woopsa-logo.png");
request.Wait();
Assert.IsTrue(request.Result.IsSuccessStatusCode);

request = client.GetAsync("http://localhost/resources/Woopsa.EmbeddedResource/Images/SubImages/woopsa-logo.png");
request.Wait();
Assert.IsTrue(request.Result.IsSuccessStatusCode);
}
}
}
}
}
Loading

0 comments on commit 30ae0ee

Please sign in to comment.