目录
- 构建
- 前置准备
- 编写服务
- 挂载服务
- 处理SoapHeader
- 调用
- 添加服务
- 调用服务
- 补充内容
构建
前置准备
框架版本要求: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 a+b;
}
}
如果输入输出参数是一个类,需要这么声明
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.TryAddSingleton<ISampleService, SampleService>();
//other code
app.UseRouting();
app.UseEndpoints(endpoints =>
{
//注册webservice
endpoints.UseSoapEndpoint<ISampleService>("/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(){
headerValue=new HeaderValue();
}
/// <summary>
/// 从BODY解析SoapHeader,SoapCore没有提供解析Header,因此从客户端提交的XML内容里取出SoapHeader
/// </summary>
/// <param name="body"></param>
public 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.Cast<XmlNode>().FirstOrDefault(n => n.LocalName == "Header")?.FirstChild;
if (node == null)
{
return;
}
var cns = node.ChildNodes.Cast<XmlNode>();
headerValue.Token=cns.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 a+b;
}
}
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
{
Token="this 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来源