网站建设需要做些什么,网站加入地图导航,做羊水亲子鉴定网站,数商云是外包吗目录构建前置准备编写服务挂载服务处理SoapHeader调用添加服务调用服务补充内容构建
前置准备
框架版本要求#xff1a;netcore3.1以上 引入nuget包 SoapCore
编写服务
1.编写服务接口 示例
using System.ServiceModel;namespace Services;[ServiceContract(Namespace netcore3.1以上 引入nuget包 SoapCore
编写服务
1.编写服务接口 示例
using System.ServiceModel;namespace Services;[ServiceContract(Namespace http://Demo.WebService/)]public interface ISampleService{[OperationContract]int CalcAdd(int a,int b);
}2.继承服务接口 示例
public class SampleService:ISampleService{public int CalcAdd(int a,int b){return ab;}
}如果输入输出参数是一个类需要这么声明
using System.Runtime.Serialization;namespace Models;[DataContract]
public class ReturnValue
{[DataMember]public int Id { get; set; }[DataMember]public string Name { get; set; }
}
挂载服务
.net6\7写法
//other code
builder.Services.AddSoapCore();
builder.Services.TryAddSingletonISampleService, SampleService();
//other code
app.UseRouting();
app.UseEndpoints(endpoints
{//注册webserviceendpoints.UseSoapEndpointISampleService(/SampleService.asmx, new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
});至此最小案例已完成启动服务后任意支持webservice的客户端可通过SampleService.asmx调用服务
处理SoapHeader
1.创建承载Header的类 示例
public class HeaderValue{public string Token{get;set;}
}2.创建ServiceBase
public class ServiceBase{public HeaderValue headerValue;public ServiceBase(){headerValuenew HeaderValue();}/// summary/// 从BODY解析SoapHeaderSoapCore没有提供解析Header因此从客户端提交的XML内容里取出SoapHeader/// /summary/// param namebody/parampublic void ParseHeaderFromBody(Stream body){body.Seek(0, SeekOrigin.Begin);using var reader new StreamReader(body);var xml reader.ReadToEndAsync().GetAwaiter().GetResult();if (string.IsNullOrEmpty(xml)){return;}var envelope new XmlDocument();envelope.LoadXml(xml);var node envelope.DocumentElement?.ChildNodes.CastXmlNode().FirstOrDefault(n n.LocalName Header)?.FirstChild;if (node null){return;}var cns node.ChildNodes.CastXmlNode();headerValue.Tokencns.FirstOrDefault(d d.LocalName Token)?.InnerText;}
}3.所有服务继承ServiceBase 示例
public class SampleService:ServiceBase,ISampleService{public int CalcAdd(int a,int b){Console.WriteLine($headervalue-token:{headerValue.Token});return ab;}
}4.创建ServiceOperationTuner
public class ServiceOperationTuner : IServiceOperationTuner{public void Tune(HttpContext httpContext, object serviceInstance, OperationDescription operation){var service serviceInstance as ServiceBase;service.ParseHeaderFromBody(httpContext.Request.Body);}}5.挂载ServiceOperationTuner
builder.Services.AddSoapServiceOperationTuner(new ServiceOperationTuner());6.其它配置
//全局启用流缓冲
app.Use((context, next)
{context.Request.EnableBuffering();return next();
});调用
添加服务 一路下一步就行了
调用服务
//初始化client,连接地址是服务发现里填的那个如果想改地址查看这个构造函数的其它重载
var _SampleServiceClient new SampleServiceClient();
//处理SoapHeader
using var scope new OperationContextScope(_SampleServiceClient.InnerChannel);
//客户端准备好同样的HeaderValue类var header MessageHeader.CreateHeader(headerValue, http://Demo.WebService/, new HeaderValue{Tokenthis is token});
OperationContext.Current.OutgoingMessageHeaders.Add(header);
//调用服务里的函数
_SampleServiceClient.CalcAdd(1,2);补充内容
1.netcore创建的webservice没有主动声明SoapHeader所以客户端想携带Header就要绕一下 2.soapheader和http header是俩个概念soapheader存在于服务请求的body中http header存在于服务请求的协议报文中 3.SoapCore来源