2009-11-17 17 views
17

私は、WCFサービスで使用されるメソッドを記述するServiceContractを持っています。このメソッドには、UriTemplateとResponseFormatを定義するWebGet属性があります。WCF OperationContractメソッドのWebGet属性に複数のResponseFormat型を持たせることはできますか?

私は単一のメソッドを再利用し、異なるUriTemplatesと異なるResponseFormatsを持つ複数のWebGet属性を持っています。基本的には、XMLとJSONのどちらかの戻り値の型のようなものを区別するために複数のメソッドを使用することを避けたいと考えています。私が今見たすべての例では、WebGetの属性ごとに異なるメソッドを作成する必要があります。

[ServiceContract] 
public interface ICatalogService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)] 
    [WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)] 
    Product GetProduct(string id); 
} 

ので、これを達成するための方法があります:ここで私はこのようなXMLとJSONの戻り値の型の両方のためにGetProductメソッドを使用したいのですが、上記の例を使用して

[ServiceContract] 
public interface ICatalogService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)] 
    Product GetProduct(string id); 

    [OperationContract] 
    [WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)] 
    Product GetJsonProduct(string id); 
} 

OperationContractサンプルです私は別のResponseFormatsを返すために別のメソッドを記述しているわけではありませんか?

ありがとうございます!

答えて

12

あなたはこの

[ServiceContract] 
public interface ICatalogService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "product/{id}/details?format={format}")] 
    Stream GetProduct(string id, string format); 
} 

を行うことができますそして、あなたのコードのハンドルの直列化にパラメータで指定した値をオフに基づいて。

XMLの場合は、シリアル化を処理するヘルパーメソッドを作成します。

public static Stream GetServiceStream(string format, string callback, DataTable dt, SyndicationFeed sf) 
     { 
      MemoryStream stream = new MemoryStream(); 
      StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); 
      if (format == "xml") 
      { 
       XmlSerializer xmls = new XmlSerializer(typeof(DataTable)); 
       xmls.Serialize(writer, dt); 
       WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
      } 
      else if (format == "json") 
      { 
       var toJSON = new JavaScriptSerializer(); 
       toJSON.RegisterConverters(new JavaScriptConverter[] { new JavaScriptDataTableConverter() }); 
       writer.Write(toJSON.Serialize(dt)); 
       WebOperationContext.Current.OutgoingResponse.ContentType = "text/json"; 
      } 
      else if (format == "jsonp") 
      { 
       var toJSON = new JavaScriptSerializer(); 
       toJSON.RegisterConverters(new JavaScriptConverter[] { new JavaScriptDataTableConverter() }); 
       writer.Write(callback + "(" + toJSON.Serialize(dt) + ");"); 
       WebOperationContext.Current.OutgoingResponse.ContentType = "text/json"; 
      } 
      else if (format == "rss") 
      { 
       XmlWriter xmlw = new XmlTextWriter(writer); 
       sf.SaveAsRss20(xmlw); 
       WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
      } 
      else if (format == "atom") 
      { 
       XmlWriter xmlw = new XmlTextWriter(writer); 
       sf.SaveAsAtom10(xmlw); 
       WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
      } 
      else 
      { 
       writer.Write("Invalid formatting specified."); 
       WebOperationContext.Current.OutgoingResponse.ContentType = "text/html"; 
      } 

      writer.Flush(); 
      stream.Position = 0; 
      return stream; 
     } 
} 
8

私の記憶が正しければ、法の下に私の仕事:JSONサービスの

契約:XMLサービスのための

[ServiceContract] 
public interface IServiceJson { 
    [OperationContract()] 
    [WebGet(UriTemplate = "Operation/?param={param}", 
         ResponseFormat = WebMessageFormat.Json)] 
    ReturnType Operation(string param); 
} 

連絡先:両方のための

[ServiceContract] 
public interface IServiceXml { 
    [OperationContract(Name = "OperationX")] 
    [WebGet(UriTemplate = "Operation/?param={param}", 
         ResponseFormat = WebMessageFormat.Xml)] 
    ReturnType Operation(string param); 
} 

実装:

public class ServiceImplementation : IServiceJson, IServiceXml { 
    ReturnType Operation(string param) { 
    // Implementation 
    } 
} 

そして、web.configファイルの設定(JSONやXML応答のためのノートエンドポイント):

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="webHttp"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="serviceBehaviour"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="serviceBehaviour" name="ServiceImplementation"> 
     <endpoint address="json/" behaviorConfiguration="webHttp" binding="webHttpBinding" 
     bindingConfiguration="webHttpBindingSettings" contract="IServiceJson"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="xml/" behaviorConfiguration="webHttp" binding="webHttpBinding" 
     bindingConfiguration="webHttpBindingSettings" contract="IServiceXml"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpBindingSettings"> 
      <readerQuotas maxStringContentLength="5000000"/> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    </system.serviceModel> 

は今、あなたはこのようなあなたのサービスを呼び出すことができます。 JSONレスポンス:http://yourServer/json/Operation/?param=value XMLレスポンス:http://yourServer/xml/Operation/?param=value

(上記のコードにバグがある場合は申し訳ありませんが、検証のために実行しませんでした)。

関連する問題