Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I New In WCF So Anybody Help Me

My Task is To Create WCF Service For Android App

Get Data From Android App Anmd Save It Into SQL server Using WCF service

My code IS Iservice.cs

[OperationContract]
        [WebGet(UriTemplate = "/GetBarCodeData/{barcode}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        [return: MessageParameter(Name = "barcode")]
        string GetBarCodeData(string barcode);


Service1.svc.cs

public string  GetBarCodeData(string barcode)
        {
       
            SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand("Insert Into Emp(Ename)values('" + barcode + "')");
            SqlDataAdapter sdap = new SqlDataAdapter();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.ExecuteNonQuery();
            // flag = true;
            //dataset ds = new dataset();
            //sqldataadapter sqldataadapter = new sqldataadapter(sqlcommand);
            //sqldataadapter.fill(ds);
            sqlConnection.Close();
            return barcode;
}


Web Config
XML
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <connectionStrings>
    <add name ="ConnectionString" providerName ="System.Data.SqlClient" ;persist security info=True;multipleactiveresultsets=True;"/>
  </connectionStrings>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>
Posted
Updated 25-Jun-14 0:36am
v2
Comments
Vedat Ozan Oner 25-Jun-14 6:38am    
do you your service up and running? have you tested it?

1 solution

first of all if you want to get data from android app and store in database , then create a wcf service , then define [OperationContract] in interface like below

[OperationContract]
[WebInvoke(UriTemplate = "/GetBarCodeData/{barcode}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string GetBarCodeData(string barcode);


then write the logic in .svc.cs for inserting data in server (Like traditional way)

now go to web.config file
in <system.servicemodel> define end points and behaviours
XML
<pre> <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding bypassProxyOnLocal="true" useDefaultWebProxy="false" hostNameComparisonMode="WeakWildcard" sendTimeout="10:15:00" openTimeout="10:15:00" receiveTimeout="10:15:00" maxBufferSize="2147483647" transferMode="StreamedRequest" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>

    <services>
      <service name="Pojectname.serviceclassname" behaviorConfiguration="Pojectname.serviceclassnameBehavior">
        <endpoint address="" binding="webHttpBinding" contract="Pojectname.serviceinterfacename" behaviorConfiguration="webBehaviour" />
        <endpoint name="mexHttpBinding" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="Pojectname.serviceclassnameBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
  </system.serviceModel
>

hope it will help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900