本文整理汇总了C#中LinFu.IoC.ServiceContainer.GetService方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.GetService方法的具体用法?C# ServiceContainer.GetService怎么用?C# ServiceContainer.GetService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinFu.IoC.ServiceContainer
的用法示例。
在下文中一共展示了ServiceContainer.GetService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScopeShouldNeverCallDisposableOnScopedObjectCreatedInAnotherThread
public void ScopeShouldNeverCallDisposableOnScopedObjectCreatedInAnotherThread()
{
var mock = new Mock<IDisposable>();
var container = new ServiceContainer();
container.AddService(mock.Object);
using (var scope = container.GetService<IScope>())
{
var signal = new ManualResetEvent(false);
WaitCallback callback = state =>
{
// Create the service instance
var instance = container.GetService<IDisposable>();
signal.Set();
};
ThreadPool.QueueUserWorkItem(callback);
// Wait for the thread to execute
WaitHandle.WaitAny(new WaitHandle[] {signal});
}
// The instance should never be disposed
}
示例2: ScopeShouldCallDisposableOnScopedObject
public void ScopeShouldCallDisposableOnScopedObject()
{
var mock = new Mock<IDisposable>();
mock.Expect(disposable => disposable.Dispose());
var container = new ServiceContainer();
container.AddService(mock.Object);
using (var scope = container.GetService<IScope>())
{
// Create the service instance
var instance = container.GetService<IDisposable>();
}
}
示例3: ScopeShouldNeverCallDisposableOnNonScopedObject
public void ScopeShouldNeverCallDisposableOnNonScopedObject()
{
var mock = new Mock<IDisposable>();
var container = new ServiceContainer();
container.AddService(mock.Object);
using (var scope = container.GetService<IScope>())
{
}
// Create the service instance OUTSIDE the scope
// Note: this should never be disposed
var instance = container.GetService<IDisposable>();
}
示例4: ShouldLoadAssemblyIntoLoaderAtRuntime
public void ShouldLoadAssemblyIntoLoaderAtRuntime()
{
string path = Path.Combine(@"..\..\..\SampleFileWatcherLibrary\bin\Debug",
AppDomain.CurrentDomain.BaseDirectory);
string targetFile = "SampleFileWatcherLibrary.dll";
string sourceFileName = Path.Combine(path, targetFile);
var container = new ServiceContainer();
container.AutoLoadFrom(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");
// There should be nothing loaded at this point since the assembly hasn't
// been copied to the target directory yet
Assert.IsFalse(container.Contains(typeof (ISampleService)));
// Copy the assembly to the target directory
// and watch for changes
string targetFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");
File.Copy(sourceFileName, targetFileName, true);
// Give the watcher thread enough time to load the assembly into memory
Thread.Sleep(500);
Assert.IsTrue(container.Contains(typeof (ISampleService)));
var instance = container.GetService<ISampleService>();
Assert.IsNotNull(instance);
string typeName = instance.GetType().Name;
Assert.AreEqual("SampleFileWatcherServiceClassAddedAtRuntime", typeName);
}
示例5: TestPropertyInjection
private static void TestPropertyInjection(string serviceName)
{
var mockTarget = new Mock<IInjectionTarget>();
mockTarget.Expect(t => t.SetValue(123));
var target = mockTarget.Object;
var container = new ServiceContainer();
container.AddService(serviceName, target);
// Use the named fluent interface for
// named instances
if (!String.IsNullOrEmpty(serviceName))
{
container.Initialize<IInjectionTarget>(serviceName)
.With(service => service.SetValue(123));
}
else
{
// Otherwise, use the other one
container.Initialize<IInjectionTarget>()
.With(service => service.SetValue(123));
}
var result = container.GetService<IInjectionTarget>(serviceName);
Assert.IsNotNull(result);
// The container should initialize the
// service on every request
mockTarget.VerifyAll();
}
示例6: Main
static void Main(string[] args)
{
string directory = AppDomain.CurrentDomain.BaseDirectory;
IServiceContainer container = new ServiceContainer();
// Load CarLibrary3.dll; If you need load
// all the libaries in a directory, use "*.dll" instead
container.LoadFrom(directory, "CarLibrary3.dll");
// Configure the container inject instances
// into the Car class constructor
container.Inject<IVehicle>()
.Using(ioc => new Car(ioc.GetService<IEngine>(),
ioc.GetService<IPerson>()))
.OncePerRequest();
Person person = new Person();
person.Name = "Someone";
person.Age = 18;
container.AddService<IPerson>(person);
IVehicle vehicle = container.GetService<IVehicle>();
vehicle.Move();
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}
示例7: ShouldCallStronglyTypedFunctorInsteadOfActualFactory
public void ShouldCallStronglyTypedFunctorInsteadOfActualFactory()
{
var container = new ServiceContainer();
Func<int, int, int> addOperation1 = (a, b) => a + b;
container.AddService("Add", addOperation1);
Func<int, int, int, int> addOperation2 = (a, b, c) => a + b + c;
container.AddService("Add", addOperation2);
Func<int, int, int, int, int> addOperation3 = (a, b, c, d) => a + b + c + d;
container.AddService("Add", addOperation3);
Assert.AreEqual(2, container.GetService<int>("Add", 1, 1));
Assert.AreEqual(3, container.GetService<int>("Add", 1, 1, 1));
Assert.AreEqual(4, container.GetService<int>("Add", 1, 1, 1, 1));
}
示例8: AroundInvokeClassesMarkedWithInterceptorAttributeMustGetActualTargetInstance
public void AroundInvokeClassesMarkedWithInterceptorAttributeMustGetActualTargetInstance()
{
var container = new ServiceContainer();
container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var mockService = new Mock<ISampleWrappedInterface>();
mockService.Expect(mock => mock.DoSomething());
// Add the target instance
container.AddService(mockService.Object);
// The service must return a proxy
var service = container.GetService<ISampleWrappedInterface>();
Assert.AreNotSame(service, mockService.Object);
// Execute the method and 'catch' the target instance once the method call is made
service.DoSomething();
var holder = container.GetService<ITargetHolder>("SampleAroundInvokeInterceptorClass");
Assert.AreSame(holder.Target, mockService.Object);
}
示例9: LinFu_Container_Allow_Wildcard_Matching
public void LinFu_Container_Allow_Wildcard_Matching()
{
var container = new ServiceContainer();
container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c =>
{
c.IncludeNamespace("SnapTests.*");
c.Bind<HandleErrorInterceptor>().To<HandleErrorAttribute>();
});
Assert.DoesNotThrow(() => container.GetService<IBadCode>());
}
示例10: ShouldFindGenericMethod
public void ShouldFindGenericMethod()
{
var container = new ServiceContainer();
container.LoadFromBaseDirectory("*.dll");
var context = new MethodFinderContext(new Type[]{typeof(object)}, new object[0], typeof(void));
var methods = typeof(SampleClassWithGenericMethod).GetMethods(BindingFlags.Public | BindingFlags.Instance);
var finder = container.GetService<IMethodFinder<MethodInfo>>();
var result = finder.GetBestMatch(methods, context);
Assert.IsTrue(result.IsGenericMethod);
Assert.IsTrue(result.GetGenericArguments().Count() == 1);
}
示例11: Main
static void Main(string[] args)
{
string directory = AppDomain.CurrentDomain.BaseDirectory;
var container = new ServiceContainer();
// Load CarLibrary.dll; If you need load
// all the libaries in a directory, use "*.dll" instead
container.LoadFrom(directory, "CarLibrary.dll");
IVehicle vehicle = container.GetService<IVehicle>();
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
return;
}
示例12: ContainerMustAllowInjectingCustomFactoriesForOpenGenericTypeDefinitions
public void ContainerMustAllowInjectingCustomFactoriesForOpenGenericTypeDefinitions()
{
var container = new ServiceContainer();
var factory = new SampleOpenGenericFactory();
container.AddFactory(typeof (ISampleGenericService<>), factory);
// The container must report that it *can* create
// the generic service type
Assert.IsTrue(container.Contains(typeof (ISampleGenericService<int>)));
var result = container.GetService<ISampleGenericService<int>>();
Assert.IsNotNull(result);
Assert.IsTrue(result.GetType() == typeof (SampleGenericImplementation<int>));
}
示例13: ShouldAutoInjectMethod
public void ShouldAutoInjectMethod()
{
var container = new ServiceContainer();
container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
var instance = new SampleClassWithInjectionMethod();
// Initialize the container
container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest();
container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest();
var result = container.GetService<ISampleService>("MyService");
Assert.AreSame(result, instance);
// On initialization, the instance.Property value
// should be a SampleClass type
Assert.IsNotNull(instance.Property);
Assert.IsInstanceOfType(typeof(SampleClass), instance.Property);
}
示例14: LazyObjectShouldNeverBeInitialized
public void LazyObjectShouldNeverBeInitialized()
{
var container = new ServiceContainer();
container.AddService<IProxyFactory>(new ProxyFactory());
container.AddService<IMethodBuilder<MethodInfo>>(new MethodBuilder());
Assert.IsTrue(container.Contains(typeof (IProxyFactory)));
var proxyFactory = container.GetService<IProxyFactory>();
var interceptor = new LazyInterceptor<ISampleService>(() => new SampleLazyService());
SampleLazyService.Reset();
// The instance should be uninitialized at this point
var proxy = proxyFactory.CreateProxy<ISampleService>(interceptor);
Assert.IsFalse(SampleLazyService.IsInitialized);
// The instance should be initialized once the method is called
proxy.DoSomething();
Assert.IsTrue(SampleLazyService.IsInitialized);
}
示例15: Main
static void Main(string[] args)
{
string directory = AppDomain.CurrentDomain.BaseDirectory;
IServiceContainer container = new ServiceContainer();
// Load CarLibrary2.dll; If you need load
// all the libaries in a directory, use "*.dll" instead
container.LoadFrom(directory, "CarLibrary2.dll");
Person person = new Person();
person.Name = "Someone";
person.Age = 18;
container.AddService<IPerson>(person);
IVehicle vehicle = container.GetService<IVehicle>();
vehicle.Move();
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}