locked
How to Consume WCF REST using web site.. RRS feed

  • Question

  • User-736091293 posted

    Hello,

    I am using WCF REST service for json responce. I have successfully run WCF service in web application but in web site I could not. Please give me web.config file settings for WCF REST service for Asp.net web site...

    And how can we consume from web browser?

    Thanks

    Sunday, March 30, 2014 10:26 AM

Answers

  • User-417640953 posted

    I can run in web application but I want to use in web site. For android application. So please give me steps for that.

    Hello,

    # If you use the .net website consume the rest service, just try below code anywhere you want to.

     WebClient proxy = new WebClient();
    
     byte[] abc = proxy.DownloadData((new Uri("http://localhost:4567/Service1.svc/GetData")));
    
     Stream strm = new MemoryStream(abc);
    
     DataContractSerializer obj = new DataContractSerializer(typeof(string));
    
     string result = obj.ReadObject(strm).ToString();
    

    # For android application consume the rest service, you can try below steps.

    http://www.codeproject.com/Articles/361107/WCF-and-Android-Part-II

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 2, 2014 9:33 PM

All replies

  • User1941103292 posted

    You would like to visit the following links for settings to be made in the web.config for the wcf service to be consumed in web application

    http://www.codeproject.com/Articles/613097/WCF-RESTful-services-and-consuming-them-in-an-HTML

    http://www.codeproject.com/Articles/642444/Creating-and-consuming-a-simple-WCF-Service-withou

    Please reply in case you have any doubts.

    Monday, March 31, 2014 2:49 AM
  • User-417640953 posted

    Hi pateltanmay,

    Thank you post the issue to asp.net forum.

    As for consume restfull service in asp.net website, please try below code. However, we must make sure our service host and accessable.

    Default.aspx.cs:

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Web;
    
    using System.Web.UI;
    
    using System.Web.UI.WebControls;
    
    using System.Runtime.Serialization;
    
    using System.Net;
    
    using System.IO; 
    
    public partial class _Default : System.Web.UI.Page
    {
    
        protected void Page_Load(object sender, EventArgs e)
    
        {
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
    
        {
    
            WebClient proxy = new WebClient();
    
            byte[] abc = proxy.DownloadData((new Uri("http://localhost:4567/Service1.svc/GetData")));
    
            Stream strm = new MemoryStream(abc);
    
            DataContractSerializer obj = new DataContractSerializer(typeof(string));
    
            string result = obj.ReadObject(strm).ToString();
    
            TextBox1.Text = result;
    
        }
    
    }

    For more information, you can refer to below.

    http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/consuming-wcf-rest-service-in-Asp-Net-web-site/

    If you want to use the web browser consume this type service, I think you can try to use the ajax to complete it like below.

    http://www.aspsnippets.com/Articles/Consuming-WCF-Rest-Service-using-jQuery-AJAX-in-ASPNet.aspx

    Hope that helps, thanks.

    Best Regards!

    Tuesday, April 1, 2014 3:44 AM
  • User-736091293 posted

    Hello,

    Thanks for reply me. I can run in web application but I want to use in web site. For android application. So please give me steps for that. How can I access it using URL like android request?

    Thanks,

    Regards

    Wednesday, April 2, 2014 2:27 PM
  • User-417640953 posted

    I can run in web application but I want to use in web site. For android application. So please give me steps for that.

    Hello,

    # If you use the .net website consume the rest service, just try below code anywhere you want to.

     WebClient proxy = new WebClient();
    
     byte[] abc = proxy.DownloadData((new Uri("http://localhost:4567/Service1.svc/GetData")));
    
     Stream strm = new MemoryStream(abc);
    
     DataContractSerializer obj = new DataContractSerializer(typeof(string));
    
     string result = obj.ReadObject(strm).ToString();
    

    # For android application consume the rest service, you can try below steps.

    http://www.codeproject.com/Articles/361107/WCF-and-Android-Part-II

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 2, 2014 9:33 PM
  • User-736091293 posted

    Hello,

    How can I control on jason responce or xml responce in wcf service. Like when I responce with both using datatable then there are lots of junk data like you can see below.. How can we control it?

    - <DoWorkResponse xmlns="http://tempuri.org/">
    - <DoWorkResult>
    - <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    - <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="ABCD" msdata:UseCurrentLocale="true">
    - <xs:complexType>
    - <xs:choice minOccurs="0" maxOccurs="unbounded">
    - <xs:element name="ABCD">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="id" type="xs:string" minOccurs="0" />
      <xs:element name="NAME" type="xs:string" minOccurs="0" />
      <xs:element name="PHONE" type="xs:string" minOccurs="0" />
      <xs:element name="TEST" type="xs:string" minOccurs="0" />
      <xs:element name="T" type="xs:string" minOccurs="0" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:choice>
      </xs:complexType>
      </xs:element>
      </xs:schema>
    - <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    - <NewDataSet xmlns="">
    - <ABCD diffgr:id="ABCD1" msdata:rowOrder="0">
      <id>kj</id>
      <NAME>lkj</NAME>
      <PHONE>lk</PHONE>
      <TEST>jl</TEST>
      <T>kj</T>
      </ABCD>
    - <ABCD diffgr:id="ABCD2" msdata:rowOrder="1">
      <id>lk</id>
      <NAME>lkj</NAME>
      <PHONE>l</PHONE>
      <TEST>jl</TEST>
      <T>kj</T>
      </ABCD>
    - <ABCD diffgr:id="ABCD3" msdata:rowOrder="2">
      <id>lk</id>
      <NAME>n</NAME>
      <PHONE>klj</PHONE>
      <TEST>l</TEST>
      <T>kj</T>
      </ABCD>
    - <ABCD diffgr:id="ABCD4" msdata:rowOrder="3">
      <id>lk</id>
      <NAME>lkj</NAME>
      <PHONE>l</PHONE>
      <TEST>jl</TEST>
      <T>j</T>
      </ABCD>
    - <ABCD diffgr:id="ABCD5" msdata:rowOrder="4">
      <id>lk</id>
      </ABCD>
      </NewDataSet>
      </diffgr:diffgram>
      </DoWorkResult>
      </DoWorkResponse>

    Tuesday, April 8, 2014 1:57 PM
  • User-417640953 posted

    pateltanmay29

    How can I control on jason responce or xml responce in wcf service. Like when I responce with both using datatable then there are lots of junk data like you can see below.. How can we control it?

    Hi,

    In the WCF rest service, we can set the response format like below.

    Json

     [OperationContract]
     [WebInvoke(UriTemplate = "Service/YourMethod", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat=WebMessageFormat.Json)]
     public string YourMethod(string param)
     {
    
     }

    XML:

     [OperationContract]
     [WebInvoke(UriTemplate = "Service/YourMethod", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat=WebMessageFormat.Xml)]
     public string YourMethod(string param)
     {
    
     }

    Thanks.

    Best Regards!

    Tuesday, April 8, 2014 9:25 PM