Skip to content

Commit

Permalink
Allow internal service types to be registered if internals are visibl…
Browse files Browse the repository at this point in the history
…e to DynamicProxyGenAssembly2.
  • Loading branch information
alexmg committed Apr 12, 2017
1 parent 9d6770e commit 1de5b64
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 17 deletions.
10 changes: 8 additions & 2 deletions Autofac.ServiceFabric.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio 15
VisualStudioVersion = 15.0.26403.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Autofac.Integration.ServiceFabric", "src\Autofac.Integration.ServiceFabric\Autofac.Integration.ServiceFabric.csproj", "{6956F17D-A87B-4FAB-8CA8-0A6AB8A14537}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Autofac.Integration.ServiceFabric.Test", "test\Autofac.Integration.ServiceFabric.Test\Autofac.Integration.ServiceFabric.Test.csproj", "{A3183165-EEDC-4177-A72C-2B9E8D5BFFA0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Scenario.InternalsVisible", "test\Test.Scenario.InternalsVisible\Test.Scenario.InternalsVisible.csproj", "{1707F51B-7E92-4905-BA3A-AFE26A12B706}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{A3183165-EEDC-4177-A72C-2B9E8D5BFFA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3183165-EEDC-4177-A72C-2B9E8D5BFFA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3183165-EEDC-4177-A72C-2B9E8D5BFFA0}.Release|Any CPU.Build.0 = Release|Any CPU
{1707F51B-7E92-4905-BA3A-AFE26A12B706}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1707F51B-7E92-4905-BA3A-AFE26A12B706}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1707F51B-7E92-4905-BA3A-AFE26A12B706}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1707F51B-7E92-4905-BA3A-AFE26A12B706}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions CodeAnalysisDictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Word>startable</Word>
<Word>Owin</Word>
<Word>Stateful</Word>
<Word>proxied</Word>
</Recognized>
<!--
<Deprecated>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<Compile Include="RegistrationExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TypeExtensions.cs" />
<Compile Include="TypeExtensionsResources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>TypeExtensionsResources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="app.config">
Expand All @@ -133,6 +138,12 @@
<Link>CodeAnalysisDictionary.xml</Link>
</CodeAnalysisDictionary>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TypeExtensionsResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TypeExtensionsResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\packages\Microsoft.ServiceFabric.Actors.2.4.164\build\Microsoft.ServiceFabric.Actors.targets" Condition="Exists('..\..\packages\Microsoft.ServiceFabric.Actors.2.4.164\build\Microsoft.ServiceFabric.Actors.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void RegisterActor<TActor>(this ContainerBuilder builder) where TA
var actorType = typeof(TActor);

if (!actorType.CanBeProxied())
throw new ArgumentException(actorType.GetInvalidForProxyErrorMessage());
throw new ArgumentException(actorType.GetInvalidProxyTypeErrorMessage());

builder.RegisterServiceWithInterception<TActor, ActorInterceptor>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static void RegisterServiceWithContainer<TService>(ContainerBuilder buil
var serviceType = typeof(TService);

if (!serviceType.CanBeProxied())
throw new ArgumentException(serviceType.GetInvalidForProxyErrorMessage());
throw new ArgumentException(serviceType.GetInvalidProxyTypeErrorMessage());

builder.RegisterServiceWithInterception<TService, ServiceInterceptor>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Castle.Core.Internal;

[assembly: AssemblyTitle("Autofac.Integration.ServiceFabric")]
[assembly: ComVisible(false)]
Expand All @@ -23,6 +24,6 @@
[assembly: AssemblyDescription("Autofac Service Fabric Integration")]

[assembly: InternalsVisibleTo("Autofac.Integration.ServiceFabric.Test, PublicKey=00240000048000009400000006020000002400005253413100040000010001008728425885ef385e049261b18878327dfaaf0d666dea3bd2b0e4f18b33929ad4e5fbc9087e7eda3c1291d2de579206d9b4292456abffbe8be6c7060b36da0c33b883e3878eaf7c89fddf29e6e27d24588e81e86f3a22dd7b1a296b5f06fbfb500bbd7410faa7213ef4e2ce7622aefc03169b0324bcd30ccfe9ac8204e4960be6")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]

[assembly: SuppressMessage("Microsoft.Design", "CA1020", Scope = "namespace", Target = "Autofac.Integration.ServiceFabric")]
12 changes: 6 additions & 6 deletions src/Autofac.Integration.ServiceFabric/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@

using System;
using System.Globalization;
using Castle.DynamicProxy.Internal;

namespace Autofac.Integration.ServiceFabric
{
internal static class TypeExtensions
{
internal static bool CanBeProxied(this Type type)
{
return type.IsClass && type.IsPublic && !type.IsSealed && !type.IsAbstract;
var open = type.IsClass && !type.IsSealed && !type.IsAbstract;
var visible = type.IsPublic || type.Assembly.IsInternalToDynamicProxy();
return open && visible;
}

internal static string GetInvalidForProxyErrorMessage(this Type type)
internal static string GetInvalidProxyTypeErrorMessage(this Type type)
{
return string.Format(
CultureInfo.CurrentCulture,
"The type {0} cannot be dynamically proxied. Please ensure the class is public and not sealed.",
type.FullName);
return string.Format(CultureInfo.CurrentCulture, TypeExtensionsResources.InvalidProxyTypeErrorMessage, type.FullName);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions src/Autofac.Integration.ServiceFabric/TypeExtensionsResources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="InvalidProxyTypeErrorMessage" xml:space="preserve">
<value>The type {0} cannot be dynamically proxied. Service types must not be sealed and need to be visible to the DynamicProxyGenAssembly2 assembly. This can be achieved by making the type public or adding the InternalsVisibleToAttribute to the assembly containing the type. e.g. [assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@
<Project>{6956f17d-a87b-4fab-8ca8-0a6ab8a14537}</Project>
<Name>Autofac.Integration.ServiceFabric</Name>
</ProjectReference>
<ProjectReference Include="..\Test.Scenario.InternalsVisible\Test.Scenario.InternalsVisible.csproj">
<Project>{1707f51b-7e92-4905-ba3a-afe26a12b706}</Project>
<Name>Test.Scenario.InternalsVisible</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.ServiceFabric.Actors;
using Microsoft.ServiceFabric.Actors.Runtime;
using Moq;
using Test.Scenario.InternalsVisible;
using Xunit;

namespace Autofac.Integration.ServiceFabric.Test
Expand Down Expand Up @@ -66,6 +67,18 @@ public void RegisterActorAddsFactoryCallback()
factoryMock.Verify(x => x.RegisterActorFactory<Actor1>(container), Times.Once);
}

[Fact]
public void RegisterActorSupportsInternalsVisibleToDynamicProxyGenAssembly2()
{
var builder = new ContainerBuilder();
builder.RegisterActor<InternalsVisibleActor>();
builder.RegisterInstance(new Mock<IActorFactoryRegistration>().Object);

var container = builder.Build();

container.AssertRegistered<InternalsVisibleActor>();
}

[Fact]
public void RegisterActorThrowsIfProvidedBuilderIsNull()
{
Expand All @@ -81,7 +94,7 @@ public void RegisterActorThrowsIfProvidedTypeIsSealed()

var exception = Assert.Throws<ArgumentException>(() => builder.RegisterActor<SealedActor>());

Assert.Equal(typeof(SealedActor).GetInvalidForProxyErrorMessage(), exception.Message);
Assert.Equal(typeof(SealedActor).GetInvalidProxyTypeErrorMessage(), exception.Message);
}

[Fact]
Expand All @@ -91,7 +104,7 @@ public void RegisterActorThrowsIfProvidedTypeIsNotPublic()

var exception = Assert.Throws<ArgumentException>(() => builder.RegisterActor<InternalActor>());

Assert.Equal(typeof(InternalActor).GetInvalidForProxyErrorMessage(), exception.Message);
Assert.Equal(typeof(InternalActor).GetInvalidProxyTypeErrorMessage(), exception.Message);
}
}

Expand Down
Loading

0 comments on commit 1de5b64

Please sign in to comment.