简体   繁体   English

我可以使用来自 Web 应用程序的 Web API 作为服务引用吗?

[英]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?是否可以作为参考添加并调用 API 控制器方法作为另一个项目的服务? What are the alternatives if this is not possible?如果这是不可能的,有什么替代方案?

Web API types of applications do not have a 'service reference' anymore. Web API 类型的应用程序不再具有“服务引用”。 They do not produce WSDL, so you cannot add them like you used to do with SOAP services.它们不生成 WSDL,因此您不能像过去使用 SOAP 服务那样添加它们。 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. Web API 通常使用轻量级 http 请求调用并返回 JSON,而不是像传统 ASMX 或 SVC (WCF) 服务那样基于 XML 的 SOAP 响应。

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.要回答您的问题,您确实可以从 Web 应用程序(例如 MVC 应用程序中的控制器方法)调用 API 服务,但您将没有代理类来帮助您。

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

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.您可以在没有 WCF 服务引用的情况下遵循几乎相同的模式。 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.对于您的应用程序来说,实现是调用 WCF 服务、API 还是其他任何东西都无关紧要。

First declare an interface that describes how you will interact with the API.首先声明一个接口,描述您将如何与 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 :我正在使用RestSharp创建 API 客户端,因为我更喜欢它而不是管理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.在您的启动中,您会将此类注册为ISomethingService的实现,并从配置中传递基本 url。 That would also allow you to pass a different url for development, production, etc. if needed.如果需要,这也将允许您为开发、生产等传递不同的 url。

Ultimately it's no different from depending on a WCF service.最终它与依赖 WCF 服务没有什么不同。 One difference is that a WCF service defines an interface, but in this case you have to do it.一个区别是 WCF 服务定义了一个接口,但在这种情况下您必须这样做。 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.您可以将它们的接口或 API 包装在实现您自己的接口的类中,从而使您能够控制所依赖的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM