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 ( ) ;
0 commit comments