ASP.NET Core中间件(Middleware)进阶学习实现SOAP 解析。

本篇将介绍实现ASP.NET Core SOAP服务端解析,而不是ASP.NET Core整个WCF host。

因为WCF中不仅仅只是有SOAP, 它还包含很多如消息安全性,生成WSDL,双工信道,非HTTP传输等。

ASP.NET Core 官方推荐大家使用RESTful Web API的解决方案提供网络服务。

SOAP 即 Simple Object AccessProtocol 也就是简单对象访问协议。

SOAP 呢,其指导理念是“唯一一个没有发明任何新技术的技术”,

是一种用于访问 Web 服务的协议。

因为 SOAP 基于XML 和 HTTP ,其通过XML 来实现消息描述,然后再通过 HTTP 实现消息传输。

SOAP 是用于在应用程序之间进行通信的一种通信协议。

因为是基于 XML 和HTTP 的,所以其独立于语言,独立于平台,并且因为 XML 的扩展性很好,所以基于 XML 的 SOAP 自然扩展性也不差。

通过 SOAP 可以非常方便的解决互联网中消息互联互通的需求,其和其他的 Web 服务协议构建起 SOA 应用的技术基础。

下面来正式开始 ASP.NET Core 实现SOAP 服务端解析。

新建项目

首先新建一个ASP.NET Core Web Application -》 SOAPService 然后再模板里选择 Web API。

然后我们再添加一个Class Library -》 CustomMiddleware

实现

下面我们来实现功能,首先在类库项目中添加以下引用

Install-Package Microsoft.AspNetCore.Http.Abstractions
Install-Package System.ServiceModel.Primitives
Install-Package System.Reflection.TypeExtensions
Install-Package System.ComponentModel

首先新建一个 ServiceDescription、ContractDescription和OperationDescription 类,这里需要注意的是ServiceDescription,ContractDescription和OperationDescription这里使用的是不能使用 System.ServiceModel.Description命名空间中的类型。它们是示例中简单的新类型。

ServiceDescription.cs

public class ServiceDescription

{

public Type ServiceType { get; private set; }

public IEnumerable<ContractDescription> Contracts { get; private set; }

public IEnumerable<OperationDescription> Operations => Contracts.SelectMany(c => c.Operations);

public ServiceDescription(Type serviceType)

{

ServiceType = serviceType;

var contracts = new List<ContractDescription>();

foreach (var contractType in ServiceType.GetInterfaces())

{

foreach (var serviceContract in contractType.GetTypeInfo().GetCustomAttributes<ServiceContractAttribute>())

{

contracts.Add(new ContractDescription(this, contractType, serviceContract));

}

}

Contracts = contracts;

}

}

ContractDescription.cs

public class ContractDescription

{

public ServiceDescription Service { get; private set; }

public string Name { get; private set; }

public string Namespace { get; private set; }

public Type ContractType { get; private set; }

public IEnumerable<OperationDescription> Operations { get; private set; }

public ContractDescription(ServiceDescription service, Type contractType, ServiceContractAttribute attribute)

{

Service = service;

ContractType = contractType;

Namespace = attribute.Namespace ?? "http://tempuri.org/"; // Namespace defaults to http://tempuri.org/

Name = attribute.Name ?? ContractType.Name; // Name defaults to the type name

var operations = new List<OperationDescription>();

foreach (var operationMethodInfo in ContractType.GetTypeInfo().DeclaredMethods)

{

foreach (var operationContract in operationMethodInfo.GetCustomAttributes<OperationContractAttribute>())

{

operations.Add(new OperationDescription(this, operationMethodInfo, operationContract));

}

}

Operations = operations;

}

}

OperationDescription.cs

public class OperationDescription

{

public ContractDescription Contract { get; private set; }

public string SoapAction { get; private set; }

public string ReplyAction { get; private set; }

public string Name { get; private set; }

public MethodInfo DispatchMethod { get; private set; }

public bool IsOneWay { get; private set; }

public OperationDescription(ContractDescription contract, MethodInfo operationMethod, OperationContractAttribute contractAttribute)

{

Contract = contract;

Name = contractAttribute.Name ?? operationMethod.Name;

SoapAction = contractAttribute.Action ?? $"{contract.Namespace.TrimEnd('/')}/{contract.Name}/{Name}";

IsOneWay = contractAttribute.IsOneWay;

ReplyAction = contractAttribute.ReplyAction;

DispatchMethod = operationMethod;

}

}

添加完成后下面来新建一个中间件 SOAPMiddleware ,对于新建中间件可以参考我之前的文章:http://www.cnblogs.com/linezero/p/5529767.html

SOAPMiddleware.cs 代码如下:

public class SOAPMiddleware

{

private readonly RequestDelegate _next;

private readonly Type _serviceType;

private readonly string _endpointPath;

private readonly MessageEncoder _messageEncoder;

private readonly ServiceDescription _service;

private IServiceProvider serviceProvider;

public SOAPMiddleware(RequestDelegate next, Type serviceType, string path, MessageEncoder encoder,IServiceProvider _serviceProvider)

{

_next = next;

_serviceType = serviceType;

_endpointPath = path;

_messageEncoder = encoder;

_service = new ServiceDescription(serviceType);

serviceProvider = _serviceProvider;

}

public async Task Invoke(HttpContext httpContext)

{

if (httpContext.Request.Path.Equals(_endpointPath, StringComparison.Ordinal))

{

Message responseMessage;

//读取Request请求信息

var requestMessage = _messageEncoder.ReadMessage(httpContext.Request.Body, 0x10000, httpContext.Request.ContentType);

var soapAction = httpContext.Request.Headers["SOAPAction"].ToString().Trim('\"');

if (!string.IsNullOrEmpty(soapAction))

{

requestMessage.Headers.Action = soapAction;

}

//获取操作

var operation = _service.Operations.Where(o => o.SoapAction.Equals(requestMessage.Headers.Action, StringComparison.Ordinal)).FirstOrDefault();

if (operation == null)

{

throw new InvalidOperationException($"No operation found for specified action: {requestMessage.Headers.Action}");

}

//获取注入的服务

var serviceInstance = serviceProvider.GetService(_service.ServiceType);

//获取操作的参数信息

var arguments = GetRequestArguments(requestMessage, operation);

// 执行操作方法

var responseObject = operation.DispatchMethod.Invoke(serviceInstance, arguments.ToArray());

var resultName = operation.DispatchMethod.ReturnParameter.GetCustomAttribute<MessageParameterAttribute>()?.Name ?? operation.Name + "Result";

var bodyWriter = new ServiceBodyWriter(operation.Contract.Namespace, operation.Name + "Response", resultName, responseObject);

responseMessage = Message.CreateMessage(_messageEncoder.MessageVersion, operation.ReplyAction, bodyWriter);

httpContext.Response.ContentType = httpContext.Request.ContentType;

httpContext.Response.Headers["SOAPAction"] = responseMessage.Headers.Action;

_messageEncoder.WriteMessage(responseMessage, httpContext.Response.Body);

}

else

{

await _next(httpContext);

}

}

private object[] GetRequestArguments(Message requestMessage, OperationDescription operation)

{

var parameters = operation.DispatchMethod.GetParameters();

var arguments = new List<object>();

// 反序列化请求包和对象

using (var xmlReader = requestMessage.GetReaderAtBodyContents())

{

// 查找的操作数据的元素

xmlReader.ReadStartElement(operation.Name, operation.Contract.Namespace);

for (int i = 0; i < parameters.Length; i++)

{

var parameterName = parameters[i].GetCustomAttribute<MessageParameterAttribute>()?.Name ?? parameters[i].Name;

xmlReader.MoveToStartElement(parameterName, operation.Contract.Namespace);

if (xmlReader.IsStartElement(parameterName, operation.Contract.Namespace))

{

var serializer = new DataContractSerializer(parameters[i].ParameterType, parameterName, operation.Contract.Namespace);

arguments.Add(serializer.ReadObject(xmlReader, verifyObjectName: true));

}

}

}

return arguments.ToArray();

}

}

public static class SOAPMiddlewareExtensions

{

public static IApplicationBuilder UseSOAPMiddleware<T>(this IApplicationBuilder builder, string path, MessageEncoder encoder)

{

return builder.UseMiddleware<SOAPMiddleware>(typeof(T), path, encoder);

}

public static IApplicationBuilder UseSOAPMiddleware<T>(this IApplicationBuilder builder, string path, Binding binding)

{

var encoder = binding.CreateBindingElements().Find<MessageEncodingBindingElement>()?.CreateMessageEncoderFactory().Encoder;

return builder.UseMiddleware<SOAPMiddleware>(typeof(T), path, encoder);

}

}

这里对于输出的消息做了一个封装,以输出具有正确的元素名称的消息的主体。

添加一个 ServiceBodyWriter 类。

public class ServiceBodyWriter : BodyWriter

{

string ServiceNamespace;

string EnvelopeName;

string ResultName;

object Result;

public ServiceBodyWriter(string serviceNamespace, string envelopeName, string resultName, object result) : base(isBuffered: true)

{

ServiceNamespace = serviceNamespace;

EnvelopeName = envelopeName;

ResultName = resultName;

Result = result;

}

protected override void OnWriteBodyContents(XmlDictionaryWriter writer)

{

writer.WriteStartElement(EnvelopeName, ServiceNamespace);

var serializer = new DataContractSerializer(Result.GetType(), ResultName, ServiceNamespace);

serializer.WriteObject(writer, Result);

writer.WriteEndElement();

}

}

这里对于中间件整个就完成了。

服务端

在服务端使用,这里你也可以新建一个Web 项目。

因为刚刚我们已经新建好了一个Web API项目,我们就直接拿来使用。

首先添加 CustomMiddleware 引用

在 SOAPService 中添加一个 CalculatorService 类

这里我为了方便将接口契约也放在CalculatorService 中,你也可以新建一个接口。

然后在 Startup.cs  的 ConfigureServices 中注入 CalculatorService

