You must Sign In to post a response.
Category: WCF
#766056
To handle the WCF in Json is very simple. You have to take care the following in your application.
In the WCF interface specify the Request and Response format.
You can all the WCF service as follows as REST Call.
By Nathan
Direction is important than speed
In the WCF interface specify the Request and Response format.
[OperationContract]
[WebInvoke(UriTemplate = "/Login",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = "POST")]
Login_Response Login(Login_Request Login_RequestP);
You can all the WCF service as follows as REST Call.
HttpWebRequest TheRequestL = (HttpWebRequest)WebRequest.Create("URL");
TheRequestL.ContentType = "application/json";
TheRequestL.ContentLength = zInputJsonDataP.Length;
TheRequestL.Method = "POST";
TheRequestL.AllowAutoRedirect = false;
Stream aRequestStreamL = TheRequestL.GetRequestStream();
byte[] postBytes = System.Text.Encoding.ASCII.GetBytes(zInputDataP);
aRequestStreamL.Write(postBytes, 0, postBytes.Length);
aRequestStreamL.Close();
HttpWebResponse aResponseL = (HttpWebResponse)TheRequestL.GetResponse();
string zVersionL = aResponseL.ProtocolVersion.ToString();
StreamReader aStreamReaderL = new StreamReader(aResponseL.GetResponseStream());
StringBuilder aHttpCallXmlOutL = new StringBuilder();
string zTempL;
while ((zTempL = aStreamReaderL.ReadLine()) != null)
{
aHttpCallXmlOutL.Append(zTempL);
}
aStreamReaderL.Close();
zOutputDataP = aHttpCallXmlOutL.ToString();
By Nathan
Direction is important than speed
#766071
Hi,
1. Add some DataContract to your WCFService application:
2. Add WCF Service reference and name it as WCFService1.
3. Call these functions from your application:
PostData();
GetData();
1. Add some DataContract to your WCFService application:
using System.Runtime.Serialization;
namespace WCFService
{
[DataContract]
public class XYZContract
{
[DataMember]
public string FName { get; set; }
[DataMember]
public string LName { get; set; }
}
}
2. Add WCF Service reference and name it as WCFService1.
3. Call these functions from your application:
PostData();
GetData();
private static void PostData()
{
XYZContract UserInfo = new XYZContract
{
FName = "XYZ",
LName = "PQR"
};
DataContractJsonSerializer jSonSerializer =
new DataContractJsonSerializer(typeof(XYZContract));
MemoryStream objMemrStrm = new MemoryStream();
jSonSerializer.WriteObject(objMemrStrm, UserInfo);
string szData =
Encoding.UTF8.GetString(objMemrStrm.ToArray(), 0, (int)objMemrStrm.Length);
WebClient objWC = new WebClient();
objWC.Headers["Content-type"] = "application/json";
objWC.Encoding = Encoding.UTF8;
objWC.UploadString("http://localhost:56462/WCFService1.svc
/SaveDetails", "POST", szData); /*Assuming that WCFService contains SaveDetails method*/
}
private static void GetData()
{
WebClient objWC = new WebClient();
string szFirstName= "XYZ";
string szURL =
string.Format("http://localhost:56462/WCFService1.svc
/GetDetails/{0}", szFirstName); /*Assuming that WCFService contains GetDetails method*/
byte[] bytData = objWC.DownloadData(szURL);
Stream streamData = new MemoryStream(bytData);
DataContractJsonSerializer jSonSerializer =
new DataContractJsonSerializer(typeof(XYZContract));
XYZContract UserInfo = jSonSerializer.ReadObject(streamData) as XYZContract;
Console.WriteLine("First Name : " + UserInfo.FName);
Console.WriteLine("Last Name : " + UserInfo.LName);
}
#766082
Hi,,
Inside IService:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/InsertBook/{bookName}/{author}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
void InsertBookDetails(string bookName, string author);
}
2)Inside Service.cs
public class Service1 : IService1
{
public void InsertBookDetails(string bookName, string author)
{
using (TestBook_RestEntities obj = new TestBook_RestEntities())
{
obj.sp_InsertBookDetails(bookName, author, DateTime.Now, DateTime.Now);
}
}
}
3)Entity
[DataContract]
public class Book_Entity
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string BookName { get; set; }
[DataMember]
public string Author { get; set; }
[DataMember]
public string CreatedDate { get; set; }
[DataMember]
public string ModifiedDate { get; set; }
}
Inside IService:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/InsertBook/{bookName}/{author}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
void InsertBookDetails(string bookName, string author);
}
2)Inside Service.cs
public class Service1 : IService1
{
public void InsertBookDetails(string bookName, string author)
{
using (TestBook_RestEntities obj = new TestBook_RestEntities())
{
obj.sp_InsertBookDetails(bookName, author, DateTime.Now, DateTime.Now);
}
}
}
3)Entity
[DataContract]
public class Book_Entity
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string BookName { get; set; }
[DataMember]
public string Author { get; set; }
[DataMember]
public string CreatedDate { get; set; }
[DataMember]
public string ModifiedDate { get; set; }
}
#766718
HI,
Go through the below mentioned Links, It will help u lot
1.http://dotnetmentors.com/wcf/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract.aspx
2.http://www.codeproject.com/Articles/201901/CREATE-RESTful-WCF-Service-API-Using-POST-Step-By
3.http://www.codeproject.com/Articles/722874/Post-JSON-data-to-WCF-RESTful-Service-using-jQuery
4.http://www.topwcftutorials.net/2014/02/post-json-to-wcf-restful-service.html
Go through the below mentioned Links, It will help u lot
1.http://dotnetmentors.com/wcf/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract.aspx
2.http://www.codeproject.com/Articles/201901/CREATE-RESTful-WCF-Service-API-Using-POST-Step-By
3.http://www.codeproject.com/Articles/722874/Post-JSON-data-to-WCF-RESTful-Service-using-jQuery
4.http://www.topwcftutorials.net/2014/02/post-json-to-wcf-restful-service.html
Return to Return to Discussion Forum