Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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 - Fatal编程技术网

C# 从WCF服务返回我的对象。

C# 从WCF服务返回我的对象。,c#,wcf,C#,Wcf,我有一个承载WCF服务的应用程序,我想返回这个类对象: namespace classes { [DataContract] public class NetworkAdapter { [DataMember] public string Name { get; set; } [DataMember] public string ID { get; set; } [DataMember]

我有一个承载WCF服务的应用程序,我想返回这个类对象:

namespace classes
{
    [DataContract]
    public class NetworkAdapter
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string ID { get; set; }
        [DataMember]
        public string Description { get; set; }
        [DataMember]
        public string IPAddress { get; set; }
        [DataMember]
        private string gatewayIpAddress;
        [DataMember]
        public string Speed { get; set; }
        [DataMember]
        public string NetworkInterfaceType { get; set; }
        [DataMember]
        public string MacAddress { get; set; }
        [DataMember]
        private LivePacketDevice livePacketDevice;
        [DataMember]
        public PacketDevice PacketDevice { get { return livePacketDevice; } }

        public NetworkAdapter(LivePacketDevice packetDevice)
        {
            livePacketDevice = packetDevice;
        }

        public override string ToString()
        {
            return Description;
        }

        public static NetworkAdapter[] getAll()
        {
            List<NetworkAdapter> list = new List<NetworkAdapter>();
            foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
                foreach (UnicastIPAddressInformation uniCast in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (!System.Net.IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6)
                    {
                        StringBuilder gatewayIPAddresses = new StringBuilder();
                        string gatewayIPAddressesDisplay = string.Empty;
                        foreach (var address in adapter.GetIPProperties().GatewayAddresses)
                        {
                            gatewayIPAddresses.Append(address.Address);
                            gatewayIPAddresses.Append(" ");
                        }

                        if (gatewayIPAddresses.Length > 0)
                        {
                            gatewayIPAddressesDisplay = gatewayIPAddresses.ToString().TrimEnd(' ');
                        }

                        if (!list.Any(l => l.ID == adapter.Id))
                        {
                            list.Add(new NetworkAdapter(getDevice(adapter.Id))
                            {
                                Name = adapter.Name,
                                ID = adapter.Id,
                                Description = adapter.Description,
                                IPAddress = uniCast.Address.ToString(),
                                NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(),
                                Speed = adapter.Speed.ToString("#,##0"),
                                MacAddress = getMacAddress(adapter.GetPhysicalAddress().ToString()),
                                gatewayIpAddress = gatewayIPAddressesDisplay
                            });
                        }
                    }
                }

            //return list.GroupBy(n => n.ID).Select(g => g.FirstOrDefault()).ToArray();
            return list.ToArray();
        }

        private static LivePacketDevice getDevice(string id)
        {
            return LivePacketDevice.AllLocalMachine.First(x => x.Name.Contains(id));
        }

        private static string getMacAddress(string oldMAC)
        {
            int count = 0;
            string newMAC = oldMAC;

            for (int i = 2; i < oldMAC.Length; i += 2)
            {
                newMAC = newMAC.Insert(i + count++, ":");
            }

            return newMAC;
        }

        public string defaultGateway
        {
            get
            {
                if (gatewayIpAddress != "")
                {
                    return gatewayIpAddress;
                }

                return "n/a";
            }
        }

        private static string getIpFourthSegment(string ipAddress)
        {
            try
            {
                string[] arr = ipAddress.Split('.');
                return arr[3];
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
当尝试运行
GetDate
函数时,它工作正常并返回简单字符串。
也许我需要以其他方式配置我的类?我已经向每个成员添加了
[DataMember]
LivePacketDevice
,并且
PacketDevice
需要是
[DataContract]
的,而如果它们只是
[Serializable]
的话,它也可以工作。否则,WCF不知道如何将它们传输到客户端


此外,建议只传输仅保存数据的对象,而不传输功能,因为客户端将无法使用该功能。在客户端创建的存根将只包含数据字段,而不包含方法,因为代码不会被传输/克隆。

我建议您拆分数据并尝试只返回一个“虚拟”对象。换句话说,不要依赖于
getAll
方法。为什么要将
[DataMember]
放在私有字段(
livePacketDevice
)上,并且
PacketDevice
是可序列化的对象?调试wcf服务的一个良好开端是添加一些跟踪。请参阅:user@Alberto:no他们不是
[Serializable]
对于
[DataContract]
来说是不必要的。它工作得很好without@Franck我知道。这就是为什么我写它们必须是
[DataContract]
。我不确定的是,它们是否可以只是
[Serializable]
,而不是
[DataContract]
。(这也是为什么我会这样评论鲁德雷斯的答案)。
[ServiceContract()]
public interface IService1
{
    [OperationContract]
    NetworkAdapter[] GetAdapters();

    [OperationContract]
    string GetDate();
}

[ServiceBehavior(
    ConcurrencyMode = ConcurrencyMode.Multiple,
    InstanceContextMode = InstanceContextMode.PerSession)]

public class service1 : IService1
{
    public NetworkAdapter[] GetAdapters()
    {
        IEnumerable<NetworkAdapter> adapters = NetworkAdapter.getAll();
        return adapters.ToArray();
    }

    public string GetDate()
    {
        return DateTime.Now.ToString();
    }
}
An unhandled exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll

Additional information: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9599960'.