2

我一直在尝试制作一个 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 服务提供的链接时,我都会得到一个空白屏幕。

谢谢您的帮助!

4

1 回答 1

2

所以我不必在评论中发帖:

你的身体在 fiddler 中是什么样子的,使用 RestSharp 你可以简单地创建一个 poco,RestRequest.AddBody(MyPoco);

我认为网址:

 string URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/json/SaveNewSightingRest";

应该:

string URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc";

进而:

RestRequest.Resource = "json/SaveNewSightingRest";

更新:我复制了您的代码,更改了网址/资源,在提琴手中,正文是:<String /> 为了澄清,这是正在发送的正文,因此您希望看到您尝试发送的 json。

有错误:

服务器在处理请求时遇到错误。异常消息是“无法使用 DataContractSerializer 反序列化具有根名称“String”和根命名空间“”的 XML 正文(用于操作“SaveNewSightingRest”和合同(“ISightingServiceRest”、“http://tempuri.org/”))。确保将 XML 对应的类型添加到服务的已知类型集合中。

于 2011-10-05T00:42:58.263 回答