public void ConfigureServices(IServiceCollection services)
        {            // Add framework services.            services.AddMvc();services.AddScoped<CalculatorService>();}

在Configure 方法中加入中间件

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

{

loggerFactory.AddConsole(Configuration.GetSection("Logging"));

loggerFactory.AddDebug();

//加入一个/CalculatorService.svc 地址,绑定Http

app.UseSOAPMiddleware<CalculatorService>("/CalculatorService.svc", new BasicHttpBinding());

app.UseMvc();

}

这样就完成了服务端编写。

客户端

新建一个 Console Application -》SOAPClient

添加如下引用:

Install-Package System.ServiceModel.Primitives
Install-Package System.Private.ServiceModel
Install-Package System.ServiceModel.Http

Program代码如下:

编写好以后,分别对应到目录使用dotnet run执行程序。

成功建立了连接,也有返回。也就实现SOAP 的解析。

示例代码GitHub:https://github.com/linezero/Blog/tree/master/SOAPService

参考文档:https://blogs.msdn.microsoft.com/dotnet/2016/09/19/custom-asp-net-core-middleware-example/

原文地址:http://www.cnblogs.com/linezero/p/aspnetcoresoap.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

ASP.NET Core中间件(Middleware)实现WCF SOAP服务端解析相关推荐

  1. ASP.NET Core -中间件(Middleware)使用

    ASP.NET Core开发,开发并使用中间件(Middleware). 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 每个组件选择是否传递给管道中的下一个组件的请求,并能之前和下一组 ...

  2. ASP.NET Core 中间件Diagnostics使用

    ASP.NET Core 中间件(Middleware)Diagnostics使用.对于中间件的介绍可以查看之前的文章ASP.NET Core 开发-中间件(Middleware). Diagnost ...

  3. ASP.NET Core 中间件(Middleware)详解

    ASP.NET Core 中间件(Middleware)详解 原文:ASP.NET Core 中间件(Middleware)详解 本文为官方文档译文,官方文档现已非机器翻译 https://docs. ...

  4. ASP.NET Core中间件初始化探究

    前言 在日常使用ASP.NET Core开发的过程中我们多多少少会设计到使用中间件的场景,ASP.NET Core默认也为我们内置了许多的中间件,甚至有时候我们需要自定义中间件来帮我们处理一些请求管道 ...

  5. ASP.NET Core 中间件

    1.前言 中间件(middleware)是一种装配到应用管道以处理请求和响应的组件.每个组件: ●可选择是否将请求传递到管道中的下一个组件. ●可在管道中的下一个组件前后执行工作. 请求委托(requ ...

  6. 利用Asp.Net Core的MiddleWare思想处理复杂业务流程

    最近利用Asp.Net Core 的MiddleWare思想对公司的古老代码进行重构,在这里把我的设计思路分享出来,希望对大家处理复杂的流程业务能有所帮助. 背景 一个流程初始化接口,接口中根据传入的 ...

  7. 如何一秒钟从头构建一个 ASP.NET Core 中间件

    前言 其实地上本没有路,走的人多了,也便成了路. -- 鲁迅 就像上面鲁迅说的那样,其实在我们开发中间件的过程中,微软并没有制定一些策略或者文档来约束你如何编写一个中间件程序, 但是其中却存在者一些最 ...

  8. 在ASP.NET Core使用Middleware模拟Custom Error Page功能

    一.使用场景 在传统的ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAtt ...

  9. ASP.NET Core 中间件分类

    ASP.NET Core 中间件的配置方法可以分为以上三种,对应的Helper方法分别是:Run(), Use(), Map(). Run(),使用Run调用中间件的时候,会直接返回一个响应,所以后续 ...

最新文章

  1. 防止SQL注入式攻击
  2. jenkins搭建流水线项目
  3. vue.js分页组件(新手学习记录)
  4. 测试学开发——第一课:java学习路程
  5. 再次详解clientHeight、offsetHeight、scrollHeight
  6. 编辑视频贴纸软件_视频特效编辑软件下载-视频特效编辑器下载V10.9.93-西西软件下载...
  7. DTCMS插件的制作实例电子资源管理(二)Admin后台页面编写
  8. Mysql优化碎片空间
  9. 【7】PR音频及结合AU去除噪音【8】PR字幕运用
  10. 快递查询API接口对接案例(顺丰、圆通、中通、德邦、天天)
  11. 当Activity设置为透明主题时,按Home键,Dialog闪烁的解决方法
  12. 山东高中学业水平考试时间2020计算机,2020年山东省高中学业水平等级考试报名时间及科目...
  13. 灰度共生矩阵(GLCM)计算速度快很多,用numpy写的
  14. 电脑主板有哪几种结构?
  15. php弱口令总结,web漏洞之弱口令
  16. 2o2021年安徽高考成绩查询,2021安徽高考数学答案-2021年安徽高考数学试题及答案...
  17. 重磅!全球Top 1000计算机科学家公布:张宏江居大陆科学家之首
  18. 云服务器nginx站点主页路径更改方法
  19. Hexo,创建属于你自己的博客
  20. FFmpeg花屏解决(修改源码,丢弃不完整帧)

热门文章

  1. 话里话外:ERP与PDM、MES的关系区别是什么
  2. Canvas的save和restore
  3. .NET Core Runtime vs .NET Framework Runtime
  4. .NET 6 中的 Logging Source Generator
  5. .NET 6新特性试用 | LINQ功能改进
  6. ML.NET Cookbook:(16)什么是规范化?为什么我需要关心?
  7. 探索 .Net Core 的 SourceLink
  8. Prism for WPF 搭建一个简单的模块化开发框架
  9. .NET 5 的 Target Framework 详解[上篇]
  10. 基于.NetCore3.1系列 —— 日志记录之自定义日志组件