当前位置: 首页 > news >正文

【WebService】C#搭建的标准WebService接口,在使ESB模版作为参数无法获取参数数据

一、问题说明

1.1 问题描述

使用C# 搭建WebService接口,并按照ESB平台人员的要求,将命名空间改为"http://esb.webservice",使用Postman+ESB平台人员提供的入参示例进行测试时,callBussiness接口参数message始终为null

以下是ESB平台提供的模版

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://esb.webservice"><soapenv:Header/><soapenv:Body><esb:callBussiness><!--Optional:--><message><![CDATA[...]]></message></esb:callBussiness></soapenv:Body>
</soapenv:Envelope>

1.2 C# WebService代码

using System;
using System.IO;
using System.Text;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
using Rss_WebServer.code;namespace ESB
{/// <summary>/// WebService 的摘要说明/// </summary>[WebService(Namespace = "http://esb.webservice")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService]public class WebService : System.Web.Services.WebService{[WebMethod(Description = "调用业务")]public string callBussiness(string message){return message; }}
}

1.3 Postman 测试参数

  • POST http://localhost:55305/WebService.asmx?op=callBussiness
  • Headers
KEYVALUEDESCRIPTION
Content-Typetext/xml; charset=utf-8
SOAPAction“http://esb.webservice/callBussiness”
  • Body
    • raw XML(text/xml)
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://esb.webservice"><soapenv:Header/><soapenv:Body><esb:callBussiness><!--Optional:--><message><![CDATA[<root><author>少莫千华</author><email>370763160@qq.com</email></root>]]></message></esb:callBussiness></soapenv:Body>
</soapenv:Envelope>

在这里插入图片描述

在这里插入图片描述

二、问题分析

根本问题是ESB平台提供的参数模版并没有完全按照标准的WebService协议进行编写,导致使用官方搭建的WebService接口无法正常的解析参数,所以要想解决此问题,有两个途径:

  • 与ESB平台人员沟通,要求其标准化参数模版
  • 自己重新解构WebService参数

三、解决方案

3.1 与ESB平台人员沟通,要求其标准化参数模版

3.1.1 标准模版 - 使用命名空间缩写

callBussiness接口的message参数添加命名空间缩写esb

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://esb.webservice"><soapenv:Header/><soapenv:Body><esb:callBussiness><!--Optional:--><esb:message><![CDATA[...]]></esb:message></esb:callBussiness></soapenv:Body>
</soapenv:Envelope>

3.1.1 标准模版 - 使用完整的命名空间

callBussiness接口的使用完整的命名空间<callBussiness xmlns="http://esb.webservice">

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soapenv:Body><callBussiness xmlns="http://esb.webservice"><!--Optional:--><message><![CDATA[...]]></message></callBussiness></soapenv:Body>
</soapenv:Envelope>

3.2 自己重新解构WebService参数

从请求对象base.Context.Request中重新获取所有Body内容(InputStream),然后再进行自定义解析。
:因为是搭建的标准的WebService接口Body内容(InputStream)在进去函数内部前已经被SOAP协议解析过一次,所以InputStream起始内容位置Position 指向数据流的结尾,所以在读取之前,先要将InputStream起始内容位置Position设置为0,否则读取的内容为空('')

using System;
using System.IO;
using System.Text;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
using Rss_WebServer.code;
namespace ESB
{/// <summary>/// WebService 的摘要说明/// </summary>[WebService(Namespace = "http://esb.webservice")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService]public class WebService : System.Web.Services.WebService{[WebMethod(Description = "调用业务")]public string callBussiness(string message){try{if (string.IsNullOrEmpty(message)){message = WebServiceAnalysis(base.Context.Request, nameof(message));}return message;}catch(Exception exp){return exp.Message;}}/// <summary>/// 重新解析 WebService/// </summary>/// <param name="request"></param>/// <param name="name"></param>/// <returns></returns>private string WebServiceAnalysis(System.Web.HttpRequest request,string name){try{if(request.ContentLength == 0){throw new Exception($"Body(xml数据) 无数据");}// 获取请求内容Stream inputStream = request.InputStream;// 重新获取内容inputStream.Position = 0;// 读取请求主体内容using (StreamReader reader = new StreamReader(inputStream, Encoding.UTF8)){string requestBody = reader.ReadToEnd();XmlDocument xmlDoc = new XmlDocument();xmlDoc.LoadXml(requestBody);XmlNode strNode = xmlDoc.SelectSingleNode($"//{name}");if (strNode != null){return strNode.InnerText;}else{throw new Exception($"未在Body(xml数据)找到{name}节点");}}}catch(Exception exp){throw exp;}}}
}
http://www.lryc.cn/news/186148.html

相关文章:

  • Sqlserver关于tempdb临时数据库文件个数的最佳实践
  • 【Java】微服务——微服务介绍和Eureka注册中心
  • C++ virtual 虚函数 虚基类
  • redis分布式秒杀锁
  • 【Redis】String内部编码方式
  • 川西旅游网系统-前后端分离(前台vue 后台element UI,后端servlet)
  • Paddle使用pyinstaller打包出错的解决方法
  • 【Java acm】特殊输入
  • 在Ubuntu 20.04搭建最小实验环境
  • 使用uwsgi部署Flask
  • Android平台实现lottie动画
  • JAVA练习百题之求矩阵对角线之和
  • MEM备考打卡
  • 短视频矩阵源码开发部署---技术解析
  • 百度小程序制作源码 百度引流做关键词排名之技巧
  • 【计算机组成 课程笔记】7.3 高速缓存 Cache
  • vscode搭建c/c++环境
  • macOS Sonoma 14.0(23A344) 正式版带 OpenCore 0.9.6 和 FirPE 三分区镜像
  • 神经网络(MLP多层感知器)
  • git与github的交互(文件与文件夹的上传)
  • Visual Studio常见编译错误记录
  • 如何应对数据安全四大挑战?亚马逊云科技打出“组合拳”
  • JavaScript——数据类型、类型转换
  • C位操作符
  • 【linux进程(三)】进程有哪些状态?--Linux下常见的三种进程状态
  • numString.charAt(i) - ‘0‘
  • 《Python 自动化办公应用大全》书籍推荐(包邮送书五本)
  • day57:ARMday4,程序状态寄存器读写指令、软中断指令、C和汇编的混合编程、开发板介绍
  • el-cascader
  • 图论第3天----第841题、第463题