我有两个Wcf restful服务1.建议和2. 搜索。当我使用搜索服务时,建议服务正常但我得到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提供here
创建了服务代理以及用于返回数据的XML here
我确信呼叫点击服务,服务正确解析XML ,因为我已通过服务调试,但我不这样做了解问题所在..有任何帮助吗?