从Postman向WCF服务发送MTOM编码请求遇问题求助
问题分析与解决方案
首先得指出你遇到的核心问题:端点地址配置错误导致WSDL生成了重复的服务地址,这直接引发了后续的请求异常。接下来一步步帮你解决:
1. 修正端点地址配置
看你的Web.config里的服务端点配置:
<endpoint address="ResponseService.svc" contract="MySvc.IResponseService" binding="wsHttpBinding" bindingConfiguration="wsHttpMtomBinding"></endpoint>
这里的address属性设置成了"ResponseService.svc",而WCF会自动把服务的基地址(也就是ResponseService.svc所在的根地址)和这个address拼接起来,结果就生成了http://localhost:50809/ResponseService.svc/ResponseService.svc这种重复且无效的地址。
你需要把address改成空字符串(或者直接省略这个属性,默认就是空):
<endpoint address="" contract="MySvc.IResponseService" binding="wsHttpBinding" bindingConfiguration="wsHttpMtomBinding"></endpoint>
修改后重新启动服务,再访问WSDL地址http://localhost:50809/ResponseService.svc?wsdl,就能看到<soap12:address>里的location变成了正确的http://localhost:50809/ResponseService.svc。
2. 调整Postman请求地址
现在你应该向http://localhost:50809/ResponseService.svc发送POST请求,而不是那个重复的地址。
3. Postman测试MTOM的关键细节
即使地址正确了,发送MTOM请求还要注意这些点,不然服务可能还是无法正确解析:
- Content-Type必须严格匹配:保持你提供的格式,但注意Postman里不需要对action的引号做额外转义,直接写
action="urn:test:test1:ResponseInputMessage"即可 - Payload要完整:你给出的示例Payload缺少了核心的SOAP信封内容,第一个multipart部分需要包含完整的SOAP XML,比如:
--uuid:83d3b2a6-5437-4366-bfb1-6f7c8b49add5 Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml; action=\"urn:test:test1:ResponseInputMessage\"" Content-Transfer-Encoding: binary Content-ID: <root@example.org> <soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <!-- 这里填写与RegistryResponseType对应的XML结构内容 --> </soap12:Body> </soap12:Envelope> --uuid:83d3b2a6-5437-4366-bfb1-6f7c8b49add5-- - 请求方法必须是POST:wsHttpBinding默认使用POST方法,Postman里不要选错请求类型。
额外排查点
如果调整后还是有问题,可以做这些检查:
- 确认服务没有编译错误,已经正常启动
- 查看Web.config里的
protocolMapping,你配置了https绑定,但测试用的是http,如果服务没有配置HTTPS证书,可能需要把protocolMapping改成<add binding="wsHttpBinding" scheme="http" /> - 开启WCF跟踪日志(在Web.config里添加跟踪配置),日志能帮你定位具体的解析错误或服务异常
内容的提问来源于stack exchange,提问作者Vladimirs
相关产品推荐
相关产品推荐

