Question

I've been trying to make a WCF web service that enters a certain set of data into a database and it can be used in Windows Phone 7. In the configuration file there are two endpoints: json and soap. Here's the code for both of them:

<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>

Now, I deployed the service to a server, downloaded its metadata with slsvcutil.exe and stored both the code file and the ServiceReferenceConfig file in the project. Now, the slsvcutil only generates the configuration for the soap endpoint.

Now, I try to use the service using the RestSharp library, by using the following code:

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);
    }
}

If I try to use the URL given in the sample code, I get an "endpoint not found" error, which is to be expected. However, if I try to use the SOAP endpoint via the URL http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/soap/SaveNewSightingRest, I just get a "Bad Request" status code.

Why does this happen? Is there any way I can make it work? If it can be any helpful, everytime I click on the link I provided for the SOAP service I just get a blank screen.

Thanks for the help!

Était-ce utile?

La solution

So I don't have to post in comments:

What does your body look like in fiddler, with RestSharp you can create a poco and simply, RestRequest.AddBody(MyPoco);

I think the url:

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

should be:

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

and then:

RestRequest.Resource = "json/SaveNewSightingRest";

update: I copied your code, chanding the url/resource and in fiddler the body is: <String /> To clarify, this is the body being sent, so you'd expect to see the json you're trying to send.

with error:

The server encountered an error processing the request. The exception message is 'Unable to deserialize XML body with root name 'String' and root namespace '' (for operation 'SaveNewSightingRest' and contract ('ISightingServiceRest', 'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top