Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从WCF服务器返回的对象为空_C#_Wcf_Automapper_Dto - Fatal编程技术网

C# 从WCF服务器返回的对象为空

C# 从WCF服务器返回的对象为空,c#,wcf,automapper,dto,C#,Wcf,Automapper,Dto,在客户机应用程序中,我输入了一个项目id,WCF服务器应该返回类型为ItemDTO的对象。但是,当作出答复时,对象是空的 ItemDTO itm = Connection.Instance.HttpProxy.GetItem(ItemID); 到目前为止,我可以证实的是: 已成功从数据库中获取请求的项目 映射确实有效(检查了结果映射的内容) 内容确实已发送(使用Fiddler检查) 但是,当我检查itm的内容时,它是空的 这是WCF服务器中的GetItem()方法: public Ite

在客户机应用程序中,我输入了一个项目id,WCF服务器应该返回类型为
ItemDTO
的对象。但是,当作出答复时,对象是空的

ItemDTO itm = Connection.Instance.HttpProxy.GetItem(ItemID);
到目前为止,我可以证实的是:

  • 已成功从数据库中获取请求的项目
  • 映射确实有效(检查了结果映射的内容)
  • 内容确实已发送(使用Fiddler检查)

  • 但是,当我检查
    itm
    的内容时,它是空的

这是WCF服务器中的
GetItem()
方法:

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);
    }
}
连接类别:

 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();
    }
}

最后修复了它,解决方案非常简单

只需为DTO类(在本例中为
ItemDTO
)的两端提供一个名称空间即可。

i、 e:
[DataContract]
[DataContract(名称空间“PoSDTO”)]

对我来说,这表明主机端发生了错误,但现在异常或web错误返回给了您。您可以尝试使用Fiddler调试通信。有时Fiddler会收到客户无法接收的错误消息。另外,在主机端打开日志记录并检查错误。
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; }
}
 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();
    }
}
[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; }
}