我只是想创建一个非常简单的WCF服务,它将与javascript ajax调用(REST)进行交互,但它不适合我,似乎我迷失了:
服务接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace Wcf.App
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(
UriTemplate = "GetData/{value}",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string getEmpData(string value);
}
}
实现:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace Wcf.App
{
public class Clocked
{
public string date { get; set; }
public string type { get; set; }
}
public class DATA
{
public string firstName { get; set; }
public string lastName { get; set; }
public string emailAddress { get; set; }
public string phoneNum { get; set; }
public string image { get; set; }
public string title { get; set; }
public List<Clocked> clocked { get; set; }
}
public class RootObject
{
public DATA DATA { get; set; }
}
public class Service1 : IService1
{
public string getEmpData(string test)
{
try
{
return " hello " + test;
}
catch (Exception ex)
{
return "Can not open connection! " + ex.Message + " Connection: " + connetionString;
}
}
catch (SqlException e)
{
return e.Message;
}
}
}
}
现在,当我尝试使用jQuery AJAX调用此服务时,它会抛出404未找到错误。
JS:
$.ajax({
url: "Service1.svc/GetData/3046722425",
type: "GET",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
配置:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
<bindings>
<customBinding>
<binding name="basicConfig">
<binaryMessageEncoding/>
<httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864" />
</binding>
</customBinding>
<webHttpBinding>
<binding name="basicConfig">
<security mode="None">
<transport clientCredentialType="Basic"/>
</security>
</binding>
<binding>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<client/>
<services>
<service name="Wcf.App.Service1" behaviorConfiguration="ServiceBehaviour">
<!--<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="Wcf.App.IService1"/>-->
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="Wcf.App.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<behavior name="REST">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ServiceBehaviour">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
</behavior>
<behavior name="REST">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
答案 0 :(得分:0)
请确保您已从here激活了WCF组件。
HTTP Error 404.3 - Not Found" while browsing wcf service on Windows Server 2008(64bit)
答案 1 :(得分:-1)
尝试从浏览器点击一次服务(丢弃方法名称Getdata和param值)或将其添加到WCFTestClient应用程序。