【问题标题】:WCF Services Rest CallWCF 服务休息调用
【发布时间】:2025-12-28 06:20:10
【问题描述】:

我有一个可在 SOAP 和 REST 中调用的 WCF 服务。 如果 make SOAP 调用可以正常工作,但是使用 REST 我会遇到问题。

总之,该方法返回 POCO ENTITY,但是当我调用时,我得到一个连接错误取消。 如果我调用另一个返回布尔值或字符串(即本机类型)的方法,则不会发生同样的事情。

在我看来,错误似乎不是我正在使用的 POCO 实体(这就是我正在使用的 Devart ,所以很确定它是)。 所以我做了什么,我创建了它的自定义地图(具有相同的属性),并使用 AutoMapper 进行映射。

问题依然存在:-(

这是.svc.cs

 public List<GetLuoghiSimiliByAddressesDTO> GetLuoghiSimiliByAddress(string toponimo, string nomestrada, string civico, int idcomune)
    {
        Agile.SL.Services.IAnagraficaService srv = new Agile.SL.Services.Impl.AnagraficaService();
        List<GetLuoghiSimiliByAddressesDTO> result = new List<GetLuoghiSimiliByAddressesDTO>();
        Mapper.CreateMap<DTOGetLuoghiSimiliByAddress, GetLuoghiSimiliByAddressesDTO>();
        foreach (var dto in srv.GetLuoghiSimiliByAddress(toponimo, nomestrada, civico, idcomune).ToList<DTOGetLuoghiSimiliByAddress>())
        {
            GetLuoghiSimiliByAddressesDTO newdto = Mapper.Map<DTOGetLuoghiSimiliByAddress, GetLuoghiSimiliByAddressesDTO>(dto);
            result.Add(newdto);
        }

        return result;
    }

结果正确包含我的对象列表。

这是服务器

[OperationContract]
    [WebGet(UriTemplate = "GetLuoghiSimiliByAddress?Toponimo={toponimo}&Nome_Strada={nomestrada}&Civico={civico}&Id_Comune={idcomune}",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    List<GetLuoghiSimiliByAddressesDTO> GetLuoghiSimiliByAddress(string toponimo, string nomestrada, string civico, int idcomune);

正确使用此方法和操作合同

    public bool IsUserAlreadyRegistered(string email)
    {
        Agile.SL.Services.IAnagraficaService srv = new Agile.SL.Services.Impl.AnagraficaService();
        return srv.CheckEmailExistance(email);
    }            

[OperationContract]
[WebGet(UriTemplate = "IsUserAlreadyRegistered?Email={email}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
bool IsUserAlreadyRegistered(string email);

这是GetLuoghiSimiliByAddressesDTO

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Merqurio.Agile.DL.Model.Entities
{
    [DataContract(IsReference = true)]
    [Serializable]
    public class GetLuoghiSimiliByAddressesDTO
    {

        private int _Id_Luogo;
        private string _Toponimo;
        private string _Nome_Strada;
        private string _Civico;


        public GetLuoghiSimiliByAddressesDTO()
        {

        }


    /// <summary>
    /// There are no comments for Id_Luogo in the schema.
    /// </summary>
        [DataMember(Order=1)]
        public int Id_Luogo
        {
            get
            {
                return this._Id_Luogo;
            }
            set
            {
                if (this._Id_Luogo != value)
                {
                    this._Id_Luogo = value;
                }
            }
        }


    /// <summary>
    /// There are no comments for Toponimo in the schema.
    /// </summary>
        [DataMember(Order=2)]
        public string Toponimo
        {
            get
            {
                return this._Toponimo;
            }
            set
            {
                if (this._Toponimo != value)
                {
                    this._Toponimo = value;
                }
            }
        }


    /// <summary>
    /// There are no comments for Nome_Strada in the schema.
    /// </summary>
        [DataMember(Order=3)]
        public string Nome_Strada
        {
            get
            {
                return this._Nome_Strada;
            }
            set
            {
                if (this._Nome_Strada != value)
                {

                    this._Nome_Strada = value;

                }
            }
        }


    /// <summary>
    /// There are no comments for Civico in the schema.
    /// </summary>
        [DataMember(Order=4)]
        public string Civico
        {
            get
            {
                return this._Civico;
            }
            set
            {
                if (this._Civico != value)
                {

                    this._Civico = value;

                }
            }
        }


    }

}

请帮帮我!

【问题讨论】:

  • 你能显示 GetLuoghiSimiliByAddressesDTO 代码吗?
  • 是的,我做到了!你能帮我吗?

标签: wcf web-services rest soap automapper


【解决方案1】:

您不能在 DataContract 上使用“IsReference = true”。

来自 MSDN:

使用 IsReference 属性指示 DataContractSerializer 插入保留对象引用信息的 XML 构造。

您返回的是 JSON,而不是 XML...

反正你这里不需要,我看不到任何循环依赖。

【讨论】:

    【解决方案2】:

    进行 REST 调用时返回的错误代码是什么。还可以尝试在您的服务上启用跟踪,以查看请求通过 REST 失败的原因。要启用跟踪,请遵循此link

    还可以尝试使用 fiddler 来检查您对服务的请求。

    【讨论】: