Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8ae48c9

Browse filesBrowse files
Updated star wars .NET 8.0 project (#140)
* Fixed visual studio launch text * Converted from startup.cs to program.cs launch style
1 parent 1f03f57 commit 8ae48c9
Copy full SHA for 8ae48c9

File tree

4 files changed

+140
-188
lines changed
Filter options

4 files changed

+140
-188
lines changed

‎src/ancillary-projects/starwars/starwars-api70/Startup.cs

Copy file name to clipboardExpand all lines: src/ancillary-projects/starwars/starwars-api70/Startup.cs
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ public void ConfigureServices(IServiceCollection services)
119119
options.ConnectionKeepAliveInterval = SOCKET_CONNECTION_KEEPALIVE;
120120
});
121121

122-
services.AddControllers();
122+
// if you have rest controllers this item be sure they are included.
123+
// Graphql and rest can live side by side in the same project without issue
124+
// --------------------------------------------------
125+
// builder.Services.AddControllers();
123126
}
124127

125128
/// <summary>
@@ -144,10 +147,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
144147

145148
// if you have no rest controllers this item can be safely skipped
146149
// graphql and rest can live side by side in the same project without issue
147-
app.UseEndpoints(endpoints =>
148-
{
149-
endpoints.MapControllers();
150-
});
150+
// app.UseEndpoints(endpoints =>
151+
// {
152+
// endpoints.MapControllers();
153+
// });
151154

152155
// ************************************************************
153156
// Finalize the graphql setup:

‎src/ancillary-projects/starwars/starwars-api80/Program.cs

Copy file name to clipboardExpand all lines: src/ancillary-projects/starwars/starwars-api80/Program.cs
+131-10Lines changed: 131 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,142 @@
99

