【发布时间】:2011-03-08 15:30:18
【问题描述】:
我已经抨击这个问题将近 2 天了。我已经阅读了数百篇博客、SO 帖子和 MSDN 文章,但仍然没有让我的 WCF 服务正常运行!
一些背景: 我有两个项目:
- 1 C# Web 应用程序 - WCF 数据将由 JQuery 使用。我已经编写了一个 JQuery 代理,但我不确定它是否有效,因为我的 WCF 服务似乎没有响应!
- 1 C# WCF 服务 - 目前,我不想要任何比针对某些数据的验证服务更复杂的东西。我想使用 JQuery 来检查这些服务,以验证用户在表单上的输入(例如有效的电子邮件)。
服务 - 目前,我只是想让 IsEmailValid 工作:
namespace AtomicService
{
[ServiceContract]
public interface IValidation
{
[OperationContract, WebInvoke(
Method="POST",
BodyStyle= WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string IsEmailValid(string email);
[OperationContract]
bool DoesClientExist(string client);
[OperationContract]
bool IsPasswordOk(string password);
[OperationContract]
bool IsPostcodeValid(string postcode, string isoalpha2);
[OperationContract]
bool IsTelephoneValid(string telephone, string isoalpha2);
}
}
我的实现:
namespace AtomicService
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Validation : IValidation
{
public string IsEmailValid(string email)
{
return string.Format("Response:{0}", AtomicCore.Validation.CheckEmail(email).ToString());
}
//others removed for brevity
}
}
当我使用 WCFTestClient 测试我的 web 服务时,一切都很好:
使用 Fiddler 检查会返回 400 错误请求:
此外,当我尝试使用浏览器访问服务时,绝对什么都没有发生 - 只是一个空白页面。
当我使用我的 JQuery 代理时:
/// <reference path="~/System/jquery.js" />
ServiceProxy = function () //constructor for the proxy
{
this._baseURL = "http://127.0.0.1:88/Validation.svc/";
};
ServiceProxy.prototype =
{
// getArticles: function (success, error) {
// this._doAjax("GetArticles", null, success, error);
// },
isEmailValid: function (email, success, error) {
var data = { email: email };
this._doAjax("IsEmailValid", data, success, error)
},
_defaultErrorHandler: function (xhr, status, error) {
alert(xhr.statusText + ' ' + error);
},
_doAjax: function (method, data, fnSuccess, fnError) {
if (!data) data = {};
if (!fnError) fnError = this._defaultErrorHandler;
$.ajax({
type: "GET",
url: this._baseURL + method,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: fnSuccess,
error: fnError,
dataFilter: function (data) {
var response;
if (typeof (JSON) !== "undefined" && typeof (JSON.parse) === "function")
response = JSON.parse(data);
else
response = eval("(" + data + ")");
if (response.hasOwnProperty("d"))
return response.d;
else
return response;
}
});
}
};
我收到一个错误 - 但不知道错误是什么。我再也看不到树木了!
我完全糊涂了? WCF 似乎比旧的 .NET ASMX 服务方法复杂得多,但我想尝试学习 - 到目前为止它一直很痛苦!
一如既往的帮助,不胜感激。
【问题讨论】: