I have 2 projects (WCF project and Asp.Net project). I followed this tutorial here .
In this tutorial, we realize all in one project (see sources here ).
I have a reference to my WCF project in Web project.
I try to display a value in my alert but there is nothing.
Here is my Service1.svc :
namespace WSSage100{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string GetData2(int value)
{
return string.Format("You entered: {0}", value);
}
public string[] GetUser(string Id)
{
return new User().GetUser(Convert.ToInt32(Id));
}
public string GetTEST()
{
return "OKKKKKKKK";
}
}
Here is IService1.cs :
namespace WSSage100
{
[XmlSerializerFormat]
[ServiceContract(Namespace ="WSSage100")]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
string GetData2(int value);
[OperationContract]
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetTEST();
Here is my web page (aspx) with JavaScript code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" type="text/javascript" src="JavaScript/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
function WCFJSON() {
var userid = "1";
Type = "POST";
Url = "Service1.svc/GetUser";
Data = '{"Id": "' + userid + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json"; varProcessData = true;
CallService();
}
function CallService() {
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null;
varUrl = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function ServiceSucceeded(result) {
if (DataType == "json") {
resultObject = result.GetUserResult;
for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
$(document).ready(
function () {
WCFJSON();
}
);
</script>
<style type="text/css">
#form1 {
height: 255px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div></div>
</form>
</body>
</html>
Here is Service1.svc in my Web project :
<%@ ServiceHost Language="C#" Debug="true" Service="WSSage100.Service1"%>
Here is my web.config in my Web project :
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<client>
<endpoint address="http://localhost:52768/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WebServiceSage100.IService1"
name="BasicHttpBinding_IService1" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<services>
<service name="WSSage100.Service1">
<endpoint address="" binding="webHttpBinding" contract="WSSage100.IService1">
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
Brian, localhost:52768/Service1.svc display
I have this error when I use Inspect Element
I'm new in this domain and sorry for my bad english.
I had 2 projects in my solution, the WCF and an application console (GettingStartedHost) that will do to turn back the web service.
I also added the connectionstrings in app.config of the GettingStartedHost project and now it works well.
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.