0

次のようなWCFサービスがあります。

[ServiceContract]
public class SomeService
{
    [WebInvoke(UriTemplate = "/test", Method = "POST")]
    public string Test()
    {
        using (var reader = OperationContext.Current.RequestContext.RequestMessage.GetReaderAtBodyContents())
        {
            var content = reader.ReadOuterXml().Replace("<Binary>", "").Replace("</Binary>", "");
            return content;
        }
    }
}

そして、次のような構成ファイルがあります。

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Project.SomeService">
        <endpoint address="" binding="webHttpBinding" contract="Project.SomeService"
                  bindingConfiguration="webHttpBinding_SomeService" behaviorConfiguration="endpointBehavior_SomeService" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding_SomeService">
          <security mode="None"></security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="endpointBehavior_SomeService">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

しかし、POSTメソッドを使用してこのURLでフィドラーを使用して呼び出すと:

http://localhost:1111/SomeService.svc/Test

体で:

asdasd

代わりに返されますがYXNkYXNk、なぜこのようになったのですか?

私のコードは、C#、フレームワーク 4、VS2010Pro でビルドされています。

助けてください。前もって感謝します。

4

1 に答える 1

4

何かがbase64です-結果または要求のいずれかをエンコードします。のASCIIバイトは、base64でエンコードさasdasdれた場合と同じように出力されます。YXNkYXNk

ボディをどのように提供するかは明確ではありませんが、WireSharkまたはFiddlerを使用して正確な要求/応答を調べ、base64エンコーディングが発生している場所を特定し、その理由を特定してから修正することをお勧めします。

編集:今、私はあなたのコードを詳しく調べました、それはかなり明確なようです。

あなたのリクエストはおそらくバイナリBinaryデータを含むことを意図しています-それがあなたがXML内にタグを持っている理由です。あなたはそれを無視し、バイナリデータのXML表現をテキストとして扱うことにしましたが、そうすべきではありません。バイナリデータは、base64を介してXMLで表されます。したがって、次のことを行う必要があります。

  • 外側のXMLを文字列として取得してから文字列操作を実行するのではなく、XMLをXMLとして解析します
  • Binaryタグの内容を文字列として取得します
  • Convert.FromBase64String元のバイナリデータを取得するために使用します
  • バイナリデータが元々テキストであったと思われる場合はEncoding.GetString、を使用して元に戻します
于 2012-07-03T07:05:04.570 に答える