1010
namespace GraphQL.AspNet.StarWarsAPI6X
1111
{
12-
using Microsoft.AspNetCore.Hosting;
13-
using Microsoft.Extensions.Hosting;
12+
using System;
13+
using GraphQL.AspNet.Configuration;
14+
using GraphQL.AspNet.Execution;
15+
using GraphQL.AspNet.StarwarsAPI.Common.Services;
16+
using Microsoft.AspNetCore.Builder;
17+
using Microsoft.AspNetCore.WebSockets;
18+
using Microsoft.Extensions.DependencyInjection;
1419

1520
public static class Program
1621
{
22+
private const string ALL_ORIGINS_POLICY = "_allOrigins";
23+
24+
private static readonly TimeSpan SOCKET_CONNECTION_KEEPALIVE = TimeSpan.FromSeconds(10);
25+
1726
public static void Main(string[] args)
1827
{
19-
CreateHostBuilder(args).Build().Run();
20-
}
28+
var builder = WebApplication.CreateBuilder(args);
29+
30+
// Add various required services
31+
// ----------------------------------------------
32+
builder.Services.AddAuthorization();
33+
builder.Services.AddSingleton<StarWarsDataRepository>();
34+
builder.Services.AddScoped<IStarWarsDataService, StarWarsDataService>();
35+
36+
// apply an unrestricted cors policy for the demo services
37+
// to allow use on many of the tools for testing (graphiql, altair etc.)
38+
// Do not do this in production
39+
builder.Services.AddCors(options =>
40+
{
41+
options.AddPolicy(
42+
ALL_ORIGINS_POLICY,
43+
builder =>
44+
{
45+
builder.AllowAnyOrigin()
46+
.AllowAnyHeader()
47+
.AllowAnyMethod();
48+
});
49+
});
50+
51+
// ----------------------------------------------------------
52+
// Register GraphQL with the application
53+
// ----------------------------------------------------------
54+
// By default graphql will scan your assembly for any GraphControllers
55+
// and automatically wire them up to the schema
56+
// you can control which assemblies are scanned and which classes are registered using
57+
// the schema configuration options set here.
58+
//
59+
// in this example because of the two test projects (netcore3.1 and net5.0)
60+
// we have moved all the shared code to a common assembly (starwars-common) and are injecting it
61+
// as a single unit
62+
//
63+
// we then add subscription services to the schema builder returned from .AddGraphQL()
64+
builder.Services.AddGraphQL(options =>
65+
{
66+
options.ResponseOptions.ExposeExceptions = true;
67+
options.ResponseOptions.MessageSeverityLevel = GraphMessageSeverity.Information;
68+
69+
// options.ExecutionOptions.EnableMetrics = true;
70+
// options.ResponseOptions.ExposeMetrics = true;
71+
72+
var assembly = typeof(StarWarsDataRepository).Assembly;
73+
options.AddAssembly(assembly);
74+
})
75+
.AddSubscriptions(options =>
76+
{
77+
// this route path is set by default
78+
// it is listed here just as a matter of example
79+
options.Route = SubscriptionConstants.Routing.DEFAULT_SUBSCRIPTIONS_ROUTE;
80+
81+
// for some web based graphql tools such as graphiql and graphql-playground
82+
// the default keep-alive timeout of 2 minutes is too long.
83+
//
84+
// still others (like graphql-playground running in electron) do not respond/configure
85+
// for socket-level ping/pong frames to allow for socket-level keep alives
86+
//
87+
// here we set this demo project websocket keep-alive (at the server level)
88+
// to be below all those thresholds to ensure a hassle free experience.
89+
// In practice, you should configure your server (both subscription keep alives and socket keep alives)
90+
// with an interval that is compatiable with your client side environment.
91+
options.ConnectionKeepAliveInterval = SOCKET_CONNECTION_KEEPALIVE;
92+
});
2193

22-
public static IHostBuilder CreateHostBuilder(string[] args) =>
23-
Host.CreateDefaultBuilder(args)
24-
.ConfigureWebHostDefaults(webBuilder =>
25-
{
26-
webBuilder.UseStartup<Startup>();
27-
});
94+
// if you have rest controllers this item be sure they are included.
95+
// Graphql and rest can live side by side in the same project without issue
96+
// --------------------------------------------------
97+
// builder.Services.AddControllers();
98+
99+
// ASP.NET websockets implementation must also be added to the runtime
100+
builder.Services.AddWebSockets((options) =>
101+
{
102+
// here add some common origins of various tools that may be
103+
// used for running this demo
104+
// do not add these in a production app
105+
options.AllowedOrigins.Add("http://localhost:5000");
106+
options.AllowedOrigins.Add("http://localhost:4000");
107+
options.AllowedOrigins.Add("http://localhost:3000");
108+
options.AllowedOrigins.Add("null");
109+
110+
// some electron-based graphql tools send a file reference
111+
// as their origin
112+
// do not add these in a production app
113+
options.AllowedOrigins.Add("file://");
114+
options.AllowedOrigins.Add("ws://");
115+
});
116+
117+
var app = builder.Build();
118+
119+
// Configure the HTTP request pipeline.
120+
app.AddStarWarsStartedMessageToConsole();
121+
app.UseRouting();
122+
app.UseCors(ALL_ORIGINS_POLICY);
123+
app.UseAuthorization();
124+
125+
// enable web sockets on this server instance
126+
// this must be done before a call to 'UseGraphQL' if subscriptions are enabled for any
127+
// schema otherwise the subscriptions may not register correctly
128+
app.UseWebSockets();
129+
130+
// if you have no rest controllers this item can be safely skipped
131+
// graphql and rest can live side by side in the same project without issue
132+
// -----------------------------------------------------------------
133+
// app.UseEndpoints(endpoints =>
134+
// {
135+
// endpoints.MapControllers();
136+
// });
137+
138+
// ************************************************************
139+
// Finalize the graphql setup by loading the schema, build out the templates for all found graph types
140+
// and publish the route to hook the graphql runtime to the web.
141+
// be sure to register it after "UseAuthorization" if you require access to this.User
142+
//
143+
// If the construction of your runtime schema has any errors they will be thrown here
144+
// before your application starts listening for requests.
145+
// ************************************************************
146+
app.UseGraphQL();
147+
app.Run();
148+
}
28149
}
29150
}

‎src/ancillary-projects/starwars/starwars-api80/Properties/launchSettings.json

Copy file name to clipboardExpand all lines: src/ancillary-projects/starwars/starwars-api80/Properties/launchSettings.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json.schemastore.org/launchsettings.json",
33
"profiles": {
4-
"StarWars: .NET 6": {
4+
"StarWars: .NET 8": {
55
"commandName": "Project",
66
"applicationUrl": "http://localhost:5000",
77
"environmentVariables": {

‎src/ancillary-projects/starwars/starwars-api80/Startup.cs

Copy file name to clipboardExpand all lines: src/ancillary-projects/starwars/starwars-api80/Startup.cs
-172Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.