-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathServiceHostExtensionsFixture.cs
109 lines (91 loc) · 4.18 KB
/
ServiceHostExtensionsFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.ServiceModel;
namespace Autofac.Integration.Wcf.Test;
public class ServiceHostExtensionsFixture
{
[Fact]
public void AddDependencyInjectionBehavior_NullContractType_ThrowsException()
{
var serviceHost = new ServiceHost(typeof(ServiceType));
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => serviceHost.AddDependencyInjectionBehavior(null, new ContainerBuilder().Build()));
Assert.Equal("contractType", exception.ParamName);
}
[Fact]
public void AddDependencyInjectionBehavior_NullContainer_ThrowsException()
{
var serviceHost = new ServiceHost(typeof(ServiceType));
var exception = Assert.Throws<ArgumentNullException>(
() => serviceHost.AddDependencyInjectionBehavior(typeof(IContractType), null));
Assert.Equal("container", exception.ParamName);
}
[Fact]
public void AddDependencyInjectionBehavior_NullParameters_ThrowsException()
{
var serviceHost = new ServiceHost(typeof(ServiceType));
var exception = Assert.Throws<ArgumentNullException>(
() => serviceHost.AddDependencyInjectionBehavior(typeof(IContractType), new ContainerBuilder().Build(), null));
Assert.Equal("parameters", exception.ParamName);
}
[Fact]
public void AddDependencyInjectionBehavior_ContractTypeNotRegistered_ThrowsException()
{
var serviceHost = new ServiceHost(typeof(ServiceType));
var contractType = typeof(IContractType);
var exception = Assert.Throws<ArgumentException>(
() => serviceHost.AddDependencyInjectionBehavior(contractType, new ContainerBuilder().Build()));
Assert.Equal("contractType", exception.ParamName);
var message = string.Format(ServiceHostExtensionsResources.ContractTypeNotRegistered, contractType.FullName);
Assert.Contains(message, exception.Message);
}
[Fact]
public void AddDependencyInjectionBehavior_ContractTypeRegistered_ServiceBehaviorConfigured()
{
var builder = new ContainerBuilder();
builder.Register(c => new ServiceType()).As<IContractType>();
IContainer container = builder.Build();
var serviceHost = new ServiceHost(typeof(ServiceType));
serviceHost.AddDependencyInjectionBehavior(typeof(IContractType), container);
var serviceBehaviorCount = serviceHost.Description.Behaviors
.OfType<AutofacDependencyInjectionServiceBehavior>()
.Count();
Assert.Equal(1, serviceBehaviorCount);
}
[Fact]
public void AddDependencyInjectionBehavior_SingleInstanceContextMode_ServiceBehaviorIgnored()
{
var builder = new ContainerBuilder();
builder.Register(c => new SingletonServiceType()).As<IContractType>();
var container = builder.Build();
var serviceHost = new ServiceHost(typeof(SingletonServiceType));
serviceHost.AddDependencyInjectionBehavior(typeof(IContractType), container);
var serviceBehaviorCount = serviceHost.Description.Behaviors
.OfType<AutofacDependencyInjectionServiceBehavior>()
.Count();
Assert.Equal(0, serviceBehaviorCount);
}
[Fact]
public void AddDependencyInjectionBehaviorWithGenericArgument_ContractTypeRegistered_ServiceBehaviorConfigured()
{
var builder = new ContainerBuilder();
builder.Register(c => new ServiceType()).As<IContractType>();
var container = builder.Build();
var serviceHost = new ServiceHost(typeof(ServiceType));
serviceHost.AddDependencyInjectionBehavior<IContractType>(container);
var serviceBehaviorCount = serviceHost.Description.Behaviors
.OfType<AutofacDependencyInjectionServiceBehavior>()
.Count();
Assert.Equal(1, serviceBehaviorCount);
}
internal interface IContractType
{
}
internal class ServiceType : IContractType
{
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
internal class SingletonServiceType : IContractType
{
}
}