Skip to content

Commit 40b4bfa

Browse files
committed
fixes multiple contract services.
1 parent 037a340 commit 40b4bfa

File tree

8 files changed

+227
-1
lines changed

8 files changed

+227
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.ServiceModel;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Solid.AspNetCore.Extensions.Wcf.Tests.Abstractions
9+
{
10+
[ServiceContract]
11+
public interface ICounterContract
12+
{
13+
[OperationContract]
14+
int Increment();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.ServiceModel;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Solid.AspNetCore.Extensions.Wcf.Tests.Abstractions
9+
{
10+
[ServiceContract]
11+
public interface IEchoContract
12+
{
13+
[OperationContract]
14+
string Echo(string value);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Solid.AspNetCore.Extensions.Wcf.Tests.Abstractions;
4+
using Solid.AspNetCore.Extensions.Wcf.Tests.Host.Services;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Solid.AspNetCore.Extensions.Wcf.Tests.Host
12+
{
13+
public class MulitpleContractTestStartup
14+
{
15+
public void ConfigureServices(IServiceCollection services)
16+
{
17+
services.AddWcfService<MultipleContract>();
18+
}
19+
20+
public void Configure(IApplicationBuilder builder)
21+
{
22+
builder.UseWcfService<MultipleContract>("/multiple", b => b.AddServiceEndpoint<IEchoContract>("/echo").AddServiceEndpoint<ICounterContract>("counter"));
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Solid.AspNetCore.Extensions.Wcf.Tests.Abstractions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.ServiceModel;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Solid.AspNetCore.Extensions.Wcf.Tests.Host.Services
10+
{
11+
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
12+
public class MultipleContract : IEchoContract, ICounterContract
13+
{
14+
public string Echo(string value)
15+
{
16+
return value;
17+
}
18+
19+
private int _counter;
20+
public int Increment()
21+
{
22+
return ++_counter;
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace Solid.AspNetCore.Extensions.Wcf.Tests
9+
{
10+
public class MultipleContractTests : IClassFixture<MultipleContractTestFixture>
11+
{
12+
private MultipleContractTestFixture _fixture;
13+
14+
public MultipleContractTests(MultipleContractTestFixture fixture)
15+
{
16+
_fixture = fixture;
17+
}
18+
19+
[Fact]
20+
public void ShouldEcho()
21+
{
22+
var service = _fixture.GetEchoService();
23+
var expected = Guid.NewGuid().ToString();
24+
var value = service.Echo(expected);
25+
Assert.Equal(expected, value);
26+
}
27+
28+
[Fact]
29+
public void ShouldIncrement()
30+
{
31+
var service = _fixture.GetCounterService();
32+
33+
var first = service.Increment();
34+
var second = service.Increment();
35+
36+
Assert.Equal(1, first);
37+
Assert.Equal(2, second);
38+
}
39+
}
40+
}

Solid.AspNetCore.Extensions.Wcf/ApplicationBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static IApplicationBuilder UseWcfService<TService>(this IApplicationBuild
4242
{
4343
var endpoints = builder.ApplicationServices.GetService<EndpointBuilder<TService>>();
4444
action(endpoints);
45-
return builder.MapWhen(context => context.Request.Path == path, b => b.UseMiddleware<WcfProxyMiddleware<TService>>(path));
45+
return builder.MapWhen(context => context.Request.Path.StartsWithSegments(path), b => b.UseMiddleware<WcfProxyMiddleware<TService>>(path));
4646
}
4747
}
4848
}

Solid.AspNetCore.Extensions.Wcf/ServiceModel/AspNetCoreServiceHost.cs

+22
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ protected override void InitializeRuntime()
7676
AddServiceEndpoint(endpoint.Contract, endpoint.Binding, endpoint.Path);
7777
base.InitializeRuntime();
7878
}
79+
80+
private string Sanitize(string relative)
81+
{
82+
while (relative.StartsWith("/"))
83+
relative = relative.Substring(1);
84+
return relative;
85+
}
86+
87+
private string GetAbsolute(string relative)
88+
{
89+
while (relative.StartsWith("/"))
90+
relative = relative.Substring(1);
91+
var baseAddress = BaseAddresses.First().ToString();
92+
if (string.IsNullOrEmpty(relative))
93+
return baseAddress;
94+
95+
if (!baseAddress.EndsWith("/"))
96+
baseAddress = baseAddress + "/";
97+
var baseUri = new Uri(baseAddress);
98+
var absolute = new Uri(baseUri, relative);
99+
return absolute.ToString();
100+
}
79101
}
80102

81103
internal abstract class AspNetCoreServiceHost : ServiceHost

0 commit comments

Comments
 (0)