0

Hi I am new to wcf service.I am creating one wcf sample programe.but response not showing in browser.I want to response show in browser.

below is my sample code:

In Interface:

 public interface ICitiesService
    {
        [OperationContract]

       [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "getdata", BodyStyle = WebMessageBodyStyle.Bare)]
       // [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "getdata")]
        string getdata();
    }

in class:

public class CitiesService : ICitiesService
    {

        public string getdata()
        {
            try
            {


                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con2"].ConnectionString);
                SqlCommand comm = new SqlCommand("select CircleID, CircleName from Circle", con);
                con.Open();
                comm.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter(comm);
                // DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);

                JavaScriptSerializer JSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                List<Dictionary<string, object>> DtRows = new List<Dictionary<string, object>>();
                Dictionary<string, object> newrow = null;

                //Code to loop each row in the datatable and add it to the dictionary object
                foreach (DataRow drow in dt.Rows)
                {
                    newrow = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        newrow.Add(col.ColumnName.Trim(), drow[col]);
                    }
                    DtRows.Add(newrow);
                }
                con.Close();


                return JSSerializer.Serialize(DtRows);
            }
            catch (Exception ex)
            {

                List<String> Parameters = new List<String>();
                Parameters.Add("getRechargeCircleList");

                //   SendErrorMail(ex.ToString(), "getRechargeCircleList", Parameters);
                // Context.Response.ContentType = "application/json; charset=utf-8";
                // Context.Response.Write(@" {""Status"":""false"",""Message"" : ""Oops! Something went Wrong""}");
                // return;
            }
            return "false";

        }
    }

web.config file:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>

  </connectionStrings>
  <appSettings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <!--
        The <authentication> section enables configuration 
        of the security authentication mode used by 
        ASP.NET to identify an incoming user. 
    -->
    <authentication mode="Windows"/>
    <!--
        The <customErrors> section enables configuration 
        of what to do if/when an unhandled error occurs 
        during the execution of a request. Specifically, 
        it enables developers to configure html error pages 
        to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
         <error statusCode="403" redirect="NoAccess.htm" />
         <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
    -->
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
  <!-- 
      The system.webServer section is required for running ASP.NET AJAX under Internet
      Information Services 7.0.  It is not necessary for previous version of IIS.
  -->
  <system.webServer>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="web"  helpEnabled="true" automaticFormatSelectionEnabled="true"  />
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service behaviorConfiguration="PavanWcfservice.Service1Behavior" name="PavanWcfservice.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="PavanWcfservice.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="" binding="netTcpBinding" contract="PavanWcfservice.IService1"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service behaviorConfiguration="PavanWcfservice.CitiesBehavior" name="PavanWcfservice.Cities">
        <endpoint address="" binding="basicHttpBinding" contract="PavanWcfservice.ICities">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service behaviorConfiguration="PavanWcfservice.CitiesServiceBehavior" name="PavanWcfservice.CitiesService">
        <endpoint address="" binding="basicHttpBinding" contract="PavanWcfservice.ICitiesService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

    </services>
    <behaviors>












      <serviceBehaviors>
        <behavior name="PavanWcfservice.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="PavanWcfservice.CitiesBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="PavanWcfservice.CitiesServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Is there any changes in web.config file.I don't know,please help me.. I want to the result is show in browser.

whenever I run the service below is my url " http://localhost:/CitiesService.svc/getdata"

the blank page will come.what should i do ....please help me

Anil Kumar
  • 303
  • 1
  • 6
  • 23
  • I am trying this for last 4 days still I am not getting answer – Anil Kumar Sep 05 '15 at 06:04
  • its working fine in wcf test client window.response came – Anil Kumar Sep 05 '15 at 06:13
  • What you're looking for is the [WCF Web HTTP Programming Model](https://msdn.microsoft.com/en-us/library/bb412169(v=vs.110).aspx). Have an additional look at this answer: http://stackoverflow.com/questions/186631/rest-soap-endpoints-for-a-wcf-service – Jan Köhler Sep 06 '15 at 09:34

0 Answers0