WCF REST服务不返回DataContract对象

时间:2011-01-19 04:53:47

标签: wcf datacontract

我有一个由以下ServiceContract定义的简单WCF服务:

[ServiceContract]
public interface IInventoryService
{
  [OperationContract]      
  Item GetItemFromBarcode(string barcode);

  [OperationContract]
  string Test(string testString);
}

使用如下定义的项目:

[DataContract]
public class Item
{
  [DataMember]
  public virtual int Id { get; set; }

  <Snip>
}

实际服务如此实施:

public class InventoryService : IInventoryService
{
  [WebGet(UriTemplate = "/Barcode/{barcode}/Item", ResponseFormat = WebMessageFormat.Json)]
  public Item GetItemFromBarcode(string barcode)
  {
     var item = (from b in repository.Query<ItemBarcode>()
             where b.BarcodeData == barcode
             select b.Item).FirstOrDefault();
     return item;

  }

  [WebGet(UriTemplate = "/Test/{testString}",ResponseFormat=WebMessageFormat.Xml)]
  public string Test(string testString)
  {
     return testString;
  }
}

在托管服务的程序的app.config中有以下内容:

<system.serviceModel>      
  <behaviors>
     <serviceBehaviors>
        <behavior>
           <serviceMetadata httpGetEnabled="true"/>
        </behavior>
     </serviceBehaviors>
     <endpointBehaviors>
        <behavior name="RESTFriendly">
           <webHttp />
        </behavior>
     </endpointBehaviors>
  </behaviors>
  <services>
     <service name="InventoryService">            
        <endpoint address="/Inventory" behaviorConfiguration="RESTFriendly" binding="webHttpBinding" contract="IInventoryService"/>
        <host>
           <baseAddresses>
              <add baseAddress="http://localhost:8080/"/>
           </baseAddresses>
        </host>
     </service>
  </services>
</system.serviceModel>

现在代码转储已经完成了,问题是:我可以使用curl或Fiddler调用Test方法,它会返回一个序列化的字符串。但是,调用返回对象的方法不会返回任何内容。 Curl吐回curl: (56) Failure when receiving data from the peer,Fiddler回复ReadResponse() failed: The server did not return a response for this request.

从我读到的,这应该是Just Work(tm)。有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:1)

因此,您似乎不能拥有DataMember的接口类型,例如IList<ItemBarcode>。我希望将我的NHibernate模型对象用作DTO。