WCF服务调用返回数据但接收方为空

时间:2016-11-10 02:18:16

标签: c# .net vb.net web-services wcf

我正在调试基本上连接到我的sql db的wcf服务,收集数据并返回列表中的数据(调用时,调用时)。但是,我的客户端(调用者)获取空数据,即使当我单步执行wcf代码时,它也会返回数据。

以下是调用wcf服务的客户端代码。请注意,它在这里,当我设置断点并查看

  

GetServerUpdatesSyncOthers

在调用返回后,我的列表都是空的,当它们不应该是(是的,客户端在vb.net中,但是wcf服务在c#中)

  Dim svc As PocketPCServerClient = GetServiceClient()

  Dim updates As ServerUpdatesSyncDTO = svc.GetServerUpdatesSyncOthers(myDeviceId, timestamps.ToArray, operatorName, Me.IdMethodName(idMethod), idMethodValue, destinationHub, withLocationGroup)

对于记录,ServerUpdatesSyncDTO在客户端看起来像这样:

<DataContract()>
Public Class ServerUpdatesSyncDTO
Public Const EMPLOYEES_PROCESS_MAXIMUM_NONE As Integer = 0
Public Const EMPLOYEES_PROCESS_MAXIMUM_ALL As Integer = -1

<DataMember()>
Public Property SystemDateTimeUtc As DateTime
<DataMember()>
Public Property SyncTimestamps As List(Of SyncTimestampDTO)
<DataMember()>
Public Property DeviceSettings As List(Of DeviceSettingDTO)
<DataMember()>
Public Property Couriers As List(Of CourierDTO)
<DataMember()>
Public Property Validation As List(Of ValidationDTO)
<DataMember()>
Public Property NameVariants As List(Of String)
<DataMember()>
Public Property NameScrubs As List(Of String)
<DataMember()>
Public Property Notes As List(Of String)
<DataMember()>
Public Property Notifications As List(Of NotifyDTO)
<DataMember()>
Public Property Locations As List(Of LocationDTO)
<DataMember()>
Public Property UpsValids As List(Of UpsValidDTO)
<DataMember()>
Public Property StatusCodes As List(Of StatusCodeDTO)
<DataMember()>
Public Property HubRecon As List(Of String)
<DataMember()>
Public Property HubNames As List(Of String)
<DataMember()>
Public Property ExtCustomFieldNames As CustomFieldHeadersDTO
<DataMember()>
Public Property HoldPickupLocations As List(Of HoldPickupDTO)
<DataMember()>
Public Property EmployeeUpdates As List(Of EmployeeDTO)
<DataMember()>
Public Property EmployeeInserts As List(Of EmployeeDTO)
<DataMember()>
Public Property EmployeeDeletes As List(Of EmployeeDTO)
<DataMember()>
Public Property EmployeesProcessMore As Boolean
<DataMember()>
Public Property EmployeesProcessMoreIdGreaterThan As String
End Class

在wcf服务端,是ServerUpdatesSyncDTO的c#版本:

  [DataContract]
public class ServerUpdatesSyncDTO
{
    public const int EMPLOYEES_PROCESS_MAXIMUM_NONE = 0;
    public const int EMPLOYEES_PROCESS_MAXIMUM_ALL = -1;

        [DataMember]
        public DateTime SystemDateTimeUtc { get; set; }
        [DataMember]
        public List<SyncTimestampDTO> SyncTimestamps { get; set; }
        [DataMember]
        public List<DeviceSettingDTO> DeviceSettings { get; set; }
        [DataMember]
        public List<CourierDTO> Couriers { get; set; }
        [DataMember]
        public List<ValidationDTO> Validation { get; set; }
        [DataMember]
        public List<string> NameVariants { get; set; }
        [DataMember]
        public List<string> NameScrubs { get; set; }
        [DataMember]
        public List<string> Notes { get; set; }
        [DataMember]
        public List<NotifyDTO> Notifications { get; set; }
        [DataMember]
        public List<LocationDTO> Locations { get; set; }
        [DataMember]
        public List<UpsValidDTO> UpsValids { get; set; }
        [DataMember]
        public List<StatusCodeDTO> StatusCodes { get; set; }
        [DataMember]
        public List<EmployeeDTO> EmployeeUpdates { get; set; }
        [DataMember]
        public List<EmployeeDTO> EmployeeInserts { get; set; }
        [DataMember]
        public List<EmployeeDTO> EmployeeDeletes { get; set; }
        [DataMember]
        public bool EmployeesProcessMore { get; set; }
        [DataMember]
        public string EmployeesProcessMoreIdGreaterThan { get; set; }
        [DataMember]
        public List<string> HubRecon { get; set; }
        [DataMember]
        public List<string> HubNames { get; set; }
        [DataMember]
        public CustomFieldHeadersDTO ExtCustomFieldNames { get; set; }
        [DataMember]
        public List<HoldPickupDTO> HoldPickupLocations { get; set; }
    }

最后,客户端调用的wcf服务方法。请记住,我可以在这里设置一个断点并逐步完成,我看到这个方法每次都会返回有效数据,没有例外。例如,其中一个项目,一个Couriers列表。当它返回时我在wcf方面的列表中有48个,但是在调用返回后客户端在“更新”中显示为Couriers为空。

 public ServerUpdatesSyncDTO GetServerUpdatesSyncOthers(string deviceId, List<SyncTimestampDTO> syncTimestamps, string operatorName, string locationCriteriaType, string locationCriteria, string destinationHub, bool withLocationGroup)
    {
        var thisMethodName = MethodBase.GetCurrentMethod().Name;
        CurrentTaskName = thisMethodName;
        CurrentDeviceId = deviceId;

        if (string.IsNullOrWhiteSpace(deviceId))
            throw new FaultException(string.Format("Argument null [{0}]", "deviceId"));
        if (syncTimestamps == null)
            throw new FaultException(string.Format("Argument null [{0}]", "syncTimestamps"));
        if (string.IsNullOrWhiteSpace(operatorName))
            throw new FaultException(string.Format("Argument null [{0}]", "operatorName"));
        if (locationCriteriaType.ToUpper() != LOCATION_ID_METHOD_BY_EMPLOYEE && locationCriteriaType.ToUpper() != LOCATION_ID_METHOD_BY_NAME)
            throw new FaultException(string.Format("locationCriteriaType [{0}] is invalid. Valid values are {1} or {2}", locationCriteriaType, LOCATION_ID_METHOD_BY_EMPLOYEE, LOCATION_ID_METHOD_BY_NAME));
        if (string.IsNullOrWhiteSpace(locationCriteria) & locationCriteriaType != LOCATION_ID_METHOD_BY_EMPLOYEE)
            throw new FaultException(string.Format("Argument null [{0}]", "locationCriteria"));

        var result = new ServerUpdatesSyncDTO();

        try
        {
            WriteServerLog(SERVER_TASK_BEGIN);
            using (var conn = GetSQLConnection())
            {
                var homeLocationCode = 0;
                var loginLocationCode = 0;
                List<int> groupLocationCodes;

                if ((locationCriteriaType == LOCATION_ID_METHOD_BY_EMPLOYEE) && (string.IsNullOrWhiteSpace(locationCriteria)))
                {
                    locationCriteria = operatorName;
                }

                loginLocationCode = GetLocationCode(conn, null, locationCriteriaType, locationCriteria);
                groupLocationCodes = GetGroupLocationCodes(conn, null, loginLocationCode);

                if (locationCriteriaType == LOCATION_ID_METHOD_BY_EMPLOYEE)
                {
                    homeLocationCode = loginLocationCode;
                }
                else
                {
                    homeLocationCode = GetLocationCode(conn, null, LOCATION_ID_METHOD_BY_EMPLOYEE, operatorName);
                }

                var lastUpdateUtc = GetLocationSyncDateTimeUtc(loginLocationCode, syncTimestamps);
                result.Couriers = GetCourierUpdates(conn, lastUpdateUtc, loginLocationCode);
                result.Validation = GetValidationUpdates(conn, lastUpdateUtc, loginLocationCode);
                result.HubNames = GetHubNames(conn, lastUpdateUtc, loginLocationCode);

                result.NameVariants = GetNameVariantUpdates(conn, lastUpdateUtc);
                result.NameScrubs = GetNameScrubUpdates(conn, lastUpdateUtc);
                result.Notes = GetNotesUpdates(conn, loginLocationCode);
                result.Notifications = GetNotifyUpdates(conn, lastUpdateUtc);
                result.Locations = GetLocationUpdates(conn, lastUpdateUtc);
                result.UpsValids = GetUpsValidUpdates(conn, lastUpdateUtc);
                result.StatusCodes = GetStatusCodeUpdates(conn, lastUpdateUtc);
                result.ExtCustomFieldNames = GetExtendedCustomFieldHeaderUpdates(conn, lastUpdateUtc);

                if (Properties.Settings.Default.HoldForPickupUpdates)
                {
                    result.HoldPickupLocations = GetHoldPickupUpdates(conn);
                }
                else
                {
                    result.HoldPickupLocations = new List<HoldPickupDTO>();
                }

                if (!string.IsNullOrWhiteSpace(destinationHub))
                {
                    result.HubRecon = GetHubReconUpdates(conn, destinationHub);
                }
                else
                {
                    result.HubRecon = new List<string>();
                }

                result.DeviceSettings = GetDeviceSettingUpdates(conn, loginLocationCode, deviceId);

                result.SystemDateTimeUtc = DateTime.Now.ToUniversalTime();
                result.SyncTimestamps = GetSyncTimestampUpdates(result.SystemDateTimeUtc, syncTimestamps, loginLocationCode, groupLocationCodes, withLocationGroup);

            }

            WriteServerLog(SERVER_TASK_COMPLETE, true, true);
            return result;

        }
        catch (FaultException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            string exText = FormatException(ex, string.Format("{0}:{1}", Assembly.GetExecutingAssembly().GetName().Name, thisMethodName));
            WriteServerLog(thisMethodName, ServerLogDTO.LogTypeEnum.Critical, exText, ServerLogDTO.EventDataFormatEnum.MultilineText);
            throw new FaultException(exText);
        }
    }  

所以,我认为虽然客户端应该是vb并且服务是wcf应该没问题,但是当它返回数据时,vb肯定不喜欢某种不匹配?我只是没有看到它。数据类型都是相同的,它们都是列表。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。通常,客户端具有wcf服务的服务引用,它可以从服务中下载WSDL并为您创建客户端类。这可以通过参考&gt;来完成。添加服务引用并指向Web服务地址。

有两点需要注意,这对我来说都是一个问题。 1.您可以右键单击服务引用并进行更新。这将下拉并更新服务引用。如果对WCF服务进行了更改,那么这是必须的,以便引用所述服务的客户端也会得到更新。否则,界面会有所不同,事情也不会奏效。 2.添加或编辑服务引用时,可以选择使用哪种类型的集合。默认情况下,我被选为数组。但是,在WCF服务中,我没有使用数组,而是使用列表。如果与服务引用/客户端代码不匹配,则集合将被破坏。如果您更新服务引用,您应该收到警告,但就我而言,我还没有更新我的服务引用。

因此,在我更新了服务引用之后,我还选择了我使用Lists而不是数组。在那之后,一切都按预期工作。

希望这有帮助。