无法托管或连接到服务
Unable to host or connect to service
我正在使用 Protobuf-net 创建一个简单的示例供我使用,但我无法让它工作。
我从客户端收到以下错误:
状态(StatusCode="不可用....
我已经从protobuf-net.Grpc下载了示例项目Client_CS、Server_CS和Shared_CS,当运行那.
启动服务器时弹出的网页,使用的是urllocalhost:44312
但是 launchSettings 说:
"iisExpress": {
"applicationUrl": "http://localhost:9385",
"sslPort": 44312
}
还有
"applicationUrl": "https://localhost:5001;http://localhost:5000",
但是启动设置为侦听端口 10042,客户端正在同一端口上寻找服务。很混乱?
合同:
[ProtoContract]
public class TestRequest
{
}
[ProtoContract]
public class TestResponse
{
[ProtoMember(1)]
public string Text { get; set; }
}
服务:
public class TestService : ITestService
{
public ValueTask<TestResponse> TestAsync(TestRequest request, CallContext context = default)
{
TestResponse resp = new TestResponse();
resp.Text = "weee";
return new ValueTask<TestResponse>(resp);
}
}
Program.cs:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureKestrel(options =>
{
options.ListenLocalhost(10042, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
});
})
.UseStartup<Startup>();
}
Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCodeFirstGrpc(config =>
{
config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
});
services.AddSingleton<ITestService, TestService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<ITestService>();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
});
});
}
}
lanuchSettings.json:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:9385",
"sslPort": 44312
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Protobuf_net.API": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": false,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
客户:
GrpcClientFactory.AllowUnencryptedHttp2 = true;
using var http = GrpcChannel.ForAddress("http://localhost:10042");
var testService = http.CreateGrpcService<ITestService>();
var result = await testService.TestAsync(new TestRequest());
Console.WriteLine($"Result: {result.Text}");
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
我认为这只是一件IDE事情;默认情况下,IDE 想要使用 IIS,坦率地说:它把一切搞得一团糟。首先,将服务器项目设置为单启动项目(右击项目,“设置为启动项目”):
现在,在 IDE 的顶部,您会可能 看到它正在尝试使用 IIS:
这是错误的配置;将其更改为服务器项目 本身 :
现在当您 运行 服务器时,它应该加载到正确的端口。
我正在使用 Protobuf-net 创建一个简单的示例供我使用,但我无法让它工作。
我从客户端收到以下错误:
状态(StatusCode="不可用....
我已经从protobuf-net.Grpc下载了示例项目Client_CS、Server_CS和Shared_CS,当运行那.
启动服务器时弹出的网页,使用的是urllocalhost:44312
但是 launchSettings 说:
"iisExpress": {
"applicationUrl": "http://localhost:9385",
"sslPort": 44312
}
还有
"applicationUrl": "https://localhost:5001;http://localhost:5000",
但是启动设置为侦听端口 10042,客户端正在同一端口上寻找服务。很混乱?
合同:
[ProtoContract]
public class TestRequest
{
}
[ProtoContract]
public class TestResponse
{
[ProtoMember(1)]
public string Text { get; set; }
}
服务:
public class TestService : ITestService
{
public ValueTask<TestResponse> TestAsync(TestRequest request, CallContext context = default)
{
TestResponse resp = new TestResponse();
resp.Text = "weee";
return new ValueTask<TestResponse>(resp);
}
}
Program.cs:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureKestrel(options =>
{
options.ListenLocalhost(10042, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
});
})
.UseStartup<Startup>();
}
Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCodeFirstGrpc(config =>
{
config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
});
services.AddSingleton<ITestService, TestService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<ITestService>();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
});
});
}
}
lanuchSettings.json:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:9385",
"sslPort": 44312
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Protobuf_net.API": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": false,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
客户:
GrpcClientFactory.AllowUnencryptedHttp2 = true;
using var http = GrpcChannel.ForAddress("http://localhost:10042");
var testService = http.CreateGrpcService<ITestService>();
var result = await testService.TestAsync(new TestRequest());
Console.WriteLine($"Result: {result.Text}");
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
我认为这只是一件IDE事情;默认情况下,IDE 想要使用 IIS,坦率地说:它把一切搞得一团糟。首先,将服务器项目设置为单启动项目(右击项目,“设置为启动项目”):
现在,在 IDE 的顶部,您会可能 看到它正在尝试使用 IIS:
这是错误的配置;将其更改为服务器项目 本身 :
现在当您 运行 服务器时,它应该加载到正确的端口。