本文整理汇总了C#中Microsoft.AspNet.Hosting.WebApplicationBuilder.Build方法的典型用法代码示例。如果您正苦于以下问题:C# WebApplicationBuilder.Build方法的具体用法?C# WebApplicationBuilder.Build怎么用?C# WebApplicationBuilder.Build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNet.Hosting.WebApplicationBuilder
的用法示例。
在下文中一共展示了WebApplicationBuilder.Build方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterAddresses_Success
public async Task RegisterAddresses_Success(string addressInput, string[] testUrls)
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "server.urls", addressInput }
})
.Build();
var applicationBuilder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(ConfigureEchoAddress);
using (var app = applicationBuilder.Build().Start())
{
using (var client = new HttpClient())
{
foreach (var testUrl in testUrls)
{
var responseText = await client.GetStringAsync(testUrl);
Assert.Equal(testUrl, responseText);
}
}
}
}
示例2: LargeDownload
public async Task LargeDownload()
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "server.urls", "http://localhost:8792/" }
})
.Build();
var applicationBuilder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
app.Run(async context =>
{
var bytes = new byte[1024];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)i;
}
context.Response.ContentLength = bytes.Length * 1024;
for (int i = 0; i < 1024; i++)
{
await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
}
});
});
using (var app = applicationBuilder.Build().Start())
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("http://localhost:8792/");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStreamAsync();
// Read the full response body
var total = 0;
var bytes = new byte[1024];
var count = await responseBody.ReadAsync(bytes, 0, bytes.Length);
while (count > 0)
{
for (int i = 0; i < count; i++)
{
Assert.Equal(total % 256, bytes[i]);
total++;
}
count = await responseBody.ReadAsync(bytes, 0, bytes.Length);
}
}
}
}
示例3: LargeUpload
public async Task LargeUpload()
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "server.urls", "http://localhost:8791/" }
})
.Build();
var applicationBuilder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
app.Run(async context =>
{
// Read the full request body
var total = 0;
var bytes = new byte[1024];
var count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length);
while (count > 0)
{
for (int i = 0; i < count; i++)
{
Assert.Equal(total % 256, bytes[i]);
total++;
}
count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length);
}
await context.Response.WriteAsync(total.ToString(CultureInfo.InvariantCulture));
});
});
using (var app = applicationBuilder.Build().Start())
{
using (var client = new HttpClient())
{
var bytes = new byte[1024 * 1024];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)i;
}
var response = await client.PostAsync("http://localhost:8791/", new ByteArrayContent(bytes));
response.EnsureSuccessStatusCode();
var sizeString = await response.Content.ReadAsStringAsync();
Assert.Equal(sizeString, bytes.Length.ToString(CultureInfo.InvariantCulture));
}
}
}
示例4: ZeroToTenThreads
public async Task ZeroToTenThreads(int threadCount)
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "server.urls", "http://localhost:8790/" }
})
.Build();
var applicationBuilder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
var serverInfo = app.ServerFeatures.Get<IKestrelServerInformation>();
serverInfo.ThreadCount = threadCount;
app.Run(context =>
{
return context.Response.WriteAsync("Hello World");
});
});
using (var app = applicationBuilder.Build().Start())
{
using (var client = new HttpClient())
{
// Send 20 requests just to make sure we don't get any failures
var requestTasks = new List<Task<string>>();
for (int i = 0; i < 20; i++)
{
var requestTask = client.GetStringAsync("http://localhost:8790/");
requestTasks.Add(requestTask);
}
foreach (var result in await Task.WhenAll(requestTasks))
{
Assert.Equal("Hello World", result);
}
}
}
}
示例5: IgnoreNullHeaderValues
public async Task IgnoreNullHeaderValues(string headerName, StringValues headerValue, string expectedValue)
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "server.urls", "http://localhost:8793/" }
})
.Build();
var hostBuilder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
app.Run(async context =>
{
context.Response.Headers.Add(headerName, headerValue);
await context.Response.WriteAsync("");
});
});
using (var app = hostBuilder.Build().Start())
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("http://localhost:8793/");
response.EnsureSuccessStatusCode();
var headers = response.Headers;
if (expectedValue == null)
{
Assert.False(headers.Contains(headerName));
}
else
{
Assert.True(headers.Contains(headerName));
Assert.Equal(headers.GetValues(headerName).Single(), expectedValue);
}
}
}
}
示例6: TestPathBase
private async Task TestPathBase(string registerAddress, string requestAddress, string expectedPathBase, string expectedPath)
{
var config = new ConfigurationBuilder().AddInMemoryCollection(
new Dictionary<string, string> {
{ "server.urls", registerAddress }
}).Build();
var builder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync(JsonConvert.SerializeObject(new
{
PathBase = context.Request.PathBase.Value,
Path = context.Request.Path.Value
}));
});
});
using (var app = builder.Build().Start())
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(requestAddress);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(responseText);
var pathFacts = JsonConvert.DeserializeObject<JObject>(responseText);
Assert.Equal(expectedPathBase, pathFacts["PathBase"].Value<string>());
Assert.Equal(expectedPath, pathFacts["Path"].Value<string>());
}
}
}
示例7: TryCreateContextUsingAppCode
private DbContext TryCreateContextUsingAppCode(Type dbContextType, ModelType startupType)
{
var builder = new WebApplicationBuilder();
if (startupType != null)
{
var reflectedStartupType = dbContextType.GetTypeInfo().Assembly.GetType(startupType.FullName);
if (reflectedStartupType != null)
{
builder.UseStartup(reflectedStartupType);
}
}
var appServices = builder.Build().Services;
return appServices.GetService(dbContextType) as DbContext;
}
示例8: DoesNotHangOnConnectionCloseRequest
public async Task DoesNotHangOnConnectionCloseRequest()
{
var config = new ConfigurationBuilder().AddInMemoryCollection(
new Dictionary<string, string> {
{ "server.urls", "http://localhost:8791" }
}).Build();
var builder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
app.Run(async context =>
{
var connection = context.Connection;
await context.Response.WriteAsync("hello, world");
});
});
using (var app = builder.Build().Start())
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Connection.Clear();
client.DefaultRequestHeaders.Connection.Add("close");
var response = await client.GetAsync("http://localhost:8791/");
response.EnsureSuccessStatusCode();
}
}
示例9: TestRemoteIPAddress
private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress, string port)
{
var config = new ConfigurationBuilder().AddInMemoryCollection(
new Dictionary<string, string> {
{ "server.urls", $"http://{registerAddress}:{port}" }
}).Build();
var builder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
app.Run(async context =>
{
var connection = context.Connection;
await context.Response.WriteAsync(JsonConvert.SerializeObject(new
{
RemoteIPAddress = connection.RemoteIpAddress?.ToString(),
RemotePort = connection.RemotePort,
LocalIPAddress = connection.LocalIpAddress?.ToString(),
LocalPort = connection.LocalPort,
IsLocal = connection.IsLocal
}));
});
});
using (var app = builder.Build().Start())
using (var client = new HttpClient())
{
var response = await client.GetAsync($"http://{requestAddress}:{port}/");
response.EnsureSuccessStatusCode();
var connectionFacts = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(connectionFacts);
var facts = JsonConvert.DeserializeObject<JObject>(connectionFacts);
Assert.Equal(expectAddress, facts["RemoteIPAddress"].Value<string>());
Assert.NotEmpty(facts["RemotePort"].Value<string>());
Assert.True(facts["IsLocal"].Value<bool>());
}
}