Skip to content

Commit 5f1cc2f

Browse files
author
Jacques Kang
committed
provide sample for hosting in web application
1 parent 8162dde commit 5f1cc2f

File tree

5 files changed

+159
-2
lines changed

5 files changed

+159
-2
lines changed

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Support using primitive or complexe types in service contract.
88

99
## Usage
1010
1. Create an interface as service contract (ideally package in a shared assembly)
11-
2. On client side, implement a proxy with help of abstract class provided by framework
12-
3. On server side, implement the service and register in IoC container
11+
2. Implement a client proxy with help of abstract class provided by framework
12+
3. Implement the service and register in IoC container
13+
4. Host the service in an console or web applciation
1314

1415
## Sample:
1516

@@ -78,6 +79,8 @@ Support using primitive or complexe types in service contract.
7879
}
7980
```
8081

82+
4. Hosting in Console application
83+
8184
```csharp
8285
// hosting in Console application
8386
class Program
@@ -114,6 +117,63 @@ Support using primitive or complexe types in service contract.
114117
}
115118
```
116119

120+
5. Hosting in Web application
121+
122+
```csharp
123+
public class Program
124+
{
125+
public static void Main(string[] args)
126+
{
127+
IWebHost webHost = BuildWebHost(args);
128+
129+
ThreadPool.QueueUserWorkItem(StartIpcService,
130+
webHost.Services.CreateScope().ServiceProvider);
131+
132+
webHost.Run();
133+
}
134+
135+
private static void StartIpcService(object state)
136+
{
137+
var serviceProvider = state as IServiceProvider;
138+
IpcServiceHostBuilder
139+
.Buid("pipeName", serviceProvider as IServiceProvider)
140+
.Start();
141+
}
142+
143+
public static IWebHost BuildWebHost(string[] args) =>
144+
WebHost.CreateDefaultBuilder(args)
145+
.UseStartup<Startup>()
146+
.Build();
147+
}
148+
```
149+
150+
151+
```csharp
152+
public class Startup
153+
{
154+
public void ConfigureServices(IServiceCollection services)
155+
{
156+
services
157+
.AddIpc()
158+
.AddService<IComputingService, ComputingService>()
159+
;
160+
}
161+
162+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
163+
{
164+
if (env.IsDevelopment())
165+
{
166+
app.UseDeveloperExceptionPage();
167+
}
168+
169+
app.Run(async (context) =>
170+
{
171+
await context.Response.WriteAsync("Hello World!");
172+
});
173+
}
174+
}
175+
```
176+
117177
I'll publish a NuGet package soon.
118178

119179
Any contributions or comments are welcome!

src/IpcServiceFramework.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.IpcServiceFramework.S
2525
EndProject
2626
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JKang.IpcServiceFramework.Core.Tests", "JKang.IpcServiceFramework.Core.Tests\JKang.IpcServiceFramework.Core.Tests.csproj", "{1EC81913-883B-487C-A3FD-98A80EDE3225}"
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpcServiceSample.WebServer", "IpcServiceSample.WebServer\IpcServiceSample.WebServer.csproj", "{D57727B9-81F1-439A-AD17-0DB26C8F0523}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -59,6 +61,10 @@ Global
5961
{1EC81913-883B-487C-A3FD-98A80EDE3225}.Debug|Any CPU.Build.0 = Debug|Any CPU
6062
{1EC81913-883B-487C-A3FD-98A80EDE3225}.Release|Any CPU.ActiveCfg = Release|Any CPU
6163
{1EC81913-883B-487C-A3FD-98A80EDE3225}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{D57727B9-81F1-439A-AD17-0DB26C8F0523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{D57727B9-81F1-439A-AD17-0DB26C8F0523}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{D57727B9-81F1-439A-AD17-0DB26C8F0523}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{D57727B9-81F1-439A-AD17-0DB26C8F0523}.Release|Any CPU.Build.0 = Release|Any CPU
6268
EndGlobalSection
6369
GlobalSection(SolutionProperties) = preSolution
6470
HideSolutionNode = FALSE
@@ -67,6 +73,7 @@ Global
6773
{D1ADB92C-B4FB-40DD-911E-46360A23381B} = {03210BFB-17B1-4775-A8A2-D302ECBF2F46}
6874
{24A3C4D2-95A2-48D9-86F2-648879EC74F4} = {03210BFB-17B1-4775-A8A2-D302ECBF2F46}
6975
{8D54E62A-ECFF-4FFF-B9D1-DB343D456451} = {03210BFB-17B1-4775-A8A2-D302ECBF2F46}
76+
{D57727B9-81F1-439A-AD17-0DB26C8F0523} = {03210BFB-17B1-4775-A8A2-D302ECBF2F46}
7077
EndGlobalSection
7178
GlobalSection(ExtensibilityGlobals) = postSolution
7279
SolutionGuid = {F87E0D46-F461-4E41-9A3B-64710A6DFB2F}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="wwwroot\" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\IpcServiceSample.ConsoleServer\IpcServiceSample.ConsoleServer.csproj" />
17+
<ProjectReference Include="..\IpcServiceSample.ServiceContracts\IpcServiceSample.ServiceContracts.csproj" />
18+
<ProjectReference Include="..\JKang.IpcServiceFramework.Server\JKang.IpcServiceFramework.Server.csproj" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using JKang.IpcServiceFramework;
2+
using Microsoft.AspNetCore;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System;
6+
using System.Threading;
7+
8+
namespace IpcServiceSample.WebServer
9+
{
10+
public class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
IWebHost webHost = BuildWebHost(args);
15+
16+
ThreadPool.QueueUserWorkItem(StartIpcService,
17+
webHost.Services.CreateScope().ServiceProvider);
18+
19+
webHost.Run();
20+
}
21+
22+
private static void StartIpcService(object state)
23+
{
24+
var serviceProvider = state as IServiceProvider;
25+
IpcServiceHostBuilder
26+
.Buid("pipeName", serviceProvider as IServiceProvider)
27+
.Start();
28+
}
29+
30+
public static IWebHost BuildWebHost(string[] args) =>
31+
WebHost.CreateDefaultBuilder(args)
32+
.UseStartup<Startup>()
33+
.Build();
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using IpcServiceSample.ConsoleServer;
2+
using IpcServiceSample.ServiceContracts;
3+
using JKang.IpcServiceFramework;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace IpcServiceSample.WebServer
10+
{
11+
public class Startup
12+
{
13+
public void ConfigureServices(IServiceCollection services)
14+
{
15+
services
16+
.AddIpc()
17+
.AddService<IComputingService, ComputingService>()
18+
;
19+
}
20+
21+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
22+
{
23+
if (env.IsDevelopment())
24+
{
25+
app.UseDeveloperExceptionPage();
26+
}
27+
28+
app.Run(async (context) =>
29+
{
30+
await context.Response.WriteAsync("Hello World!");
31+
});
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)