|
| 1 | +using Solid.AspNetCore.Extensions.Wcf.Tests.Abstractions; |
| 2 | +using Solid.AspNetCore.Extensions.Wcf.Tests.Host; |
| 3 | +using Solid.Testing; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.ServiceModel; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Solid.AspNetCore.Extensions.Wcf.Tests |
| 12 | +{ |
| 13 | + public class MultipleContractTestFixture : IDisposable |
| 14 | + { |
| 15 | + private IEchoContract _echo; |
| 16 | + private ICounterContract _counter; |
| 17 | + |
| 18 | + public MultipleContractTestFixture() |
| 19 | + { |
| 20 | + TestingServer = new TestingServerBuilder() |
| 21 | + .AddAspNetCoreHostFactory() |
| 22 | + .AddStartup<MulitpleContractTestStartup>() |
| 23 | + .Build(); |
| 24 | + } |
| 25 | + |
| 26 | + public TestingServer TestingServer { get; } |
| 27 | + |
| 28 | + public IEchoContract GetEchoService() |
| 29 | + { |
| 30 | + if (_echo == null) |
| 31 | + { |
| 32 | + var url = new Uri(TestingServer.BaseAddress, "multiple/echo"); |
| 33 | + var binding = new BasicHttpBinding(); |
| 34 | + var endpoint = new EndpointAddress(url); |
| 35 | + var factory = new ChannelFactory<IEchoContract>(binding, endpoint); |
| 36 | + var client = factory.CreateChannel(); |
| 37 | + _echo = client; |
| 38 | + |
| 39 | + var channel = client as ICommunicationObject; |
| 40 | + channel.Closed += (sender, args) => |
| 41 | + { |
| 42 | + _echo = null; |
| 43 | + }; |
| 44 | + } |
| 45 | + return _echo; |
| 46 | + } |
| 47 | + |
| 48 | + public ICounterContract GetCounterService() |
| 49 | + { |
| 50 | + if (_counter == null) |
| 51 | + { |
| 52 | + var url = new Uri(TestingServer.BaseAddress, "multiple/counter"); |
| 53 | + var binding = new BasicHttpBinding(); |
| 54 | + var endpoint = new EndpointAddress(url); |
| 55 | + var factory = new ChannelFactory<ICounterContract>(binding, endpoint); |
| 56 | + var client = factory.CreateChannel(); |
| 57 | + _counter = client; |
| 58 | + |
| 59 | + var channel = client as ICommunicationObject; |
| 60 | + channel.Closed += (sender, args) => |
| 61 | + { |
| 62 | + _counter = null; |
| 63 | + }; |
| 64 | + } |
| 65 | + return _counter; |
| 66 | + } |
| 67 | + |
| 68 | + public void Dispose() |
| 69 | + { |
| 70 | + Close(_echo); |
| 71 | + Close(_counter); |
| 72 | + TestingServer.Dispose(); |
| 73 | + } |
| 74 | + |
| 75 | + private void Close(object service) |
| 76 | + { |
| 77 | + var channel = service as ICommunicationObject; |
| 78 | + if (channel != null) |
| 79 | + channel.Close(); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments