私は、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を返すために別のメソッドを記述しているわけではありませんか?
ありがとうございます!