Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过wcf将数据插入数据库时不允许使用服务方法,同时出现插入数据服务不允许错误_Wcf_Sql Server 2008 - Fatal编程技术网

通过wcf将数据插入数据库时不允许使用服务方法,同时出现插入数据服务不允许错误

通过wcf将数据插入数据库时不允许使用服务方法,同时出现插入数据服务不允许错误,wcf,sql-server-2008,Wcf,Sql Server 2008,通过wcf向数据库插入数据时不允许使用服务方法,同时出现插入数据时不允许使用服务的错误。 代码:Iservice [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/InsertEmployeeDetails", RequestFormat = WebMessageFormat.Json, ResponseFor

通过wcf向数据库插入数据时不允许使用服务方法,同时出现插入数据时不允许使用服务的错误。
代码: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>
[运营合同]
[WebInvoke(Method=“POST”,
UriTemplate=“/InsertEmployeeDetails”,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
字符串插入EmployeeDetails(EmployeeDetails empInfo);
///
代码:服务
公共字符串InsertEmployeeDetails(EmployeeDetails empInfo)
{
字符串消息;
string connectionString=“数据源=;初始目录=emp;用户Id=sa;密码=sql@2014";
使用(SqlConnection连接=新的SqlConnection(connectionString))
{
connection.ConnectionString=ConnectionString;
connection.Open();
SqlCommand cmd=connection.CreateCommand();
string sql=“插入emp_表(名称、电子邮件、电话、名称、部门、传真、登录id、密码)值(@name、@email、@phone、@designation、@department、@fax、@login_id、@password)”;
cmd.CommandText=sql;
cmd.Parameters.Add(新的SqlParameter(“@name”,empInfo.emp_name));
cmd.Parameters.Add(新的SqlParameter(“@email”,empInfo.emp_email));
Add(新的SqlParameter(“@phone”,empInfo.emp_phone));
cmd.Parameters.Add(新的SqlParameter(“@designation”,empInfo.emp_designation));
cmd.Parameters.Add(新的SqlParameter(“@department”,empInfo.emp_department));
cmd.Parameters.Add(新的SqlParameter(“@fax”,empInfo.emp_fax));
Add(新的SqlParameter(“@login_id”,empInfo.emp_login));
cmd.Parameters.Add(新的SqlParameter(“@password”,empInfo.emp_password));
int result=cmd.ExecuteNonQuery();
如果(结果==1)
{
Message=empInfo.emp_login+“成功插入详细信息”;
}
其他的
{
Message=empInfo.emp_login+“发生错误,未插入详细信息”;
}
返回消息;
}
}
////
代码:web.config

您在客户端级别和接口级别犯了一些小错误。以下是正确的代码

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

有关详细信息,请单击此链接

您遇到的错误类型,请编写错误描述发布您遇到的错误服务方法不允许没有问题服务代码和端点。我认为您在客户端代码中出错。让我们看看您的客户端代码。您在哪里调用此方法。$.ajax({type:“POST”,url:“,数据:JSON.stringify({Name:Name,Email:Email,Phone:Phone,Designation:Designation,Department:Department,Fax:Fax,Login_Id:loginid,Password:Password}),数据类型:“JSON”,内容类型:“application/JSON;charset=utf-8”,重载后提交:true,成功:函数(msg){jQuery(“#list”)。jqGrid('addRowData');}}});});});同时查看上述链接。它显示为:不允许使用服务方法。请参阅服务帮助页,以构造对服务的有效请求。