سؤال

I have been tasked with writing a WP8 application that needs to link to a WCF web service but I can't for the life of me figure out what I am doing wrong

I can't change the WCF service, it's not mine

The method I am struggling with takes a class as a parameter and returns a different class as the result

The interface I have been given for a test method is:

[OperationContract]
[WebInvoke(Method = "POST",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    UriTemplate = "TestLogin")]
TestResults TestLogin(TestDetails Test);

public class TestDetails
{
    public string Username { get; set; }
    public string Password { get; set; }
    public string DeviceId { get; set; }
}

public string TestResult
{
    public int TestId { get; set; }
    public List<TestOrders> Orders { get; set; }
}

I have tried RestSharp but I just get a bad request

Any advice will be much appreciated

Here's my sample code:

var client = new RestClient
{
        BaseUrl = "http://www.testing.co.uk/Services/Service.svc"
};

var dto = new TestDetails
{
    username = "abc",
    password = "123",
    DeviceId = String.Empty,
    DeviceModel = String.Empty
};

var request = new RestRequest
{
    Resource = "Testlogin",
    RequestFormat = DataFormat.Json,
    Method = Method.POST
};

request.AddParameter("TestDetails", dto, ParameterType.RequestBody);
// request.AddBody(dto);
var response = client.Post<TestResult>(request);
هل كانت مفيدة؟

المحلول

OK figured it out :-) I've switched to using HttpClient

public static void Testing()
{
    var c = new HttpClient
    {
        BaseAddress = new Uri("http://www.testing.co.uk/Services/Service.svc/")
    };
    c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var json = "{\"login\":{\"DeviceId\":\"\",\"Password\":\"123\",\"Username\":\"abc\",\"DeviceModel\":\"\"}}";

    var req = new HttpRequestMessage(HttpMethod.Post, "TestLogin")
    {
        Content = new StringContent(json, Encoding.UTF8, "application/json")
    };
    c.SendAsync(req).ContinueWith(respTask =>
    {
        var response = respTask.Result.Content.ReadAsStringAsync();
        Console.WriteLine("Response: {0}", respTask.Result);
    });
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top