問題描述
如何在使用 WCF(或 WSE 3)的 Web 服務客戶端應用程序的請求和響應中混合消息編碼類型(文本/MTOM)? (How do I mix message encoding types (Text/MTOM) in the Request & Response of a Web Service client application using WCF (or WSE 3)?)
這是我的問題。我正在訪問一個 Web 服務(託管在基於 Java 的服務器上),它只接受文本編碼的請求,但它返回 MTOM 響應。我發現如果我將 Web 服務設置為 RequireMtom,它會發送一個 Mtom 請求!不幸的是,服務器在 Mtom 請求上阻塞並返回 500 錯誤。但是,如果我將其設置為文本消息編碼,則響應會正確返回,並帶有一個多部分 MIME (MTOM) 響應,該響應會導致 Microsoft Web 服務 API 出錯(下面的示例錯誤)。它期待一個文本編碼的響應,因為請求是文本編碼的。我只想在響應中使用 RequireMtom。任何人都可以在這裡幫助我嗎?
正如您在下面的錯誤中看到的(發生在標準 Web 服務 API、WCF 或 WSE3 中),當我使用文本編碼發送請求時,
參考解法
方法 1:
Yes you can send a text message and get an mtom reply(or vise versa)with WCF.
see...
http://social.msdn.microsoft.com/Forums/en‑US/wcf/thread/480f1bc4‑1fc4‑40e9‑a2ed‑efcf3009d6ef
方法 2:
I researched a bit about this and the bad news is that from WCF point of view the request and response MUST use the same enconding. So yeah, the answer above is quite correct. You have to organize with service provider to enable MTOM on both request and response. The MTOM request will not have any impact on his application, apart from changing the SOAP mime‑type, as far as I know.
方法 3:
Here is a reply I got on another forum. it basically says that the problem is with Axis2 on the Java side. Unfortunately, that is not under my control in this case.
cherry111 ‑ Posted on Saturday, November 15, 2008 12:52:08 AM
You need change the configuration on your AXIS2 web service. You may know you can enable MTOM at two places. One is in service.xml and the other is in the axis.xml. Java recommends you set it in service.xml, but .net wse3.0 client does not like it. If you enable MTOM in axis.xml, it should work.
方法 4:
Yes WCF has that mandated requirement and NO, because you can work around it like i did.
I wrote a MultiContentTypeMessageEncoder that encapsulates 3 different encoders text, mtom and fi. I also plan to encapsulate a gpb encoder if possible and if there is a good reason to do so, in the future
方法 5:
[WSE 3.0] You can define a new class that inherits from your WSE3 proxy object with a single override method GetWebResponse.
In GetWebResponse method, you can simply choose if you want to use the mtom encoding according to the response's content‑type.
After that, you have to use this proxy class instead of the generated one.
PS: The WSE3 proxy class have to be generated using wsewsdl3 as a WebClient (add this option /type:webClient to the commend line).
Reference: http://www.codeproject.com/Tips/46257/Solution‑to‑WSE‑error‑for‑WSE‑clients‑needing.
(by komma8.komma1、Eric Schlosser、Wagner Silveira、komma8.komma1、Siddharth Sawe、Zayani Chiheb)