目录
构建
前置准备
编写服务
挂载服务
处理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);
}
1
2
3
4
5
6
7
8
9
2.继承服务接口
示例
public class SampleService:ISampleService{
public int CalcAdd(int a,int b){
return a+b;
}
}
1
2
3
4
5
如果输入输出参数是一个类,需要这么声明
using System.Runtime.Serialization;
namespace Models;
[DataContract]
public class ReturnValue
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
挂载服务
.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);
});
1
2
3
4
5
6
7
8
9
10
至此最小案例已完成,启动服务后,任意支持webservice的客户端可通过"SampleService.asmx"调用服务
处理SoapHeader
1.创建承载Header的类
示例
public class HeaderValue{
public string Token{get;set;}
}
1
2
3
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
3.所有服务继承ServiceBase
示例
public class SampleService:ServiceBase,ISampleService{
public int CalcAdd(int a,int b){
Console.WriteLine($"headervalue-token:{headerValue.Token}");
return a+b;
}
}
1
2
3
4
5
6
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);
}
}
1
2
3
4
5
6
7
8
5.挂载ServiceOperationTuner
builder.Services.AddSoapServiceOperationTuner(new ServiceOperationTuner());
1
6.其它配置
//全局启用流缓冲
app.Use((context, next) =>
{
context.Request.EnableBuffering();
return next();
});
1
2
3
4
5
6
调用
添加服务
一路下一步就行了
调用服务
//初始化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
2
3
4
5
6
7
8
9
10
11
12
补充内容
1.netcore创建的webservice没有主动声明SoapHeader,所以客户端想携带Header就要绕一下
2.soapheader和http header是俩个概念,soapheader存在于服务请求的body中,http header存在于服务请求的协议报文中
3.SoapCore来源
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/kw269937519/article/details/129037097
本博客Android APP 下载 |
![]() |
支持我们就给我们点打赏 |
![]() |
支付宝打赏 支付宝扫一扫二维码 |
![]() |
微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!