私はソフトウェアプロジェクトの移行段階としてWCFインターフェイスを使用して既存のASMX Webサービスをラップして時間を節約しています。これは、System.Stringを返す1つの関数を除いてうまく機能します。WCFが返しましたSystem.IO.Streamが文字化けしました
元のASMXサービスは、指定されたパラメータに応じてテキストまたはXMLを返しました。これはASMXの問題ではありませんでした。 WCFでは、XMLの場合、返された値は<gml>
のようにエスケープされます。ここでは<gml>
になります。下のSOAPを参照してください。
要求
POST http://someuri.org/WebServices/Utils.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: http://www.someuri.org/IUtils/Function
Content-Length: 283
Accept: */*
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Host: foo.bar.org
Connection: Keep-Alive
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Function xmlns="http://www.someri.org/">
<type>...</type>
<input1>...</input1>
<input2>...</input2>
<input3>true</input3>
</Function >
</s:Body>
</s:Envelope>
応答
HTTP/1.1 200 OK
Date: Fri, 04 May 2012 11:40:01 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 2070
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<FunctionResponse xmlns="http://www.crotec.nl/">
<FunctionResult><gml>data</gml></FunctionResult>
</FunctionResponse>
</s:Body>
</s:Envelope>
いくつかのグーグルは、System.IO.Streamオブジェクトを返すに私をもたらしました。
string result = DoStuff(arg1, arg2, arg3);
byte[] bin = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new System.IO.MemoryStream(bin);
これはある点に作用します。
string result = "02010415DBD800D7E17577787A626978";
byte[] bin = {48,50,48,49,48,52,49,53,68,66,68,56,48,48,68,55,69,49,55,53,55,55,55,56,55,65,54,50,54,57,55,56};
SOAPメッセージで返された結果は、しかしです:
MDIwMTA0MTVEQkQ4MDBEN0UxNzU3Nzc4N0E2MjY5Nzg=
だから、結果の出力が文字化けしている(再び、私は(メッセージのエンコードによって引き起こされる、と思う)?)
メソッドは、OperationContractを持つattrであり、このサービスはIIS6で次のABCでホストされます。
<service name="WebServices.BeheerUtils" behaviorConfiguration="Services.ServiceBehavior">
<!-- Service Endpoints -->
<endpoint address="" binding="basicHttpBinding" contract="WebServices.IUtils"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
なぜ出力が文字化けしているのか、またはHTMLエンコーディングを防止する方法は?
インタフェース
[OperationContract]
System.IO.Stream Function(string type, string input1, string input2, string input3);
実装
public new System.IO.Stream Function(string type, string input1, string input2, string input3)
{
// Call the old ASMX method
string result = DoStuff(type, input1, input2, input3, true);
byte[] bin = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new System.IO.MemoryStream(bin);
}
テキスト 'MDIwMTA0MTVEQkQ4MDBEN0UxNzU3Nzc4N0E2MjY5Nzg ='はbase64でエンコードされた値の '02010415DBD800D7E17577787A626978'です。 – empi
あなたのWCFサービスインターフェイスに関する詳細を投稿できますか?少なくともメソッド定義。私は実際にasmxを使用して文字列またはxmlを返すことができ、WCFを使用することができない理由を理解できません(xmlは文字列ではありませんか?)。 – empi
更新していただきありがとうございますが、私の2番目の質問に答えることができますか?文字列を返すだけの理由はありませんか? – empi