开发者

WCF Webservice with JSON output misbehaving

I have been bashing this around for nearly 2 days now. I have read hundreds of blogs, SO posts and MSDN articles and I have still not got my WCF service to behave!

Some background: I have two projects:

  • 1 C# Web Application - where the WCF data will be consumed by JQuery. I have written a JQuery proxy, but I'm unsure if it works yet, because my WCF service doesn't seem to respond!
  • 1 C# WCF Service - Currently, I don't want anything more complicated than some validation services against some data. I wanted to use JQuery to check these services to validate a users input on a form (a valid email for instance).

The service - currently, I am just trying to get IsEmailValid to work:

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

My implementation:

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
    }
}

When I test my webservice using the WCFTestClient, all is well:

WCF Webservice with JSON output misbehaving

Checking with Fiddler returns a 400 Bad Request:

WCF Webservice with JSON output misbehaving

Further, when I try to access the service with a browser, absolutely nothing happens - just a blank page.

When I use my JQuery proxy:

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


};

I get an error - but have no idea what the error is. I can't see the wood for the tr开发者_StackOverflowees any longer!

I'm completely baffled? WCF seems horrifically more complicated than the old .NET ASMX service method, but I wanted to try and learn - and it's been painful so far!

Help as always, appreciated.


You're doing a GET in fiddler, and your method is set up to work with a POST.

You can change to doing a POST in fiddler, which should work once you've set up the request body. You could capture what WCFTestClient is doing to give you some idea as to the difference.


I myself have used WCF Data Service with a EDMX object in C# 4.0 and one thing you need to watch for is cross domain requests. I am not sure if this is the case here but it is always something to watch for. If the port is different, it will fall under the cross domain category and therefor be blocked by the browser unless you explicitly allow it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