Dapr service invocation throws SocketException when app is run separate from 'dapr run' CLI command
Hi,
I'm fairly new to Dapr so I'm not sure if I'm doing something wrong or that what I'm trying to achieve is actually a supported scenario. However reading the docs seems to indicate that this should be possible.
Expected/Actual Behavior
If I run my app using the following ps1 file:
$env:DAPR_APP_PORT = 5002
$env:DAPR_APP_ID = "MyApp"
dapr run --app-id $env:DAPR_APP_ID --app-port $env:DAPR_APP_PORT --app-protocol grpc -- dotnet run --no-build --framework netcoreapp3.1
I am able to call another service using DaprClient.InvokeMethodGrpcAsync<TRequest, TResponse>().
However, when I structure the ps1 file like this:
$env:DAPR_APP_PORT = 5002
$env:DAPR_APP_ID = "MyApp"
dotnet run --no-build --framework netcoreapp3.1
dapr run --app-id $env:DAPR_APP_ID --app-port $env:DAPR_APP_PORT --app-protocol grpc
I get the following exception when executing the same code as in the first scenario:
System.Net.Sockets.SocketException
HResult=0x80004005
Message=No connection could be made because the target machine actively refused it.
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
The Dapr console log doesn't seem to indicate any issue, other then a warning that no application command is found (which is correct since we run the application separately):
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://0.0.0.0:5002
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: <root path removed for privacy reasons>
←[37;1mWARNING: no application command found.←[0m
Starting Dapr with id MyApp. HTTP Port: 51524. gRPC Port: 51525
time="2021-05-11T12:56:16.7641704+02:00" level=info msg="starting Dapr Runtime -- version 1.1.2 -- commit 3148f36" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7680055+02:00" level=info msg="log level set to: info" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7682984+02:00" level=info msg="metrics server started on :51526/" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.metrics type=log ver=1.1.2
time="2021-05-11T12:56:16.7714937+02:00" level=info msg="standalone mode configured" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7715984+02:00" level=info msg="app id: MyApp" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.77164+02:00" level=info msg="mTLS is disabled. Skipping certificate request and tls validation" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7756055+02:00" level=info msg="local service entry announced: MyApp -> 10.232.4.10:51535" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.contrib type=log ver=1.1.2
time="2021-05-11T12:56:16.7756055+02:00" level=info msg="Initialized name resolution to standalone" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7756055+02:00" level=info msg="waiting for all outstanding components to be processed" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7761934+02:00" level=info msg="all outstanding components processed" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.776523+02:00" level=info msg="enabled gRPC tracing middleware" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime.grpc.api type=log ver=1.1.2
time="2021-05-11T12:56:16.776523+02:00" level=info msg="enabled gRPC metrics middleware" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime.grpc.api type=log ver=1.1.2
time="2021-05-11T12:56:16.776523+02:00" level=info msg="API gRPC server is running on port 51525" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7770999+02:00" level=info msg="enabled metrics http middleware" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime.http type=log ver=1.1.2
time="2021-05-11T12:56:16.7770999+02:00" level=info msg="enabled tracing http middleware" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime.http type=log ver=1.1.2
time="2021-05-11T12:56:16.7770999+02:00" level=info msg="http server is running on port 51524" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7770999+02:00" level=info msg="The request body size parameter is: 4" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7776202+02:00" level=info msg="enabled gRPC tracing middleware" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime.grpc.internal type=log ver=1.1.2
time="2021-05-11T12:56:16.7776787+02:00" level=info msg="enabled gRPC metrics middleware" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime.grpc.internal type=log ver=1.1.2
time="2021-05-11T12:56:16.7782392+02:00" level=info msg="internal gRPC server is running on port 51535" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:16.7788158+02:00" level=info msg="application protocol: grpc. waiting on port 5002. This will block until the app is listening on that port." app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
You're up and running! Dapr logs will appear here.
time="2021-05-11T12:56:17.0911512+02:00" level=info msg="application discovered on port 5002" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
time="2021-05-11T12:56:17.091769+02:00" level=info msg="actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime.actor type=log ver=1.1.2
time="2021-05-11T12:56:17.0931772+02:00" level=info msg="dapr initialized. Status: Running. Init Elapsed 322.2055ms" app_id="MyApp" instance=<instance name removed for privacy reasons> scope=dapr.runtime type=log ver=1.1.2
Steps to Reproduce the Problem
- Create an application that uses service invocation to call another application
- Run the application as shown earlier
Hi @wouterroos
When you use dapr run .... - dotnet run to launch your application there are a non-obvious things that are happening:
dapr runwill choose a random port for GRPC and HTTP communicationdapr runwill pass this into your application using the environment variablesDAPR_HTTP_PORTandDAPR_GRPC_PORThttps://docs.dapr.io/reference/environment/- The .NET SDK (in your application) reads these environment variables and knows how to communicate with the dapr process.
If you do dapr run and launch your application separately, you will need to choose values for these port numbers and configure both dapr and your application code correctly to match.
If you're looking for advice on putting together a dev setup, I'd suggest starting here: https://docs.dapr.io/developing-applications/sdks/dotnet/dotnet-development/
LMK if something additional should be answered or included in that documentation.
@rynowak thanks, that did the trick. Perhaps it's useful to add a pointer to this in the CLI docs I mentioned. As you mentioned, it's not really obvious how this works.