如何使用JSON输入调用和检索WCF服务 [英] How to call and retrieve WCF Service with JSON input

查看:49
本文介绍了如何使用JSON输入调用和检索WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



这是WCF的服务方式,



Hi all,

This is the WCF service method,

public LoginAuthenticationContract AuthenticateUser(Stream JSONDataStream)
        {
            LoginAuthenticationContract result = new LoginAuthenticationContract();

            try { 
                StreamReader reader = new StreamReader(JSONDataStream);
                string JSONData = reader.ReadToEnd();

                JavaScriptSerializer jss = new JavaScriptSerializer();
                User userDetail = jss.Deserialize<User>(JSONData);

                if (!string.IsNullOrEmpty(userDetail.UserName)
                    && !string.IsNullOrEmpty(userDetail.PassWord)
                    && !string.IsNullOrEmpty(userDetail.UserType.ToString()))
                {
                    var userData = from U in dataContext.UserDetails 
                                   where U.UserName == userDetail.UserName && U.Password == userDetail.PassWord && U.UserType == userDetail.UserType
                                   select U;

                    if (userData.Any())
                    {
                        result.Result = "SUCCESSFULLY LOGGED IN";

                    }
                    else {
                        result.ErrorMessage = "Invalid Username/Password";
                    }
                }
                                   

            }
            catch(Exception e)
            {
                oErrorLog.WriteErrorLog(e.Message.ToString());     
            }

            return result;
        }







我必须从winform应用程序中调用此WCF方法。我不知道如何使用JSON输入调用此方法并检索JSON值。



请分享您的想法。



先谢谢。




I have to call this WCF method from winform application. I don't know how to call this method with JSON input and retrieve the JSON values.

Please share your ideas.

Thanks in advance.

推荐答案

请尝试使用Nuget Packgae Newton.Json。从您的应用程序传递参数作为Json字符串(即,从客户端应用程序序列化对象)并反序列化WCF方法中的字符串。返回对象时请使用相同的步骤。
Please try to use Nuget Packgae Newton.Json. Pass parameter as Json string from your application(ie, serialize the object from your client application) and deserialize the string in your WCF method. while returning the object please use same steps.


大家好,



我有一个解决方案,



Hi all,

I got a solution for this,

using ProjectName.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace ProjectName.AdminTool.Forms
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

	public string Service(string uri,byte[] data)
        {
            string WebServiceURL = "http://11.11.11.11/AdminToolAPI.svc/"+uri;
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(WebServiceURL);
            webrequest.Method = "POST";
            webrequest.ContentType = "application/x-www-form-urlencoded";
            webrequest.ContentLength = data.Length;
            Stream newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
            Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
            string result = loResponseStream.ReadToEnd();           
            loResponseStream.Close();
            webresponse.Close();
            return result;
        }


        private void but_Login_Click(object sender, EventArgs e)
        {
            var obj = new User();
            obj.UserName = txtuname.Text;
            obj.PassWord = txtpasswd.Text;
            ASCIIEncoding encoding = new ASCIIEncoding();
            var json = new JavaScriptSerializer().Serialize(obj);
            byte[] data = encoding.GetBytes(json);
            string result = service.Service("AuthenticateUser", data);
            JavaScriptSerializer deserializer = new JavaScriptSerializer();
            ResultContract resultCont = deserializer.Deserialize<ResultContract>(result);
            string resultMessage = resultCont.Result;
            string errorMessage = resultCont.ErrorMessage;
            if (resultMessage != null)
            {
                this.Close();
            }
            else
            {
                MessageBox.Show("Username or Password is incorrect");
                txtuname.Focus();
            }



        }
    }
}





谢谢。



Thank You.


这篇关于如何使用JSON输入调用和检索WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