ASP.NETCore中间件(Middleware)进阶学习实现SOAP解析。
本篇将介绍实现ASP.NETCoreSOAP服务端解析,而不是ASP.NETCore整个WCFhost。
因为WCF中不仅仅只是有SOAP,它还包含很多如消息安全性,生成WSDL,双工信道,非HTTP传输等。
ASP.NETCore官方推荐大家使用RESTfulWebAPI的解决方案提供网络服务。
SOAP即SimpleObjectAccessProtocol也就是简单对象访问协议。
SOAP呢,其指导理念是“唯一一个没有发明任何新技术的技术”,
是一种用于访问Web服务的协议。
因为SOAP基于XML和HTTP,其通过XML来实现消息描述,然后再通过HTTP实现消息传输。
SOAP是用于在应用程序之间进行通信的一种通信协议。
因为是基于XML和HTTP的,所以其独立于语言,独立于平台,并且因为XML的扩展性很好,所以基于XML的SOAP自然扩展性也不差。
通过SOAP可以非常方便的解决互联网中消息互联互通的需求,其和其他的Web服务协议构建起SOA应用的技术基础。
下面来正式开始ASP.NETCore实现SOAP服务端解析。
新建项目首先新建一个ASP.NETCoreWebApplication-》SOAPService然后再模板里选择WebAPI。
然后我们再添加一个ClassLibrary-》CustomMiddleware
实现下面我们来实现功能,首先在类库项目中添加以下引用
Install-PackageMicrosoft.AspNetCore.HTTP.AbstractionsInstall-PackageSystem.ServiceModel.PrimitivesInstall-PackageSystem.Reflection.TypeExtensionsInstall-PackageSystem.ComponentModel
首先新建一个ServiceDescription、ContractDescription和OperationDescription类,这里需要注意的是ServiceDescription,ContractDescription和OperationDescription这里使用的是不能使用System.ServiceModel.Description命名空间中的类型。它们是示例中简单的新类型。
ServiceDescription.cs
publicclassServiceDescription
{
publicTypeServiceType{get;privateset;}
publicIEnumerableContractDescriptionContracts{get;privateset;}
publicIEnumerableOperationDescriptionOperations=Contracts.SelectMany(c=c.Operations);
publicServiceDescription(TypeserviceType)
{
ServiceType=serviceType;
varcontracts=newListContractDescription();
foreach(varcontractTypeinServiceType.GetInterfaces())
{
foreach(varserviceContractincontractType.GetTypeInfo().GetCustomAttributesServiceContractAttribute())
{
contracts.Add(newContractDescription(this,contractType,serviceContract));
}
}
Contracts=contracts;
}
}
ContractDescription.cs
publicclassContractDescription
{
publicServiceDescriptionService{get;privateset;}
publicstringName{get;privateset;}
publicstringNamespace{get;privateset;}
publicTypeContractType{get;privateset;}
publicIEnumerableOperationDescriptionOperations{get;privateset;}
publicContractDescription(ServiceDescriptionservice,TypecontractType,ServiceContractAttributeattribute)
{
Service=service;
ContractType=contractType;
Namespace=attribute.Namespace??"儿童白癜风能治得好吗中科治白癜风疗效更显著