Raju Giri
Raju Giri

Reputation: 43

Asp.net 6 Multi Application Hosting in Docker

I created 2 Application:

  1. API Project
  2. GRPC service Project

In the api project, I access GRPC services, when I run application on IIS Server then it's working fine but when I hosted those application on my local machine in docker Container then it's not working.

This is my Docker file of API project

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["wc.account.api/wc.account.api.csproj", "wc.account.api/"]
COPY ["wc.signalr/wc.signalr.csproj", "wc.signalr/"]
COPY ["wc.memory.cache/wc.memory.cache.csproj", "wc.memory.cache/"]
COPY ["wc.models/wc.models.csproj", "wc.models/"]
RUN dotnet restore "wc.account.api/wc.account.api.csproj"
COPY . .
WORKDIR "/src/wc.account.api"
RUN dotnet build "wc.account.api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "wc.account.api.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "wc.account.api.dll"]

This is Docker file of my GRPC project

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["wc.account/wc.account.csproj", "wc.account/"]
COPY ["wc.logger/wc.logger.csproj", "wc.logger/"]
COPY ["wc.models/wc.models.csproj", "wc.models/"]
RUN dotnet restore "wc.account/wc.account.csproj"
COPY . .
WORKDIR "/src/wc.account"
RUN dotnet build "wc.account.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "wc.account.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "wc.account.dll"]

this is my docker-compose.yml file

version: '3.4'

services:
  wc.account.api:
    image: ${DOCKER_REGISTRY-}wcaccountapi
    build:
      context: .
      dockerfile: wc.account.api/Dockerfile

  wc.account:
    image: ${DOCKER_REGISTRY-}wcaccount
    build:
      context: .
      dockerfile: wc.account/Dockerfile

I'm getting this error:

Status(StatusCode="Unavailable", Detail="Error starting gRPC call. HttpRequestException: Cannot assign requested address (localhost:49153) SocketException: Cannot assign requested address", DebugException="System.Net.Http.HttpRequestException: Cannot assign requested address (localhost:49153)
 ---> System.Net.Sockets.SocketException (99): Cannot assign requested address
   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|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.AddHttp2ConnectionAsync(HttpRequestMessage request)
   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Grpc.Net.Client.Internal.GrpcCall`2.GetResponseHeadersCoreAsync()")
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "grpcUrl": {
    "accountUrl": "https://localhost:7163/"
  },
  "Authentication": {
    "Key": "jaN8yp&bsffsL5Qnn6L7qyp&bsff8v3uyp&bsffsL5Qnn6L7",
    "Issuer": "https://localhost:46641/",
    "Audiance": "https://localhost:46641/",
    "AccessExpireMinutes": "40320"
  }
}

this is my AppSetting file where i Use GRPC Services

Upvotes: 4

Views: 874

Answers (1)

Akash Limbani
Akash Limbani

Reputation: 1351

I guess you need to change your compose file.

changes like.

version: '3.4'

network:
  account_network:
  external: true
  driver: bridge

services:
  wcaccountapi:
    container_name:wcaccountapi
    hostname:wcaccountapi
    image: ${DOCKER_REGISTRY-}wcaccountapi
    build:
      context: .
      dockerfile: wc.account.api/Dockerfile

  wcaccount:
    container_name:wcaccount
    hostname:wcaccount
    image: ${DOCKER_REGISTRY-}wcaccount
    build:
      context: .
      dockerfile: wc.account/Dockerfile

please check your docker file if it is wrong please update it.

next, you need to check your appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "grpcUrl": {
    "accountUrl": "https://wcaccount/"
  },
  "Authentication": {
    "Key": "jaN8yp&bsffsL5Qnn6L7qyp&bsff8v3uyp&bsffsL5Qnn6L7",
    "Issuer": "https://localhost:46641/",
    "Audiance": "https://localhost:46641/",
    "AccessExpireMinutes": "40320"
  }
}

Thanks in Advance :)

Upvotes: 1

Related Questions