.net - Azure 序列化中的 DTO 问题

标签 .net azure azure-sql-database

首先我想为我的英语道歉

我的 DTO 存在问题,我使用 SQLAzure 作为数据库,并在 Azure 上托管服务

我的一些对象具有其他对象的 ICollection,通常一个国家(国家/地区表)具有机场(机场表)的集合,我使用 Entity Framework 进行数据库连接和 DTO 来呈现我的对象

这一点一切都很好,但是在客户端,当我想使用对象的属性 Icollection 时,VS 说“名称不明确”,似乎在序列化过程中它重新输入了我的对象

我在服务器端有 DTOAirport

序列化后我在客户端有两种机场 首先是 airportservice.airportClient.DTOAirport 第二个是countryService.countryClient.DTOAirport

我无法使用经典的 DTOAirport 并且它不支持 Actor ...

我在使用它时遇到了很多问题,而“经典 WCF 服务”则没有这些问题

如果需要的话我可以发布代码

非常感谢您阅读我的文章

{
[DataContract]
public class DTOAirport
{
    [DataMember]
    public int IdAirportDTO { get; set; }

    [DataMember]
    public string NameAirportDTO { get; set; }

    [DataMember]
    public string IATACodeDTO { get; set; }

    [DataMember]
    public string ICAOCodeDTO { get; set; }

    [DataMember]
    public string ZoneDTO { get; set; }

    [DataMember]
    public int FKCountryDTO { get; set; }

    [DataMember]
    public string CityDTO { get; set; }

    [DataMember]
    public double LatitudeDTO { get; set; }

    [DataMember]
    public double LongitudeDTO { get; set; }

    [DataMember]
    public Nullable<int> AltitudeDTO { get; set; }
}

}

{
[DataContract]
public class DTOCountry
{
    [DataMember]
    public int IdCountryDTO { get; set; }

    [DataMember]
    public string NameCountryDTO { get; set; }

    [DataMember]
    public ICollection<DTOAirport> AirportsListDTO { get; set; }
}

}

这是我的 DTO,我使用映射器将其映射到 Entity Framework 类

这是服务器端的典型服务

        public DTOAirport GetAirportById(int idAirport)
    {
        using (FlexAzureContainer1 context = new FlexAzureContainer1())
        {
            DTOAirport airport = new DTOAirport();
            airport = DTOAirportMapper.MapFrom(context.DM_AIRPORTS.Where(a => a.IdAirport == idAirport).FirstOrDefault());
            return airport;
        }
    }

        public DTOCountry GetCountryById(int idCountry)
    {
        using (FlexAzureContainer1 context = new FlexAzureContainer1())
        {
            DTOCountry country = new DTOCountry();
            country = DTOCountryMapper.MapFrom(context.DM_COUNTRIES.Where(c => c.IdCountry == idCountry).FirstOrDefault());
            return country;
        }
    }

这是客户端的控制台测试以及它给我的错误

public class Program
{
    static AirportService.AirportServiceClient myAirportService = new AirportService.AirportServiceClient();
    static CountryService.CountryServiceClient myCountryService = new CountryService.CountryServiceClient();

    public static void Main(string[] args)
    {
         List<DTOAirport>airportsList = myCountryService.GetCountryById(1).AirportsListDTO.ToList();
        DTOCountry country = myCountryService.GetCountryById(1);    
        DTOAirport airport = myAirportService.GetAirportById(1);
        foreach (DTOAirport airport2 in country.AirportsListDTO.ToList())
        {
            Console.WriteLine(airport2.NameAirportDTO);
        }
        Console.WriteLine(country.NameCountryDTO);
        Console.WriteLine(airport.NameAirportDTO);
        Console.ReadLine();

    }
}

}

Error   1   Cannot implicitly convert type 'System.Collections.Generic.List<FlexinergieAircraft.Client.CountryService.DTOAirport>' to 'System.Collections.Generic.List<FlexinergieAircraft.Client.DTOAirport>'  D:\Projects\FlexinergieAicraft.OldVersions\FlexinergieAircraft17\FlexinergieAircraft\FlexinergieAircraft.Client\FlexinergieAircraft.Client\Program.cs   17  45  FlexinergieAircraft.Client

Error   2   Cannot implicitly convert type 'FlexinergieAircraft.Client.CountryService.DTOCountry' to 'FlexinergieAircraft.Client.DTOCountry'    D:\Projects\FlexinergieAicraft.OldVersions\FlexinergieAircraft17\FlexinergieAircraft\FlexinergieAircraft.Client\FlexinergieAircraft.Client\Program.cs   18  34  FlexinergieAircraft.Client

Error   3   Cannot implicitly convert type 'FlexinergieAircraft.Client.AirportService.DTOAirport' to 'FlexinergieAircraft.Client.DTOAirport'    D:\Projects\FlexinergieAicraft.OldVersions\FlexinergieAircraft17\FlexinergieAircraft\FlexinergieAircraft.Client\FlexinergieAircraft.Client\Program.cs   19  34  FlexinergieAircraft.Client

如果您还需要其他东西

在这个示例中,我在客户端测试了 DTO,结果是相同的,看来服务重新键入了对象

也许它是代理,但它并没有让我在 azure 和 .NET 上工作很长时间

最佳答案

您的错误消息的路径包含“.oldversions”。

客户端和服务器端有没有可能是两个不同的版本?

我还发现您有一个可为空的 int,有时可为空的类型可能是一个问题。为了以防万一,您可以尝试不使用此字段。

关于.net - Azure 序列化中的 DTO 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5388948/

相关文章:

当应用程序发送 401 时,Azure 应用程序网关抛出 502

azure - 错误 1000 : AFx Library library exception: Type Decimal is not supported

c# - InternalClientWebSocket 实例无法用于通信,因为它已转换为 'Aborted' 状态

c# - NServiceBus异常处理和消息重试机制

azure - documentdb 中的冲突解决

c# - C# 中 Azure Webjob 中的 GraceFul ShutDown

c# - 如何解决 WMI 请求的 "Provider load failure"?

.net - 如何通过 https ://localhost:[port] 使用 ngrok

performance - vNet 内部具有 Azure SQL 连接的 Azure Web App PaaS(应用服务计划)性能对比 vNet 外部

azure - 如何使用 Visual Studio 2015 在 Azure 函数中定义连接字符串?