-
情况说明
客户提供一个SAP接口,接口通过浏览器可以打开查看,如下图:
输入帐号密码后登录:
-
接口开发
连接接口服务
上述情况,SAP接口已就绪,现在开始开发接口调用。首先,创建 .NET Core Web项目,然后如下图,连接接口服务。
创建访问接口的服务
访问方法
接口访问服务 SAPWebSevice 完整代码
using ServiceReference1; using System.ServiceModel; using WebAPI.Models; namespace WebAPI.Service { public class SAPWebSevice { private readonly IConfiguration _cofig; public AppSetting _appSetting = new(); private ZMES_GOODSMVTChannel callClient { get; set; } public SAPWebSevice(IConfiguration configuration) { _cofig = configuration; _cofig.Bind(_appSetting); //_dbContextFactory = dbContextFactory; // 创建 HTTP 绑定对象 var binding = new BasicHttpBinding(); binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; // 根据 WebService 的 URL 构建终端点对象 var wsUrl = _appSetting.SAPSetting.InstoreEndpointUrl; var endpoint = new EndpointAddress(wsUrl); // 创建调用接口的工厂,注意这里泛型只能传入接口 var factory = new ChannelFactory<ZMES_GOODSMVTChannel>(binding, endpoint); factory.Credentials.UserName.UserName = _appSetting.SAPSetting.SAPUserName; factory.Credentials.UserName.Password = _appSetting.SAPSetting.SAPPassword; // 从工厂获取具体的调用实例 callClient = factory.CreateChannel(); callClient.OperationTimeout = new TimeSpan(0, 20, 0); } /// <summary> /// SAP 成品入库服务 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task<ZmesGoodsmvtResponse> DoTransfer(Zsmesgoodsmvt input) { var rq = new ZmesGoodsmvtRequest() { ZmesGoodsmvt =new ZmesGoodsmvt() }; var data = new Zsmesgoodsmvt[] { new Zsmesgoodsmvt { Aufnr=input.Aufnr, Matnr=input.Matnr, Gamng=input.Gamng, Gmein=input.Gmein, Zpost=input.Zpost, Lgort=input.Lgort } }; rq.ZmesGoodsmvt.TItems = data; try { var rp =await callClient.ZmesGoodsmvtAsync(rq); var result = rp.ZmesGoodsmvtResponse; if (result == null) { //throw new Exception("调用结果为Null!"); result=new ZmesGoodsmvtResponse() { EStatus = "E", EMessage = "调用结果为Null!" }; } return result; } catch (Exception ex) { var result = new ZmesGoodsmvtResponse() { EStatus = "E", EMessage = ex.Message }; return result; } } } }View Code
服务创建完成,现在就是调用,我们创建一个控制器(API ) 来触发。
创建控制器,采有Post方法访接口
Post API 接口,调 SAPWebService 服务:
最后,在Program.cs 中注入服务。
至此,在 .NET Core 使用 API 通过访问 SAP WebSerVice 接口功能全部完成。
标签:Core,接口,result,new,var,input,NET,SAP From: https://www.cnblogs.com/lucky-sebill/p/17971198