1

我有两个 Wcf 宁静服务 1. 建议和 2.搜索。建议服务工作正常,但使用Search 服务时出现 400 错误搜索服务被模拟为从 xml 返回。搜索服务解析 xml 并返回对象。下面是代码。

界面:

public interface IAirSearchResultsnew
    {
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetAirsearchResults?" +
            "FT={FT}&SL={SL}&EL={EL}&SD={SD}&ED={ED}&FD={FD}&AD={AD}&CD={CD}&IN={IN}&" +
            "CL={CL}&DT={DT}&RT={RT}")]
        [OperationContract]
        airsearchresult GetAirSearchResult(string FT, string SL, string EL, string SD, string ED, string FD, string AD,
            string CD, string IN, string CL, string DT, string RT);
    }

服务:

namespace Rickshaw.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AirSearchResultsnew" in code, svc and config file together.
    public class AirSearchResultsnew : IAirSearchResultsnew
    {

        public airsearchresult GetAirSearchResult(string flightType, string from, string to, string start, string end, string flexibleDates, string adults, string children, string infants, string classPreference, string departTimePreference, string returnTimePreference)
        {
            #region Parameters read and convert to Proper form
            var startDate = start.Split('/');
            var endDate = end.Split('/');
            var SD = new DateTime(int.Parse(startDate[2]), int.Parse(startDate[0]), int.Parse(startDate[1]));
            var ED = new DateTime(int.Parse(endDate[2]), int.Parse(endDate[0]), int.Parse(endDate[1]));
            var FD = bool.Parse(flexibleDates);
            var AD = int.Parse(adults);
            int CD = 0;
            CD = int.Parse(children == "select" ? "0" : children);
            int IN = 0;
            IN = int.Parse(infants == "select" ? "0" : infants);
            var CL = string.Empty;
            CL = classPreference == "undefined" ? "NA" : classPreference;
            var DT = string.Empty;
            DT = departTimePreference == "undefined" ? "NA" : departTimePreference;
            var RT = string.Empty;
            RT = returnTimePreference == "undefined" ? "NA" : returnTimePreference;
            #endregion

            #region Mocked Service
            var reader = XmlReader.Create(@"C:\Users\Yash\Documents\Visual Studio 2010\Projects\WcfService1\Samples\sample2.xml");

            XmlSerializer serializer = new XmlSerializer(typeof(airsearchresult));

            var airsearchresults = serializer.Deserialize(reader) as airsearchresult;


            return airsearchresults;

            #endregion
        }
    }
}

配置:

<configuration>
  <system.web>
    <trace enabled="true" requestLimit="100" />
    <customErrors defaultRedirect="~/Default.aspx" />
    <authorization>
      <allow roles="Administrators" />
    </authorization>
    <roleManager enabled="true" />
    <authentication mode="Forms" />
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Suggest">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="999999999" />
        </behavior>
        <behavior name="AirSearch">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="999999999"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="Rickshaw.Services.Suggest">
          <webHttp/>
        </behavior>
        <behavior name="Rickshaw.Services.AirSearchResultsnew">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

    </behaviors>

    <services>
      <service name="Rickshaw.Services.Suggest" behaviorConfiguration="Suggest">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="Rickshaw.Services.Suggest" contract="Rickshaw.Services.ISuggest" />
      </service>
      <service name="Rickshaw.Services.AirSearchResultsnew" behaviorConfiguration ="AirSearch">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="Rickshaw.Services.AirSearchResultsnew" contract ="Rickshaw.Services.IAirSearchResultsnew" />
      </service>
    </services>

  </system.serviceModel>
</configuration>

和Javascript代码:

var query = "http://localhost:4696/Services/AirSearchResultsnew.svc/GetAirSearchResults?" +
      "FT=" + FT + "&SL=" + SL + "&EL=" + EL + "&SD=" + SD + "&ED=" + ED + "&FD=" + FD + "&AD=" + AD + "&CD=" + CD + "&IN=" + IN + "&" +
            "CL=" + CL + "&DT=" + DT + "&RT=" + RT + "";
var result = getAirSearchResults(query);

function getAirSearchResults(query) {
    var module = {
        data: [null]
    };
    $.ajax({
        url: query,
        success: function (e) {
            console.log(e);
            module.data = e;
        },
        async: false,
        error: function (XHR, textStatus, errorThrown) {
            alert(textStatus + ":" + errorThrown);
        }
    });
    console.log(module.data);
    return module.data;
};

重要的是我从这里提供的 xsd 创建了服务代理

以及此处提供的用于返回数据的 XML

我确信调用会命中服务并且服务会正确解析 XML,因为我已经通过服务对其进行了调试,但我不明白问题出在哪里。有什么帮助吗?

4

0 回答 0