2016-02-17 3 views
0

Я сталкиваюсь с очень странной ситуацией. У меня есть два раздела кода, в которых два отдельных вызова для отдыха выполняются в веб-службе, а json с веб-сервера десериализуется с помощью Json.Net. Вот мои фрагменты кодаDeserialization with json.net

первый раздел:

string AuthoraizationTicket = ""; 
       using (var client = new WebClient()) 
       { 
        string url = "https://maxcvservices.dnb.com/Authentication/V2.0/"; 

        client.Headers.Add("x-dnb-user", "P200000216EA4E620E64DD980D494E7A"); 
        client.Headers.Add("x-dnb-pwd", "password#1"); 

        try 
        { 
         var response = client.DownloadString(url); 
         var DeserializedJson = JsonConvert.DeserializeObject<RootObject>(response); 
         AuthoraizationTicket = DeserializedJson.AuthenticationDetail.Token; 

        } 
        catch (Exception ex) 
        { 

        } 

выше секция выполняется правильно и Json десериализируется Правильно.

Раздел 2:

using (var wClient = new WebClient()) 
        { 
         string DUNS = txtDUNS.Value; 
         string RTNG_TRND = String.Format("https://maxcvservices.dnb.com/V5.0/organizations/{0}/products/RTNG_TRND", DUNS); 
         wClient.Headers.Add("Authorization", AuthoraizationTicket); 

         try 
         { 
          var resp = wClient.DownloadString(RTNG_TRND); 
          var RatingAndTrend = JsonConvert.DeserializeObject<RatingAndTrendDnB>(resp); 
          string creditCondition = RatingAndTrend.OrderProductResponse.OrderProductResponseDetail.Product.Organization.Assessment.FinancialConditionText.Value; 
          } 
         catch (Exception ex) 
         { 

         } 
        } 

выше раздел бросает исключение в

var RatingAndTrend = JsonConvert.DeserializeObject<RatingAndTrendDnB>(resp); 

линии говоря:

Не удалось загрузить файл или сборку «Newtonsoft.Json, Version = 8.0.0.0, Культура = нейтральная, PublicKeyToken = 30ad4fe6b2a6aeed 'или одна из ее зависимостей х годов. Определение манифеста размещенной сборки не соответствует ссылочной позиции сборки. (Исключение из HRESULT: 0x80131040 ) ":" Newtonsoft.Json, Version = 8.0.0.0, культура = нейтральной, PublicKeyToken = 30ad4fe6b2a6aeed

Вот мои классы для десериализации

public class TransactionDetail 
{ 
    public string ServiceTransactionID { get; set; } 
    public string TransactionTimestamp { get; set; } 
    public string ApplicationTransactionID { get; set; } 
} 

public class ResultMessage 
{ 
    public string ResultDescription { get; set; } 
} 

public class TransactionResult 
{ 
    public string SeverityText { get; set; } 
    public string ResultID { get; set; } 
    public string ResultText { get; set; } 
    public ResultMessage ResultMessage { get; set; } 
} 

public class AuthenticationDetail 
{ 
    public string Token { get; set; } 
} 

public class RootObject 
{ 
    public TransactionDetail TransactionDetail { get; set; } 
    public TransactionResult TransactionResult { get; set; } 
    public AuthenticationDetail AuthenticationDetail { get; set; } 
} 

public class InquiryDetail 
{ 
    public string DUNSNumber { get; set; } 
    public string CountryISOAlpha2Code { get; set; } 
} 

public class SubjectHeader 
{ 
    public string DUNSNumber { get; set; } 
} 

public class TelephoneNumber 
{ 
    public string TelecommunicationNumber { get; set; } 
    public string InternationalDialingCode { get; set; } 
} 

public class Telecommunication 
{ 
    public List<TelephoneNumber> TelephoneNumber { get; set; } 
} 

public class StreetAddressLine 
{ 
    public string LineText { get; set; } 
} 

public class PrimaryAddress 
{ 
    public List<StreetAddressLine> StreetAddressLine { get; set; } 
    public string PrimaryTownName { get; set; } 
    public string CountryISOAlpha2Code { get; set; } 
    public string TerritoryAbbreviatedName { get; set; } 
    public string PostalCode { get; set; } 
    public string TerritoryOfficialName { get; set; } 
} 

public class Location 
{ 
    public List<PrimaryAddress> PrimaryAddress { get; set; } 
} 

public class OrganizationName2 
{ 
    [JsonProperty("$")] 
    public string Value { get; set; } 
} 

public class OrganizationPrimaryName 
{ 
    public OrganizationName2 OrganizationName { get; set; } 
} 

public class OrganizationName 
{ 
    public List<OrganizationPrimaryName> OrganizationPrimaryName { get; set; } 
} 

public class FamilyTreeMemberRoleText 
{ 
    [JsonProperty("DNBCodeValue")] 
    public int DNBCodeValue { get; set; } 
    [JsonProperty("$")] 
    public string Value { get; set; } 
} 

public class FamilyTreeMemberRole 
{ 
    public FamilyTreeMemberRoleText FamilyTreeMemberRoleText { get; set; } 
} 

public class OrganizationDetail 
{ 
    public List<FamilyTreeMemberRole> FamilyTreeMemberRole { get; set; } 
} 

public class DNBStandardRating 
{ 
    [JsonProperty("DNBStandardRating")] 
    public string Rating { get; set; } 
} 

public class HistoryRatingText 
{ 
    [JsonProperty("DNBCodeValue")] 
    public int DNBCodeValue { get; set; } 
    [JsonProperty("$")] 
    public string Value { get; set; } 
} 

public class FinancialConditionText 
{ 
    [JsonProperty("DNBCodeValue")] 
    public int DNBCodeValue { get; set; } 
    [JsonProperty("$")] 
    public string Value { get; set; } 
} 

public class Assessment 
{ 
    public DNBStandardRating DNBStandardRating { get; set; } 
    public HistoryRatingText HistoryRatingText { get; set; } 
    public FinancialConditionText FinancialConditionText { get; set; } 
} 

public class Organization 
{ 
    public SubjectHeader SubjectHeader { get; set; } 
    public Telecommunication Telecommunication { get; set; } 
    public Location Location { get; set; } 
    public OrganizationName OrganizationName { get; set; } 
    public OrganizationDetail OrganizationDetail { get; set; } 
    public Assessment Assessment { get; set; } 
} 

public class ArchiveDetail 
{ 
    public int PortfolioAssetID { get; set; } 
} 

public class Product 
{ 
    public string DNBProductID { get; set; } 
    public Organization Organization { get; set; } 
    public ArchiveDetail ArchiveDetail { get; set; } 
} 

public class OrderProductResponseDetail 
{ 
    public InquiryDetail InquiryDetail { get; set; } 
    public Product Product { get; set; } 
} 

public class OrderProductResponse 
{ 
    [JsonProperty("@ServiceVersionNumber")] 
    public string ServiceVersionNumber { get; set; } 
    public TransactionDetail TransactionDetail { get; set; } 
    public TransactionResult TransactionResult { get; set; } 
    public OrderProductResponseDetail OrderProductResponseDetail { get; set; } 
} 

public class RatingAndTrendDnB 
{ 
    public OrderProductResponse OrderProductResponse { get; set; } 
} 

И мой Json по разделу 2:

{"OrderProductResponse":{"@ServiceVersionNumber":"5.0","TransactionDetail":{"ServiceTransactionID":"Id-70e1fecb52ff474c207b0000-2","TransactionTimestamp":"2014-02-15T05:54:10.834-05:00"},"TransactionResult":{"SeverityText":"Information","ResultID":"CM000","ResultText":"Success"},"OrderProductResponseDetail":{"InquiryDetail":{"DUNSNumber":"804735132","CountryISOAlpha2Code":"US"},"Product":{"DNBProductID":"RTNG_TRND","Organization":{"SubjectHeader":{"DUNSNumber":"804735132"},"Telecommunication":{"TelephoneNumber":[{"TelecommunicationNumber":"(650) 555-0000","InternationalDialingCode":"1"}]},"Location":{"PrimaryAddress":[{"StreetAddressLine":[{"LineText":"492 Koller St"}],"PrimaryTownName":"San Francisco","CountryISOAlpha2Code":"US","TerritoryAbbreviatedName":"CA","PostalCode":"94110","TerritoryOfficialName":"California"}]},"OrganizationName":{"OrganizationPrimaryName":[{"OrganizationName":{"$":"Gorman Manufacturing Company, Inc."}}]},"OrganizationDetail":{"FamilyTreeMemberRole":[{"FamilyTreeMemberRoleText":{"@DNBCodeValue":12774,"$":"Domestic Ultimate"}},{"FamilyTreeMemberRoleText":{"@DNBCodeValue":12775,"$":"Global Ultimate"}},{"FamilyTreeMemberRoleText":{"@DNBCodeValue":12773,"$":"Parent"}}]},"Assessment":{"DNBStandardRating":{"DNBStandardRating":"3A4"},"HistoryRatingText":{"@DNBCodeValue":9078,"$":"Clear"},"FinancialConditionText":{"@DNBCodeValue":415,"$":"Fair"}}},"ArchiveDetail":{"PortfolioAssetID":47651715}}}}} 

Любая помощь будет оценена

ответ

1

Похоже, что раздел 2 является либо:

  • A: Выполняется в другой сборке, чем в разделе 1, и не имеет ссылки Newtonsoft.
  • B: Ссылка на другую версию Newtonsoft.JSON.

Убедитесь, что в вашей системе пакетов NuGet (если это то, что вы используете для ссылок), у вас установлена ​​одна и та же версия Newtonsoft.JSON.

+0

Вы избили меня! Иногда проще всего просто удалить ссылку из вашего проекта (-ов) и повторно добавить его. – Ageonix

+0

благодарит за отзыв. Я ссылался на две разные версии Json.net. Один в ссылочной библиотеке классов и других в самом веб-проекте, и это бамбук мой разум –

Смежные вопросы