-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInstanceTestFixture.cs
82 lines (73 loc) · 2.51 KB
/
InstanceTestFixture.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
using Solid.AspNetCore.Extensions.Wcf.Tests.Abstractions;
using Solid.AspNetCore.Extensions.Wcf.Tests.Host;
using Solid.Testing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Solid.AspNetCore.Extensions.Wcf.Tests
{
public class InstanceTestFixture : IDisposable
{
private IInstanceTestService _perCall;
private IInstanceTestService _singleton;
public InstanceTestFixture()
{
TestingServer = new TestingServerBuilder()
.AddAspNetCoreHostFactory()
.AddStartup<InstanaceTestStartup>()
.Build();
}
public TestingServer TestingServer { get; }
public IInstanceTestService GetPerCallService()
{
if (_perCall == null)
{
var url = new Uri(TestingServer.BaseAddress, "percall");
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress(url);
var factory = new ChannelFactory<IInstanceTestService>(binding, endpoint);
var client = factory.CreateChannel();
_perCall = client;
var channel = client as ICommunicationObject;
channel.Closed += (sender, args) =>
{
_perCall = null;
};
}
return _perCall;
}
public IInstanceTestService GetSingletonService()
{
if (_singleton == null)
{
var url = new Uri(TestingServer.BaseAddress, "singleton");
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress(url);
var factory = new ChannelFactory<IInstanceTestService>(binding, endpoint);
var client = factory.CreateChannel();
_singleton = client;
var channel = client as ICommunicationObject;
channel.Closed += (sender, args) =>
{
_singleton = null;
};
}
return _singleton;
}
public void Dispose()
{
Close(_perCall);
Close(_singleton);
TestingServer.Dispose();
}
private void Close(object service)
{
var channel = service as ICommunicationObject;
if (channel != null)
channel.Close();
}
}
}