0

In the client application, I input an item id, and the WCF server is supposed to return an object of type ItemDTO. However, when the reply is made, the object is empty.

ItemDTO itm = Connection.Instance.HttpProxy.GetItem(ItemID);

What I can confirm so far:

  • The item requested is successfully taken from the database
  • The mapping did work (checked the contents of the resulting mapping)
  • The contents was indeed sent (checked using Fiddler) enter image description here

  • However, when I check the contents of itm, it's empty.

This is the GetItem() method in the WCF Server:

public ItemDTO GetItem(string itemID) {
    using (var db = new PoSEntities()) {
        var query = from i in db.Items
                    where i.ItemID.Equals(itemID)
                    select i;
        Item item = query.FirstOrDefault();
        return item == null ? null : Mapping.Map<Item, ItemDTO>(item);
    }
}

I use AutoMapper to map the entity object to the DTO:

    public static H Map<T, H>(T i) {
        Mapper.CreateMap<T, H>();
        return Mapper.Map<T, H>(i);
    }

The Item class is generated by Entity Framework:

public partial class Item
{
    public Item()
    {
        this.Sal1 = new HashSet<Sal1>();
    }

    public string ItemID { get; set; }
    public string Name { get; set; }
    public string Manufacturer { get; set; }
    public int StockQuantity { get; set; }
    public decimal Price { get; set; }

    public virtual ICollection<Sal1> Sal1 { get; set; }
}

Connection class:

 public sealed class Connection {

    private readonly string _address = "http://Edwin:8080/PoS";
    private static Connection _instance;
    private static object _padLock = new Object();
    private static ChannelFactory<IPoS> httpFactory;

    private static IPoS _httpProxy; //Singleton
    public IPoS HttpProxy { get { return _httpProxy; } }

    public static Connection Instance {
        get {
            if (_instance == null) {
                lock (_padLock) {
                    if (_instance == null) {
                        _instance = new Connection();
                    }

                }
            }
            return _instance;
        }
    }

    private Connection() {
        httpFactory = new ChannelFactory<IPoS>(
            new BasicHttpBinding(), 
            new EndpointAddress(_address));
        _httpProxy = httpFactory.CreateChannel();
    }
}

ItemDTO Class:

[DataContract]
public class ItemDTO {
    [DataMember]
    public string ItemID { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Manufacturer { get; set; }
    [DataMember]
    public int StockQuantity { get; set; }
    [DataMember]
    public decimal Price { get; set; }
}
4

1 回答 1

0

终于修好了,解决方法很简单。

ItemDTO只需在两端为 DTO 类(在本例中为)提供一个命名空间。
即:[DataContract][DataContract(Namespace"PoSDTO")]

于 2013-06-14T10:34:05.373 回答