起因
在前面在Asp.Net Core调用WebSerivce,只是尝试使用,是为老项目移植做准备。在这
在Asp.Net Core调用WebSerivce ,正好前一段时间在GitHub看到
https://github.com/dotnet/wcf/issues/654 说VS要支持WebService同步调用,在昨天更新了VS2019 16.1版本,昨天一直在忙,今天抽空看了一下果然可以支持同步调用。
配置服务
主要在客户端选项中,选择生成同步操作。其他还是一样的。
使用
在Startup.cs文件中,在ConfigureServices注册服务
services.AddTransient(x => new MyServiceSoapClient(MyServiceSoapClient.EndpointConfiguration.MyServiceSoap));
public class HomeController : Controller
{
private MyServiceSoapClient _myservice;
public HomeController(MyServiceSoapClient myService)
{
_myservice = myService;
}
public IActionResult Index()
{
string result = _myservice.GetDataByNId(123);
return Content(result);
}
}