Skip to content

Commit

Permalink
add autofac support
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiningRush committed Dec 22, 2017
1 parent 218f3ff commit c6163b2
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 30 deletions.
45 changes: 44 additions & 1 deletion src/ServiceAnt.IocInstaller.Autofac/ServiceAntModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Autofac;
using ServiceAnt.Handler.Request;
using ServiceAnt.Infrastructure.Dependency;
using ServiceAnt.Request.Handler;
using ServiceAnt.Subscription;
using System;
using System.Collections.Generic;
Expand All @@ -12,9 +13,30 @@ namespace ServiceAnt.IocInstaller.Autofac
{
public class ServiceAntModule : Module
{
private static System.Reflection.Assembly[] _handlerAssemblies;

public ServiceAntModule(params System.Reflection.Assembly[] handlerAssemblies)
{
_handlerAssemblies = handlerAssemblies;
}

public static void RegisterHandlers(IComponentContext container)
{
foreach (var aHandlerAssembly in _handlerAssemblies)
{
var handlerTypes = aHandlerAssembly.GetTypes().Where(p => typeof(ServiceAnt.Handler.IHandler).IsAssignableFrom(p) && !p.IsInterface);

foreach (var aHandler in handlerTypes)
{
RegisterHandlerType(container, aHandler);
}
}
}

protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<InProcessServiceBus>().AsSelf().As<IServiceBus>().SingleInstance();
var serviceBus = InProcessServiceBus.Default;
builder.RegisterInstance(serviceBus).As<IServiceBus>();
builder.RegisterType<InMemorySubscriptionsManager>().AsSelf().As<ISubscriptionManager>().SingleInstance();
builder.RegisterType<InMemoryRequestHandlerManager>().AsSelf().As<IRequestHandlerManager>().SingleInstance();

Expand All @@ -23,6 +45,27 @@ protected override void Load(ContainerBuilder builder)
return new IocResolver(ctx.Resolve<IComponentContext>());
});

