user3266638
user3266638

Reputation: 449

Can I use a web api from a web application as a service reference?

Is it possible to add as a reference and call an APIs controller methods as a service on another project? What are the alternatives if this is not possible?

Upvotes: 1

Views: 966

Answers (2)

Scott Hannen
Scott Hannen

Reputation: 29222

When you create a service reference you end up with a reference to an interface and a client class that implements the interface.

You can follow pretty much the same pattern without a WCF service reference. In fact, that's one of the benefits of depending on an interface. It doesn't matter to your application whether the implementation is a call to a WCF service, an API, or anything else.

First declare an interface that describes how you will interact with the API.

public interface ISomethingService
{
    public SomeData GetSomeData(string id);
} 

That interface is what your other classes depend on. They'll never know what the implementation is.

Your implementation could be something like this. I'm using RestSharp to create the API client because I like it better than managing an HttpClient:

public class SomethingServiceApiClient : ISomethingService
{
    private readonly string _baseUrl;

    public SomethingServiceApiClient(string baseUrl)
    {
        _baseUrl = baseUrl;
    }

    public SomeData GetSomeData(string id)
    {
        var client = new RestClient(_baseUrl);
        var request = new RestRequest($"something/{id}", Method.POST);
        var response = client.Execute<SomeData>(request);
        return response.Data;
    }
}

In your startup you would register this class as the implementation of ISomethingService and pass the base url from configuration. That would also allow you to pass a different url for development, production, etc. if needed.

Ultimately it's no different from depending on a WCF service. One difference is that a WCF service defines an interface, but in this case you have to do it. That's actually a good thing, because it's better for your application to define its own interface rather than directly depending on the ones someone else provides. You can wrap their interface or API in a class that implements your own interface, giving you control over the interface you depend on.

Upvotes: 0

douglas.kirschman
douglas.kirschman

Reputation: 146

Web API types of applications do not have a 'service reference' anymore. They do not produce WSDL, so you cannot add them like you used to do with SOAP services. No proxy classes are generated... no intelli-sense.

Web APIs are typically called with lightweight http requests and return JSON and not XML based SOAP responses like traditional ASMX or SVC (WCF) services.

You have some reading to do I believe.

To answer your question, you CAN indeed call API services from a web application (say a controller method in an MVC app), but you won't have proxy classes to help you.

https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

Upvotes: 1

Related Questions