Skip to content

Commit 062f68e

Browse files
committed
add NetTcp and convert to top level statement
1 parent 8c4edf1 commit 062f68e

File tree

6 files changed

+70
-69
lines changed

6 files changed

+70
-69
lines changed

Basic/Services/Hosting/WindowsService/Service/CalculatorService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ public double Divide(double n1, double n2)
2626
return n1 / n2;
2727
}
2828
}
29-
30-
}
29+
}

Basic/Services/Hosting/WindowsService/Service/ICalculatorService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using CoreWCF;
5-
64
namespace CoreWcf.Samples.WindowsService
75
{
86
// Define a service contract.
@@ -18,4 +16,4 @@ public interface ICalculatorService
1816
[OperationContract]
1917
double Divide(double n1, double n2);
2018
}
21-
}
19+
}
Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
using CoreWCF;
6-
using CoreWCF.Configuration;
7-
using Microsoft.AspNetCore.Builder;
8-
using Microsoft.Extensions.DependencyInjection;
9-
using Microsoft.Extensions.Hosting;
10-
using Microsoft.Extensions.Hosting.WindowsServices;
11-
12-
namespace CoreWcf.Samples.WindowsService
4+
const int HttpPort = 5000;
5+
const int NetTcpPort = 8089;
6+
var options = new WebApplicationOptions
137
{
14-
public class Program
8+
Args = args,
9+
ContentRootPath = WindowsServiceHelpers.IsWindowsService()
10+
? AppContext.BaseDirectory : default
11+
};
12+
13+
var builder = WebApplication.CreateBuilder(options);
14+
15+
// Enable CoreWCF Services, with metadata (WSDL) support
16+
builder.Services.AddHostedService<WindowsServiceWorker>()
17+
.AddServiceModelServices()
18+
.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>()
19+
.AddServiceModelMetadata();
20+
21+
builder.WebHost.UseKestrel(options =>
22+
{
23+
options.ListenAnyIP(HttpPort);
24+
})
25+
.UseNetTcp(NetTcpPort);
26+
27+
builder.Host.UseWindowsService(options =>
28+
{
29+
// Set service name (Optional)
30+
options.ServiceName = "CoreWCF Windows Service";
31+
});
32+
33+
var app = builder.Build();
34+
35+
// Configure the bindings and endpoints
36+
app.UseServiceModel(builder =>
37+
{
38+
// Add the Calculator Service
39+
builder.AddService<CalculatorService>(serviceOptions =>
1540
{
16-
static void Main(string[] args)
17-
{
18-
var options = new WebApplicationOptions
19-
{
20-
Args = args,
21-
ContentRootPath = WindowsServiceHelpers.IsWindowsService()
22-
? AppContext.BaseDirectory : default
23-
};
24-
25-
var builder = WebApplication.CreateBuilder(options);
26-
27-
// Enable CoreWCF Services, with metadata (WSDL) support
28-
builder.Services.AddHostedService<WindowsServiceWorker>()
29-
.AddServiceModelServices()
30-
.AddServiceModelMetadata();
31-
32-
builder.Host.UseWindowsService(options =>
33-
{
34-
// Set service name (Optional)
35-
options.ServiceName = "CoreWCF Windows Service";
36-
});
37-
38-
var app = builder.Build();
39-
40-
// Configure the bindings and endpoints
41-
app.UseServiceModel(builder =>
42-
{
43-
// Add the Calculator Service
44-
builder.AddService<CalculatorService>(serviceOptions => { })
45-
// Add BasicHttpBinding endpoint
46-
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new BasicHttpBinding(), "/CalculatorService/basicHttp");
47-
48-
// Configure WSDL to be available
49-
var serviceMetadataBehavior = app.Services.GetRequiredService<CoreWCF.Description.ServiceMetadataBehavior>();
50-
serviceMetadataBehavior.HttpGetEnabled = true;
51-
});
52-
53-
app.Run();
54-
}
55-
}
56-
}
41+
// Set the default host name:port in generated WSDL and the base path for the address
42+
serviceOptions.BaseAddresses.Add(new Uri($"http://localhost:{HttpPort}/CalculatorService/basicService"));
43+
})
44+
// Add BasicHttpBinding endpoint
45+
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new BasicHttpBinding(), "/CalculatorService/basicHttp")
46+
// Add NetTcp endpoint
47+
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new NetTcpBinding(), $"net.tcp://localhost:{NetTcpPort}/CalculatorService/netTcp");
48+
49+
// Configure WSDL to be available
50+
var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
51+
serviceMetadataBehavior.HttpGetEnabled = true;
52+
});
53+
54+
app.Run();
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"profiles": {
33
"Service": {
4-
"commandName": "Project",
5-
"dotnetRunMessages": true,
6-
"launchBrowser": true,
7-
"launchUrl": "http://localhost:5000/CalculatorService/basicHttp",
8-
"environmentVariables": {
9-
"ASPNETCORE_ENVIRONMENT": "Development"
10-
}
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"launchBrowser": true,
7+
"launchUrl": "http://localhost:5000/CalculatorService/basicService",
8+
"environmentVariables": {
9+
"ASPNETCORE_ENVIRONMENT": "Development"
10+
}
1111
}
1212
}
1313
}

Basic/Services/Hosting/WindowsService/Service/Service.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>true</ImplicitUsings>
68
</PropertyGroup>
79

10+
<ItemGroup>
11+
<Using Include="CoreWCF" />
12+
<Using Include="CoreWCF.Configuration" />
13+
<Using Include="CoreWCF.Description" />
14+
<Using Include="CoreWcf.Samples.WindowsService" />
15+
<Using Include="Microsoft.Extensions.Hosting.WindowsServices" />
16+
</ItemGroup>
17+
818
<ItemGroup>
919
<PackageReference Include="CoreWCF.Http" Version="1.*" />
20+
<PackageReference Include="CoreWCF.NetTcp" Version="1.*" />
1021
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="6.*" />
1122
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.*" />
1223
</ItemGroup>

Basic/Services/Hosting/WindowsService/Service/ServiceWorker.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Threading;
5-
using System.Threading.Tasks;
6-
using Microsoft.Extensions.Hosting;
7-
using Microsoft.Extensions.Logging;
8-
94
namespace CoreWcf.Samples.WindowsService
105
{
116
public class WindowsServiceWorker : BackgroundService

0 commit comments

Comments
 (0)