[英]calling WCF from ajax and getting this error,Failed to load:server responded with a status of 500 (System.ServiceModel.ServiceActivationException)
I am new to WCF and got stuck and I am pretty sure that,error is in web.config,tried many changes in web.config file to get clear this issue, but it not work and failed to findout,i need help,thanks in advance................................................
我是 WCF 的新手并且被卡住了,我很确定,错误在 web.config 中,尝试了 Z2567A5EC9705EB7AC2C984033E0618,9DksZ.config 文件中的许多更改来解决这个问题,但没有解决这个问题,但没有帮助,提前................................................
web.config web.config
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<connectionStrings>
<add name="DBCS" connectionString="server=ddcs; initial catalog=user;integrated security =SSPI"/>
</connectionStrings>
<compilation debug="true" targetFramework="4.7.2"/>
<httpRuntime targetFramework="4.7.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior" >
<!-- 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">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="Service" behaviorConfiguration="ServiceAspNetAjaxBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<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>
Service.cs
服务.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
public Userdetails[] GetUserdetailsByEmailId(throughEmail e)
{
List<Userdetails> userdetails = new List<Userdetails>();
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("wcf_ajax_getdata", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@emailid";
parameter.Value = e.email;
cmd.Parameters.Add(parameter);
con.Open();
SqlDataAdapter rdr = new SqlDataAdapter(cmd);
DataTable dtresult = new DataTable();
rdr.Fill(dtresult);
if (dtresult.Rows.Count > 0)
{
for (int i = 0; i < dtresult.Rows.Count; i++)
{
Userdetails userInfo = new Userdetails();
userInfo.First_Name = dtresult.Rows[i]["First_Name"].ToString();
userInfo.Middle_Initial = dtresult.Rows[i]["Middle_Initial"].ToString();
userInfo.Last_Name = dtresult.Rows[i]["Last_Name"].ToString();
userInfo.Email_Id = dtresult.Rows[i]["Email_Id"].ToString();
userInfo.Emp_Id = Convert.ToInt32(dtresult.Rows[i]["Emp_Id"].ToString());
userInfo.Designation = dtresult.Rows[i]["Designation"].ToString();
userInfo.Joining_Date = dtresult.Rows[i]["Joining_Date"].ToString();
userInfo.Salary = dtresult.Rows[i]["Salary"].ToString();
userInfo.Address = dtresult.Rows[i]["Address"].ToString();
userdetails.Add(userInfo);
}
}
return userdetails.ToArray();
}
}
}
IService.cs
IService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Userdetails[] GetUserdetailsByEmailId(throughEmail e);
}
Default.aspx
默认.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="font-family:Arial">
Email ID :
<input id="txtId" type="text" style="width: 86px" />
<input type="button" id="btnGetUser" value="Get User" />
<br /><br />
<table border="1" style="border-collapse: collapse">
<tr>
<td>First Name</td>
<td><input id="txtName" type="text" /></td>
</tr>
<tr>
<td>Middle Name</td>
<td><input id="txtmname" type="text" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input id="txtlname" type="text" /></td>
</tr>
</table>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btnGetUser').click(function () {
throughEmail = {};
throughEmail.email = $('#txtId').val();
$.ajax({
type: "POST",
url: "/Service.svc/GetUserdetailsByEmailId",
data: JSON.stringify(throughEmail),
//contentType: "text/xml; charset=utf-8",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#txtName').val(data.d.First_Name);
$('#txtmname').val(data.d.Middle_Initial);
$('#txtlname').val(data.d.Last_Name);
},
error: function (err) {
alert(err);
}
});
});
});
</script>
</html>
According to the code you provided, I made a demo.根据你提供的代码,我做了一个demo。 This is the web.config of the demo:
这是演示的 web.config:
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior" >
<!-- 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">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="ServiceDemo.Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="ServiceDemo.IService" behaviorConfiguration="ServiceAspNetAjaxBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<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>
I used Postman to test this service, and it sent back data normally.
According to the information you provided, I found that you are using webhttpbinding, but I did not find in your configuration file.根据你提供的信息,我发现你使用的是webhttpbinding,但是我在你的配置文件中没有找到。 If you use webhttpbinding, you must configure in the configuration file.
如果使用 webhttpbinding,则必须在配置文件中进行配置。
<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
We need to add to the endpointBehavior.我们需要添加到endpointBehavior。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.