builder.RegisterAssemblyTypes(_handlerAssemblies).AsSelf();
}
private static void RegisterHandlerType(IComponentContext container, Type aHandlerType)
{
var interfaces = aHandlerType.GetInterfaces();
foreach (var aInterface in interfaces)
{
if (!typeof(ServiceAnt.Handler.IHandler).IsAssignableFrom(aInterface))
{
continue;
}

var genericArgs = aInterface.GetGenericArguments();
if (genericArgs.Length == 1)
{
if (typeof(IRequestHandler).IsAssignableFrom(aInterface))
container.Resolve<IServiceBus>().AddRequestHandler(genericArgs[0], new ServiceAnt.Handler.IocHandlerFactory(container.Resolve<IocResolver>(), aHandlerType, genericArgs[0]));
else
container.Resolve<IServiceBus>().AddSubScription(genericArgs[0], new ServiceAnt.Handler.IocHandlerFactory(container.Resolve<IocResolver>(), aHandlerType, genericArgs[0]));
}
}
}
}
}
17 changes: 14 additions & 3 deletions src/ServiceAnt.IocInstaller.Castle/ServiceAntInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,36 @@ namespace ServiceAnt.IocInstaller.Castle
{
public class ServiceAntInstaller : IWindsorInstaller
{
private readonly Assembly[] _handlerAssemblies;
private IWindsorContainer _container;
private IServiceBus _serviceBus;

public ServiceAntInstaller()
public ServiceAntInstaller(params Assembly[] handlerAssemblies)
{
_handlerAssemblies = handlerAssemblies;
}

public void Install(IWindsorContainer container, IConfigurationStore store)
{
_container = container;

container.Register(
Component.For<IServiceBus>().ImplementedBy<InProcessServiceBus>().LifestyleSingleton(),
Component.For<IServiceBus>().Instance(InProcessServiceBus.Default),
Component.For<ISubscriptionManager>().ImplementedBy<InMemorySubscriptionsManager>().LifestyleSingleton(),
Component.For<IRequestHandlerManager>().ImplementedBy<InMemoryRequestHandlerManager>().LifestyleSingleton());


_serviceBus = container.Resolve<IServiceBus>();
container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;

foreach (var aHandlerAssembly in _handlerAssemblies)
{
container.Register(Classes.FromAssembly(aHandlerAssembly)
.BasedOn<ServiceAnt.Handler.IHandler>()
.WithService.Self()
.LifestyleTransient());
}


}

private void Kernel_ComponentRegistered(string key, IHandler handler)
Expand Down
3 changes: 3 additions & 0 deletions src/ServiceAnt/InProcessServiceBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class InProcessServiceBus : IServiceBus

public event Action<string, string, Exception> OnLogBusMessage;

private static Lazy<InProcessServiceBus> _defaultInstance = new Lazy<InProcessServiceBus>();
public static InProcessServiceBus Default => _defaultInstance.Value;

public InProcessServiceBus()
{
_subcriptionManager = new InMemorySubscriptionsManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=4.6.2.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\packages\Autofac.4.6.2\lib\net45\Autofac.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\MSTest.TestFramework.1.1.18\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
Expand All @@ -46,14 +50,31 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="UnitTest1.cs" />
<Compile Include="ServiceAntModule_Test.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ServiceAnt.IocInstaller.Autofac\ServiceAnt.IocInstaller.Autofac.csproj">
<Project>{1fa5221c-9a54-4132-b7cf-cf7cc7a30e59}</Project>
<Name>ServiceAnt.IocInstaller.Autofac</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\ServiceAnt\ServiceAnt.csproj">
<Project>{fb6688e2-094a-4d25-9eea-85c3b7ccc4a2}</Project>
<Name>ServiceAnt</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
72 changes: 72 additions & 0 deletions test/ServiceAnt.IocInstaller.Autofac.Test/ServiceAntModule_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using Autofac;
using ServiceAnt.Handler.Subscription.Handler;
using ServiceAnt.Request.Handler;
using ServiceAnt.Handler;

namespace ServiceAnt.IocInstaller.Autofac.Test
{
[TestClass]
public class ServiceAntModule_Test
{
private static string RESULT_CONTAINER = "";

public ServiceAntModule_Test()
{
}

[TestMethod]
public async Task CanHandleEventByIocHandler()
{
var testValue = "HelloWorld";
var newContainer = new ContainerBuilder();
newContainer.RegisterModule(new ServiceAntModule(System.Reflection.Assembly.GetExecutingAssembly()));
var autofacContainer = newContainer.Build();
ServiceAntModule.RegisterHandlers(autofacContainer);

await autofacContainer.Resolve<IServiceBus>().Publish(new TestTray() { Result = testValue });

Assert.AreEqual(testValue, RESULT_CONTAINER);
}

[TestMethod]
public async Task CanHandleRequestByIocHandler()
{
var testValue = "HelloWorld2";
var newContainer = new ContainerBuilder();
newContainer.RegisterModule(new ServiceAntModule(System.Reflection.Assembly.GetExecutingAssembly()));
var autofacContainer = newContainer.Build();
ServiceAntModule.RegisterHandlers(autofacContainer);

var result = await autofacContainer.Resolve<IServiceBus>().SendAsync<string>(new TestTray() { Result = testValue });

Assert.AreEqual(testValue, result);
}

public class TestTray : TransportTray
{
public string Result { get; set; }
}

public class IocEventHandler : IEventHandler<TestTray>
{
public Task HandleAsync(TestTray param)
{
RESULT_CONTAINER = param.Result;

return Task.Delay(1);
}
}

public class IocRequestHandler : IRequestHandler<TestTray>
{
public Task HandleAsync(TestTray param, IRequestHandlerContext handlerContext)
{
handlerContext.Response = param.Result;
return Task.Delay(1);
}
}
}
}
14 changes: 0 additions & 14 deletions test/ServiceAnt.IocInstaller.Autofac.Test/UnitTest1.cs

This file was deleted.

1 change: 1 addition & 0 deletions test/ServiceAnt.IocInstaller.Autofac.Test/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="4.6.2" targetFramework="net452" />
<package id="MSTest.TestAdapter" version="1.1.18" targetFramework="net452" />
<package id="MSTest.TestFramework" version="1.1.18" targetFramework="net452" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public async Task CanHandleEventByIocHandler()
{
var testValue = "HelloWorld";
var newContainer = new WindsorContainer();
newContainer.Install(new ServiceAntInstaller());

newContainer.Register(Component.For<IocEventHandler>().LifestyleTransient());
newContainer.Install(new ServiceAntInstaller(System.Reflection.Assembly.GetExecutingAssembly()));

await newContainer.Resolve<IServiceBus>().Publish(new TestTray() { Result = testValue });

Expand All @@ -42,21 +40,19 @@ public async Task CanHandleRequestByIocHandler()
{
var testValue = "HelloWorld2";
var newContainer = new WindsorContainer();
newContainer.Install(new ServiceAntInstaller());

newContainer.Register(Component.For<IocRequestHandler>().LifestyleTransient());
newContainer.Install(new ServiceAntInstaller(System.Reflection.Assembly.GetExecutingAssembly()));

var result = await newContainer.Resolve<IServiceBus>().SendAsync<string>(new TestTray() { Result = testValue });

Assert.AreEqual(testValue, result);
}

internal class TestTray : TransportTray
public class TestTray : TransportTray
{
public string Result { get; set; }
}

internal class IocEventHandler : IEventHandler<TestTray>
public class IocEventHandler : IEventHandler<TestTray>
{
public Task HandleAsync(TestTray param)
{
Expand All @@ -66,7 +62,7 @@ public Task HandleAsync(TestTray param)
}
}

internal class IocRequestHandler : IRequestHandler<TestTray>
public class IocRequestHandler : IRequestHandler<TestTray>
{
public Task HandleAsync(TestTray param, IRequestHandlerContext handlerContext)
{
Expand Down
4 changes: 2 additions & 2 deletions test/ServiceAnt.test/InProcessEventBus_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private class TestEventDataT<T> : TransportTray<T>

#region EventTests
[TestMethod]
public void DynamicSubscription_ShouldTrigger()
public async Task DynamicSubscription_ShouldTrigger()
{
var eventBus = new InProcessServiceBus();
var result = "error";
Expand All @@ -49,7 +49,7 @@ public void DynamicSubscription_ShouldTrigger()
});

var testEventData = new TestEventData() { Msg = "success" };
eventBus.PublishSync(testEventData);
await eventBus.Publish(testEventData);

Assert.AreEqual(testEventData.Msg, result);
}
Expand Down

0 comments on commit c6163b2

Please sign in to comment.