1

最初の WCF Web サービスを作成しました。渡されたパラメーターに基づいて、そこから画像を返そうとしています。エラーが発生しています:

応答メッセージのコンテンツ タイプ image/jpeg がバインディングのコンテンツ タイプ (text/xml; charset=utf-8) と一致しません。カスタム エンコーダーを使用する場合は、IsContentTypeSupported メソッドが適切に実装されていることを確認してください。

この問題を解決するにはどうすればよいですか?

サービスを呼び出している私の Web サイトの web.config ファイルには、次の構成があります。

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IRestImageService" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:59473/RestImageService.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRestImageService"
    contract="RestImageService.IRestImageService" name="BasicHttpBinding_IRestImageService" />
</client>

Web サービスの web.config は次のようになります。

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>

サービス契約:

[OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "Image/{type}/{typeid}/{imageid}/{size}/{extension}",
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    Stream Image(string type, string typeid, string imageid, string size = "lrg", string extension = "jpg");

私は WCF で非常に新しいので、ヘルプ/ポインターは大歓迎です!

更新 ティムの提案を実装した後、新しいエラーが発生しました:

メッセージを受け入れることができる localhost:59473/RestImageService.svc でリッスンしているエンドポイントはありませんでした。これは、多くの場合、アドレスまたは SOAP アクションが正しくないことが原因です。詳細については、InnerException (存在する場合) を参照してください。

この問題を修正するために Web サービスを構成する方法がわかりません。助言がありますか?

これは、Web サービスにアクセスする方法です。

RestImageServiceClient client = new RestImageServiceClient();
    client.Image(WSC.Common.BO.User.User.ImageFolder.Buyer, "27085", "BuyerPhoto", "LRG", "jpg");

イメージが機能したら、イメージの src タグを Web サービスの URL に設定できるようにしたいと考えています。

4

1 に答える 1

1

basicHttpBindingSOAP (バージョン 1.1) です。REST ベースのサービスを有効にするには、webHttpBinding.

私はこのようなことを試してみます。サービスの構成ファイルで、次の変更を行います。

<protocolMapping>
  <add binding="webHttpBinding" scheme="http"/>
</protocolMapping>

これにより、デフォルトのバインディングが通話用に設定されますwebHttpBindinghttp

<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

上記のコードは、 のデフォルトのエンドポイント動作を追加する必要webHttpがあります。

最後に、クライアント構成ファイルで、次の変更を行います。

<client>
  <endpoint address="http://localhost:59473/RestImageService.svc"
            binding="webHttpBinding"
            contract="RestImageService.IRestImageService" 
            name="WebHttpBinding_IRestImageService" />
</client>

これがうまくいくとは断言できませんが、私は (SOAP 側で) WCF を使って多くのことを行ってきたので、少なくとも正しい方向に向けられると思います。

編集

RestImageServiceClientSOAP クライアントです。REST サービスを使用するには、HTTP API を使用する必要があります。[WCF REST Service with JSON] http://www.codeproject.com/Articles/327420/WCF-REST-Service-with-JSONの例を次に示します。

WebClient client = new WebClient();
byte[] data = client.DownloadData("http://localhost:11523/Service1.svc/GetData");
Stream stream = new MemoryStream(data);

DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
string result = obj.ReadObject(stream).ToString();

「WCF REST JSON Client exmaple」をグーグルで検索することをお勧めします。これを行うには、多くのヒットといくつかの異なる方法が得られます。

追加のメモとして、SOAP エンドポイントがサービスで公開されている限り、SOAP クライアントで SOAP 呼び出しを行う ことができます。

于 2013-09-01T03:05:34.013 に答える