2011-01-19 80 views
1

我有以下的ServiceContract定义一个简单的WCF服务:WCF REST服务没有返回DataContract对象

[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> 

既然代码转储出的方式,这个问题:我可以调用Test方法只是卷曲或提琴手精细,它返回一个序列化的字符串。但是,调用返回对象的方法不会返回任何内容。卷曲吐回curl: (56) Failure when receiving data from the peer和提琴手响应ReadResponse() failed: The server did not return a response for this request.

从我读的,这应该只是工作(tm)。有什么明显的我失踪了吗?

+0

GetItemFromBarcode中可能发生异常吗?也许修改为只返回一个内联创建的Item来确保你的WCF配置是正确的,因为WCF REST中的异常处理有点模糊。或者,Item类的其余部分是什么,也许是序列化问题?你也可以打开WCF跟踪。 – 2011-01-19 05:25:09

+0

@James Webster,我不能抛出异常(我告诉Visual Studio打破所有异常)。其实,我想我只是想出了这个问题。 – 2011-01-19 05:28:24

回答

1

因此,看起来你不能有一个接口类型DataMember,如​​。我希望将我的NHibernate模型对象作为DTO使用。