C#uses Refit to connect to the WebService interface

C#uses Refit to connect to the WebService interface

Group friends said that. NET Core could not connect with Web Services, so the webmaster found some information and hoped to help him

最后更新 3/15/2023 8:33 PM
沙漠尽头的狼
预计阅读 3 分钟
分类
.NET
标签
.NET C# Refit WebService

Refit is a powerful type-safe RESTful HTTP client library that helps us communicate easily with Web APIs. However, in this problem, we need to use Refit to communicate with the Web Service, so we need to make some specific configurations for Refit.

The following is an example of using Refit to invoke the Web Service interface:

  1. First, you need to add a reference to the Refit library in the project. You can search for Refit and install it through the NuGet package manager.

  2. Then we need to define an interface to describe the Web Service interface, such as:

public interface IMyWebService
{
    [Post("/MyWebService.asmx")]
    Task<string> MyWebServiceMethod(string param1, string param2);
}

其中 [Post] 指定了调用的 HTTP 方法和 URL,Task<string> 是方法返回类型。

  1. 接下来,我们需要使用 Refit 的 RestService.For 方法创建一个客户端实例:
var client = RestService.For<IMyWebService>("http://example.com");

其中 "http://example.com" 是 Web Service 的地址。

  1. Finally, we can use the client instance to invoke the Web Service interface:
var result = await client.MyWebServiceMethod("param1", "param2");

其中 result 是 Web Service 方法的返回值。

需要注意的是,由于 Web Service 接口不是基于 RESTful 架构的,因此需要进行一些特定的配置。例如,在接口定义中使用 [Post] 指定调用的 HTTP 方法为 POST,同时需要将 Web Service 方法的名称作为 URL 的一部分,例如:

public interface IMyWebService
{
    [Post("/MyWebService.asmx/MyWebServiceMethod")]
    Task<string> MyWebServiceMethod(string param1, string param2);
}

In addition, you need to specify the SOAP 1.1 namespace for the Web Service in the client instance, for example:

var client = RestService.For<IMyWebService>("http://example.com", new RefitSettings
{
    UrlParameterFormatter = new SoapUrlParameterFormatter(),
    ContentSerializer = new XmlContentSerializer(new RefitXmlSerializerSettings
    {
        Namespace = "http://schemas.xmlsoap.org/soap/envelope/",
        UseXmlSerializerFormat = true
    })
});

在这里,我们使用了 SoapUrlParameterFormatter 来处理 URL 中的参数,使用了 XmlContentSerializerRefitXmlSerializerSettings 来处理请求和响应的 XML 数据。

In short, using Refit to invoke the Web Service interface requires some specific configuration, but as long as you follow the above example, the docking can be easily completed.

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读