Deserialize JSON object sent from Android app to WCF webservice

前端 未结 1 1139
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 03:20

I\'m trying to send a JSON object to my webservice method, the method is defined like this:

public String SendTransaction(string trans)
{
            var jso         


        
相关标签:
1条回答
  • 2020-12-02 03:35

    @Tobias, This is not an answer. But since it was a little bit long for comment, I post it here. Maybe it can help to diagnose your problem. [A full working code].

    public void TestWCFService()
    {
        //Start Server
        Task.Factory.StartNew(
            (_) =>{
                Uri baseAddress = new Uri("http://localhost:8080/Test");
                WebServiceHost host = new WebServiceHost(typeof(TestService), baseAddress);
                host.Open();
            },null,TaskCreationOptions.LongRunning).Wait();
    
    
        //Client
        var jsonString = new JavaScriptSerializer().Serialize(new { xaction = new { Imei = "121212", FileName = "Finger.NST" } });
        WebClient wc = new WebClient();
        wc.Headers.Add("Content-Type", "application/json");
        var result = wc.UploadString("http://localhost:8080/Test/Hello", jsonString);
    }
    
    [ServiceContract]
    public class TestService
    {
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        public User Hello(Transaction xaction)
        {
            return new User() { Id = 1, Name = "Joe", Xaction = xaction };
        }
    
        public class User
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public Transaction Xaction { get; set; }
        }
    
        public class Transaction
        {
            public string Imei { get; set; }
            public string FileName { get; set; }
        }
    }
    
    0 讨论(0)
提交回复
热议问题