Служба WCF SOAP выдает ошибку Произошла ошибка при получении ответа HTTP на http://localhost/Service1.svc - PullRequest
0 голосов
/ 13 июля 2020

когда я создаю клиента и вызываю getNews, он переходит в службу, но после возврата он дает мне эту ошибку

System.ServiceModel.CommunicationException: 'Произошла ошибка при получении ответа HTTP на http://localhost/Service1.svc. Это может быть связано с тем, что привязка конечной точки службы не использует протокол HTTP. Это также может быть связано с прерыванием контекста HTTP-запроса сервером (возможно, из-за завершения работы службы). См. Журналы сервера для более подробной информации. '

Интерфейс:

    [ServiceContract , XmlSerializerFormat]
public interface IService1
{
    [OperationContract]

    //  string getNews(getNewsRequest request);
    // GetNewsOutputStructure getNews (getNewsRequest request);
    // getNewsResponseStructure getNews(getNewsRequest request);
    getNewsResponse getNews();

    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

реализация

       public getNewsResponse getNews()
    {
        getNewsResponse response = null;

        List<NewsListStructure> newsList = new List<NewsListStructure>();
        NewsStructure news = null;
        for (int i = 0; i < 2; i++)
        {
            news = new NewsStructure();
            news.NewsText = "Test";
            news.NewsId = "22";
            news.NewsDate = DateTime.Now;
            MediaExtentionsArrayStructure[] mediaExtentionsArrayStructures = null;
            news.MediaExtentionsArrayObject = mediaExtentionsArrayStructures;   //nnList[i].NewsDate;
            news.NewsClassificationObject = new NewsClassificationStructure { NewsClassificationId = 1.ToString(), NewsClassificationDescription = "g" };
            //  news.MediaArrayObject = nnList[i].NewsDate;
            NewsListStructure newsStructure = new NewsListStructure();
            newsStructure.NewsObject = news;
            newsList.Add(newsStructure);

        }
        //   return newsList;

        GetNewsOutputStructure getNewsOutputStructure = new GetNewsOutputStructure();
        getNewsOutputStructure.NewsListObject = newsList.ToArray();
        getNewsOutputStructure.ResponseStatus = ResponseStatusType.Success;
        getNewsOutputStructure.DataSchemaVersion = 1;
        getNewsOutputStructure.FaildReasonListObject = new FaildReasonListStructure[1] ;
        var fdf = new ErrorStructure() { Item = "hh" , ItemElementName = ItemChoiceType.ErrorMessage};
        getNewsOutputStructure.FaildReasonListObject[0] = new FaildReasonListStructure();
        getNewsOutputStructure.FaildReasonListObject[0].Item = fdf;
        // return  getNewsOutputStructure;

        getNewsResponseStructure getNewsResponseStructure = new getNewsResponseStructure();
        getNewsResponseStructure.Item = getNewsOutputStructure;

     // return getNewsResponseStructure;
     //   response = new getNewsResponse(getNewsResponseStructure);
        response = new getNewsResponse();
        response.getNewsResponseObject = getNewsResponseStructure;
       response.getNewsResponseObject.Item = newsList;

     return response;
    }

после ответа на возврат я получил сообщение об ошибке, и это объект Я верну

    [DataContract]
public class getNewsResponse
{

    //  [System.ServiceModel.MessageBodyMemberAttribute()]
    [XmlElement]
    [DataMember]
    public getNewsResponseStructure getNewsResponseObject;

    public getNewsResponse()
    {
    }

    public getNewsResponse(getNewsResponseStructure getNewsResponseObject)
    {
        this.getNewsResponseObject = getNewsResponseObject;
    }
}

[DataContract]
public class getNewsResponseStructure
{

    // [DataMember]
    private object itemField;

    /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute("getNewsResponseDetailObject", typeof(GetNewsOutputStructure))]
    //  [System.Xml.Serialization.XmlElementAttribute("ServiceError", typeof(CommonErrorStructure))]
    [XmlElement]


    [DataMember]
    public object Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
}

[DataContract]
public partial class GetNewsOutputStructure
{

    private ResponseStatusType responseStatusField;

    private NewsListStructure[] newsListObjectField;

    private FaildReasonListStructure[] faildReasonListObjectField;

    private decimal dataSchemaVersionField;


   
   // [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    [XmlElement]


    /// <remarks/>
    [DataMember]
    public ResponseStatusType ResponseStatus
    {
        get
        {
            return this.responseStatusField;
        }
        set
        {
            this.responseStatusField = value;
        }
    }

