dotnet-architecture / eShopOnContainers

Cross-platform .NET sample microservices and container based application that runs on Linux Windows and macOS. Powered by .NET 7, Docker Containers and Azure Kubernetes Services. Supports Visual Studio, VS for Mac and CLI based environments with Docker CLI, dotnet CLI, VS Code or any other code editor. Moved to https://github.com/dotnet/eShop.
https://dot.net/architecture
24.53k stars 10.35k forks source link

Calling one service from another #117

Closed HarshaRaikar closed 7 years ago

HarshaRaikar commented 7 years ago

Hi,

How do I call a microservice function from another?

I want to call Catalog service function from another service .I am doing the following public async Task GetCatalogItem(string name) { _apiClient = new HttpClient();

        var catalogUrl = $"{_remoteServiceBaseUrl}GetCatalogItem/withname/{name}";

        var dataString = "";

        // work with HttpClient call
        dataString = await _apiClient.GetStringAsync(catalogUrl);

        //var dataString = await _apiClient.GetStringAsync(catalogUrl);
        var response = JsonConvert.DeserializeObject<CatalogItem>(dataString);

        return response;
    }

But I am getting the error : "CurlException: Couldn't connect to server"

Thanks -Harsha

CESARDELATORRE commented 7 years ago

For queries, you call it the same way you do it from the MVC app, for instance. Using HttpClient, similar code than what you have. The important point is to use the internal name resolution based on the names of the microservices, like we do: For instance, this is the env-var we use at the Catalog microservice, defined at the docker-compose.override.yml: CatalogUrl=http://**catalog.api**:5101 You could also use catalog.api within the C# code, if you'd like. In your code you have {_remoteServiceBaseUrl}, I guess it container the "/" needed? What's the value of {_remoteServiceBaseUrl} in your case?

In regards updates, or how to propagate changes between microservices, that communication should be asynchronous and based on events. We're building a sample EventBus in the dev branches using a RabbitMQ container for the underneath messaging, etc.

But for quering, Http with HttpClient is the most usual approach.

CESARDELATORRE commented 7 years ago

Closing this issue (question) until further notice