0

I'm trying to change HTTPS port in an ASP.NET Core 6 Grpc application but the application always shows error whenever specific port is not set (in my case it's 7113). I'm planning to dockerize the application and the IP needs to be dynamically set. This is my Program.cs file,

global using dtms_service_master.Models.Context;
using dtms_service_master.Controllers;
using dtms_service_master.Models;
using dtms_service_master.Repositories;
using dtms_service_master.Services;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.ListenAnyIP(80, listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http1;
    });
    serverOptions.ListenAnyIP(7113, listenOptions => // in my case this needs always to be 7113 :(
    {
        listenOptions.Protocols = HttpProtocols.Http2;
        listenOptions.UseHttps();
    });
});

var Configuration = builder.Configuration;

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682

// Add services to the container.
builder.Services.AddGrpc();
builder.Services.AddGrpcHttpApi();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
builder.Services.AddGrpcSwagger();

builder.Services.AddGrpcReflection();

builder.Services.AddCors(o =>
    o.AddDefaultPolicy(builder =>
    {
        builder.WithOrigins("*")
               .AllowAnyMethod()
               .AllowAnyHeader()
               .WithExposedHeaders(
                    "Grpc-Status",
                    "Grpc-Message",
                    "Grpc-Encoding",
                    "Grpc-Accept-Encoding");
    }));

builder.Services.AddDbContextFactory<ServiceMasterContext>(
    options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddRepository();
builder.Services.AddServices();
builder.Services.AddAutoMapper(typeof(Mapper));
builder.Services.Configure<KestrelServerOptions>(options => options.AllowSynchronousIO = true);

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "gRPC HTTP API Example V1");
});

app.UseRouting();
app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true });
app.UseCors();

if (app.Environment.IsDevelopment())
    app.MapGrpcReflectionService();

app.UseEndpoints(endpoints =>
{
    endpoints.MapGrpcService<GreeterService>();
    endpoints.MapGrpcService<DummyController>();
    endpoints.MapGrpcService<VendorController>();
});

app.MapGet("/", () => "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");

app.Run();

This is the error I mentioned before,

Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="Error starting gRPC call. HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:7113) SocketException: No connection could be made because the target machine actively refused it.", DebugException="System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:7113)
 ---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|283_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.DefaultConnectAsync(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
   at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at Grpc.Net.Client.Internal.GrpcCall`2.RunCall(HttpRequestMessage request, Nullable`1 timeout)")
   at Grpc.Net.Client.Internal.HttpContentClientStreamWriter`2.WriteAsyncCore[TState](Func`5 writeFunc, TState state)
   at Kreya.Grpc.Core.Importer.ServerReflection.GrpcServerReflectionImporter.ReadAllServices(AsyncDuplexStreamingCall`2 call)
   at Kreya.Grpc.Core.Importer.ServerReflection.GrpcServerReflectionImporter.ImportViaServerReflection(GrpcServerReflectionImporterOptions options)

Any idea how do I change the HTTPS port without error?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dryluigi
  • 35
  • 2
  • 6
  • You need to use same port number that server is using. The server will not respond if you use the wrong port number. The port number can be a user input, but the server must be listening to the port number that the user enters. – jdweng Jul 09 '22 at 14:58
  • when are you getting error on startup or during calling any API ? check this. https://stackoverflow.com/a/70817994/9247039 – CodingMytra Jul 09 '22 at 18:03
  • The port can also be blocked by a firewall. – Yuri Golobokov Jul 13 '22 at 00:10
  • You changed the port on the server, but did you set the same port on the client? – Yuri Golobokov Jul 13 '22 at 00:12

1 Answers1

0

Since you are using Docker then you need to expose the port using the docker run -p command. Check it's link here.

Moreover how to do this thing in asp.net core app, see this.

yogihosting
  • 5,494
  • 8
  • 47
  • 80