简体   繁体   中英

service method not allowed in inserting data into database through wcf, while inserting data service not allowed error arose

service method not allowed in inserting data into database through wcf, while inserting data service not allowed error arose.
code:Iservice

        [OperationContract]
        [WebInvoke(Method = "POST",
             UriTemplate = "/InsertEmployeeDetails",
             RequestFormat = WebMessageFormat.Json,
             ResponseFormat = WebMessageFormat.Json)]
        string InsertEmployeeDetails(EmployeeDetails empInfo);


///
code:service


        public string InsertEmployeeDetails(EmployeeDetails empInfo)
        {
            string Message;

            string connectionString = "Data Source=.; Initial Catalog=emp ;User Id=sa; Password=sql@2014";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.ConnectionString = connectionString;
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();
                string sql = "insert into emp_table(name,email,phone,designation,department,fax,login_id,password) values(@name,@email,@phone,@designation,@department,@fax,@login_id,@password)";
                cmd.CommandText = sql;
                cmd.Parameters.Add(new SqlParameter("@name", empInfo.emp_name));
                cmd.Parameters.Add(new SqlParameter("@email", empInfo.emp_email));
                cmd.Parameters.Add(new SqlParameter("@phone", empInfo.emp_phone));
                cmd.Parameters.Add(new SqlParameter("@designation", empInfo.emp_designation));
                cmd.Parameters.Add(new SqlParameter("@department", empInfo.emp_department));
                cmd.Parameters.Add(new SqlParameter("@fax", empInfo.emp_fax));
                cmd.Parameters.Add(new SqlParameter("@login_id", empInfo.emp_login));
                cmd.Parameters.Add(new SqlParameter("@password", empInfo.emp_password));
                int result = cmd.ExecuteNonQuery();
                if (result == 1)
                {
                    Message = empInfo.emp_login + "Details inserted Successfully";
                }
                else
                {
                    Message = empInfo.emp_login + "Error occured, Details not inserted";
                }
                return Message;


            }
        }
////
code:web.config


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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="MetadataExchangeHttpBinding_IService1">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:57137/Service1.svc/mex" binding="wsHttpBinding"
        bindingConfiguration="MetadataExchangeHttpBinding_IService1"
        contract="ServiceReference1.IService1" name="MetadataExchangeHttpBinding_IService1" />
    </client>
    <services>
      <service name="Database_WCF.Service1" behaviorConfiguration="Database_WCFService.Service1Bahaviors">
        <endpoint address="" binding="webHttpBinding" contract="Database_WCF.IService1" behaviorConfiguration="ServiceAspNetAjaxBehavior">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="Database_WCF.IService1" ></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Database_WCFService.Service1Bahaviors">
          <!-- 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="ServiceAspNetAjaxBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
     <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Headers" value="Content-Type, Accept, X-Requested-With" />
          <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
        </customHeaders>
      </httpProtocol>

    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        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>

</configuration>

You made small mistakes in client level and Interface level.Here is the correct code

var jsonData={ Name: name, Email: email, Phone: phone, Designation: designation, Department:    department, Fax: fax, Login_Id: loginid, Password: password };
$.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8", 
   url:"http://localhost/asd/Service1.svc/InsertEmployeeDetails",
   data: jsonData,
   dataType: "Json",            
   reloadAfterSubmit: true,
   success: function (msg) {
      jQuery("#list").jqGrid('addRowData'); 
   } 
  });
 }); 
});

Interface Code

[OperationContract]
[WebInvoke(Method = "POST",UriTemplate ="/InsertEmployeeDetails?emp_name={Name}&emp_email={Email}",RequestFormat =WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
string InsertEmployeeDetails(EmployeeDetails empInfo);

For more information click this link http://forums.asp.net/t/1702719.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM