簡體   English   中英

通過wcf將數據插入數據庫時​​不允許使用service方法,同時出現不允許插入數據服務的錯誤

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

通過wcf將數據插入數據庫時​​不允許使用service方法,而不允許數據插入服務出現錯誤。
代碼: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>

您在客戶端級別和接口級別上犯了一些小錯誤。這是正確的代碼

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'); 
   } 
  });
 }); 
});

接口代碼

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

有關更多信息,請單擊此鏈接http://forums.asp.net/t/1702719.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM