我一直在尝试制作一个 WCF Web 服务,它将一组特定的数据输入数据库,并且可以在 Windows Phone 7 中使用。在配置文件中有两个端点:json 和 soap。这是他们两个的代码:
<service name="LiveAndesWCF.SightingServiceRest" behaviorConfiguration="MetadataBehavior">
<endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.ISightingServiceRest"/>
<endpoint address="json" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="LiveAndesWCF.ISightingServiceRest"/>
</service>
...
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00">
<readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00">
<readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
现在,我将服务部署到服务器,使用 slsvcutil.exe 下载其元数据并将代码文件和 ServiceReferenceConfig 文件存储在项目中。现在,slsvcutil 只为soap 端点生成配置。
现在,我尝试通过使用以下代码使用RestSharp库来使用该服务:
public static void sendSighting()
{
string URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/json/SaveNewSightingRest";
var request = new RestRequest(Method.POST);
var client = new RestClient(URL);
request.AddHeader("Accept", "application/json");
request.AddHeader("Host", "liveandesor.web711.discountasp.net");
request.AddHeader("Content-type", "application/json");
JObject json = new JObject();
json.Add("UserId", "letroll");
json.Add("Location", "TEST");
json.Add("Longitude", 50);
json.Add("Latitude", 50);
json.Add("Comment", "TEST");
json.Add("SpeciesId", 438);
json.Add("_Date", new DateTime(2011, 8, 12));
json.Add("DateString", (new DateTime(2011, 8, 12)).ToString());
json.Add("SightingTypeId", 1);
json.Add("Quantity", 1);
json.Add("AgeId", 1);
json.Add("SexId", 1);
json.Add("PhotoURL", new JArray(new List<String>()));
json.Add("Request", false);
json.Add("Visibility", false);
json.Add("SightingStateId", 1);
json.Add("Created_at", new DateTime());
json.Add("Updated_at", new DateTime());
String jason = json.ToString();
try
{
//request.AddObject(json);
request.AddBody(jason);
RestResponse resource;
client.ExecuteAsync(request, (response) =>
{
resource = response;
string content = resource.Content;
string status_code = resource.StatusCode.ToString();
string response_status = resource.ResponseStatus.ToString();
});
}
catch (Exception e)
{
string error = "Error: " + e.ToString() + "\n. Stack Trace: " + e.StackTrace;
Console.WriteLine(error);
}
}
如果我尝试使用示例代码中给出的 URL,我会收到“未找到端点”错误,这是意料之中的。但是,如果我尝试通过 URL http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/soap/SaveNewSightingRest使用 SOAP 端点,我只会收到“错误请求”状态代码。
为什么会这样?有什么办法可以让它工作吗?如果它有帮助的话,每次我点击我为 SOAP 服务提供的链接时,我都会得到一个空白屏幕。
谢谢您的帮助!