    [XmlElement]


    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute("NewsListObject", Order = 1)]
    
    [DataMember]

    public NewsListStructure[] NewsListObject
    {
        get
        {
            return this.newsListObjectField;
        }
        set
        {
            this.newsListObjectField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute("FaildReasonListObject", Order = 2)]
    [DataMember]
    [XmlElement]


    public FaildReasonListStructure[] FaildReasonListObject
    {
        get
        {
            return this.faildReasonListObjectField;
        }
        set
        {
            this.faildReasonListObjectField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute(Order = 3)]
    [DataMember]
    [XmlElement]

    public decimal DataSchemaVersion
    {
        get
        {
            return this.dataSchemaVersionField;
        }
        set
        {
            this.dataSchemaVersionField = value;
        }
    }
}


[DataContract]
[Flags]
public enum ResponseStatusType
{
   // [System.Runtime.Serialization.EnumMemberAttribute()]
   [EnumMember]

    /// <remarks/>
    Success,
    //  [System.Runtime.Serialization.EnumMemberAttribute()]
    [EnumMember]

    /// <remarks/>
    Fail,
}

[DataContract]

public partial class NewsListStructure
{

    private NewsStructure newsObjectField;

    [DataMember]
    /// <remarks/>
 //   [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    [XmlElement]
    public NewsStructure NewsObject
    {
        get
        {
            return this.newsObjectField;
        }
        set
        {
            this.newsObjectField = value;
        }
    }
}

[DataContract]
public partial class FaildReasonListStructure
{

    private ErrorStructure itemField;

    [DataMember]
    /// <remarks/>
 //   [System.Xml.Serialization.XmlElementAttribute("ErrorObject", Order = 0)]
    [XmlElement]
    public ErrorStructure Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
}

[DataContract]
public partial class ErrorStructure
{

    private string itemField;

    private ItemChoiceType itemElementNameField;

    /// <remarks/>
 //   [System.Xml.Serialization.XmlElementAttribute("ErrorCode", typeof(string), Order = 0)]
  //  [System.Xml.Serialization.XmlElementAttribute("ErrorMessage", typeof(string), Order = 0)]
  //  [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
  [DataMember]
    [XmlElement]
    public string Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
 //   [System.Xml.Serialization.XmlIgnoreAttribute()]
    [XmlElement]
    public ItemChoiceType ItemElementName
    {
        get
        {
            return this.itemElementNameField;
        }
        set
        {
            this.itemElementNameField = value;
        }
    }
}

[DataContract]
[Flags]
public enum ItemChoiceType
{
   // [System.Runtime.Serialization.EnumMemberAttribute()]
   [EnumMember]
    /// <remarks/>
    ErrorCode,
  //  [System.Runtime.Serialization.EnumMemberAttribute()]
    [EnumMember]

    /// <remarks/>
    ErrorMessage,
}

[DataContract]
public partial class NewsStructure
{

    private string newsIdField;

    private NewsClassificationStructure newsClassificationObjectField;

    private string newsTextField;

    private MediaArrayStructure[] mediaArrayObjectField;

    private MediaExtentionsArrayStructure[] mediaExtentionsArrayObjectField;

    private System.DateTime newsDateField;

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
  [DataMember]
    [XmlElement]
    public string NewsId
    {
        get
        {
            return this.newsIdField;
        }
        set
        {
            this.newsIdField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
  [DataMember]
    [XmlElement]
    public NewsClassificationStructure NewsClassificationObject
    {
        get
        {
            return this.newsClassificationObjectField;
        }
        set
        {
            this.newsClassificationObjectField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute(Order = 2)]
  [DataMember]
    [XmlElement]
    public string NewsText
    {
        get
        {
            return this.newsTextField;
        }
        set
        {
            this.newsTextField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute("MediaArrayObject", Order = 3)]
  [DataMember]
    [XmlElement]
    public MediaArrayStructure[] MediaArrayObject
    {
        get
        {
            return this.mediaArrayObjectField;
        }
        set
        {
            this.mediaArrayObjectField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute("MediaExtentionsArrayObject", Order = 4)]
  [DataMember]
    [XmlElement]
    public MediaExtentionsArrayStructure[] MediaExtentionsArrayObject
    {
        get
        {
            return this.mediaExtentionsArrayObjectField;
        }
        set
        {
            this.mediaExtentionsArrayObjectField = value;
        }
    }

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute(DataType = "date", Order = 5)]
  [DataMember]
    [XmlElement]
    public System.DateTime NewsDate
    {
        get
        {
            return this.newsDateField;
        }
        set
        {
            this.newsDateField = value;
        }
    }
}

[DataContract]
public partial class MediaExtentionsArrayStructure
{

    private string itemField;

    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute("MediaExtentions", Order = 0)]
  [DataMember]
    [XmlElement]
    public string Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
}

[DataContract]
public partial class MediaArrayStructure
{

    private byte[] itemField;

    [DataMember]
    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute("Media", DataType = "base64Binary", Order = 0)]
    [XmlElement]
    public byte[] Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
}

[DataContract]
public partial class NewsClassificationStructure
{

    private string newsClassificationIdField;

    private string newsClassificationDescriptionField;

    [DataMember]
    /// <remarks/>
  //  [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    [XmlElement]
    public string NewsClassificationId
    {
        get
        {
            return this.newsClassificationIdField;
        }
        set
        {
            this.newsClassificationIdField = value;
        }
    }

    [DataMember]
    /// <remarks/>
 //   [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
    [XmlElement]
    public string NewsClassificationDescription
    {
        get
        {
            return this.newsClassificationDescriptionField;
        }
        set
        {
            this.newsClassificationDescriptionField = value;
        }
    }


}


//[System.Diagnostics.DebuggerStepThroughAttribute()]
//[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
//[System.Xml.Serialization.XmlSchemaProviderAttribute("ExportSchema")]
//[System.Xml.Serialization.XmlRootAttribute]
[DataContract]

public partial class CommonErrorStructure : object, System.Xml.Serialization.IXmlSerializable
{

    private System.Xml.XmlNode[] nodesField;

    private static System.Xml.XmlQualifiedName typeName = new System.Xml.XmlQualifiedName();

    [DataMember]
    [XmlElement]
    public System.Xml.XmlNode[] Nodes
    {
        get
        {
            return this.nodesField;
        }
        set
        {
            this.nodesField = value;
        }
    }



    public void ReadXml(System.Xml.XmlReader reader)
    {
        this.nodesField = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
    }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public static System.Xml.XmlQualifiedName ExportSchema(System.Xml.Schema.XmlSchemaSet schemas)
    {
        System.Runtime.Serialization.XmlSerializableServices.AddDefaultSchema(schemas, typeName);
        return typeName;
    }
}

код клиента

           ServiceReference2.Service1Client client = new ServiceReference2.Service1Client();


        ServiceReference2.getNewsRequest k = new ServiceReference2.getNewsRequest();

        k.getNewsRequestObject = new ServiceReference2.GetNewsRequestStructure();
        k.getNewsRequestObject.LastReceivedNewsItemId = "10000";
        k.getNewsRequestObject.NumberOfRequestedNews = "5";
        k.getNewsRequestObject.ServiceToken = "";
        k.getNewsRequestObject.SPAGovernmentSectorKey = "";
        k.getNewsRequestObject.SubServiceObject = new ServiceReference2.SubServicesStructure { SubServiceId = "1", SubServiceDescription = "desc" };
        k.getNewsRequestObject.TypeOfData = ServiceReference2.TypeOfDataType.All;
        k.yefiSOAPHeaderElement = new ServiceReference2.YefiHeader();
          k.yefiSOAPHeaderElement.ServiceID = "1";
        k.yefiSOAPHeaderElement.SourceID = "2";
        k.yefiSOAPHeaderElement.SourceName = "genral";

        var t = client.getNews( k);
        // Always close the client.
        client.Close();

Ответы [ 2 ]

0 голосов
/ 14 июля 2020

проблема была в моем клиенте, я попытался отправить объект другого типа, который я передал в службу

это неверно

response.getNewsResponseObject.Item = newsList;

это правда

  response.getNewsResponseObject.Item = getNewsOutputStructure;

так как в сервисе я сказал ему, я пришлю вам

GetNewsOutputStructure object not List<NewsListObject>

в сервисе

      [System.Xml.Serialization.XmlElementAttribute("getNewsResponseDetailObject", typeof(GetNewsOutputStructure), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
0 голосов
/ 14 июля 2020

Это потому, что вы передаете List в response.getNewsResponseObject.Item. Согласно определению вашего класса, response.getNewsResponseObject.Item может принимать только тип объекта, а не тип List .

введите описание изображения здесь

Есть два решения. Первый - изменить класс getNewsResponseStructure, как показано ниже:

 public class getNewsResponseStructure
    {

        // [DataMember]
        private List<Object> itemField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("getNewsResponseDetailObject", typeof(GetNewsOutputStructure))]
        //  [System.Xml.Serialization.XmlElementAttribute("ServiceError", typeof(CommonErrorStructure))]
        [XmlElement]


        [DataMember]
        public List<Object> Item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }
    }

Второй - создать NewsListStructure и передать его в response.getNewsResponseObject.Item:

 NewsListStructure news = new NewsListStructure();
 response.getNewsResponseObject.Item = news;
...