RuSh
RuSh

Reputation: 1693

Migrating ASMX Service to WCF Service - AJAX Post

I have a asp.net asmx service and i would like to convert it to a wcf service.

I'm having problems calling the wcf service from jquery ajax POST request with parameters.

if i call the WCF service without parameters or pass the parameter in a json format it works OK.

When executing the below jquery post to the wcf service i get error 500.

Please note , i cannot change the way the jquery request is made.

Original ASMX Service:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void SendStatus(string param1, string param2)
{
..................
}

jQuery POST:

var d = { param1: 1, param2: 2 };
        $.ajax({
            type: "POST",
            url: "/Service1.asmx/SendStatus",
            data: d,
            success: function () { }
        });

NEW WCF Service:

[OperationContract]
[WebInvoke]

public void SendStatus(string param1, string param2)
{
}

jQuery POST:

var d = { param1: 1, param2: 2 };
 $.ajax({
            type: "POST",
            url: "/Service2.svc/SendStatus",
            data: d,
            success: function () { }
        });

Upvotes: 4

Views: 908

Answers (3)

Dave Ward
Dave Ward

Reputation: 60570

If you can't change the client-side code, you should not migrate these endpoints from ASMX to WCF. WCF uses a different, less-flexible serializer than ASMX and it's likely that you'll run into trouble that can only be resolved by changing how data is sent, received, and/or handled on the client-side.

If you're willing to deal with that turmoil anyway, a much better migration path would be waiting until ASP.NET Web API is shipped and moving to it. If you move to WCF now, you'll just be behind again later this year when Web API is released.

Upvotes: 1

Anvesh
Anvesh

Reputation: 319

I think you have to pass string parameter values in double quotes (""). Like this:

var d = { param1: "1", param2: "2" };
     $.ajax({
                type: "POST",
                url: "/Service2.svc/SendStatus",
                data: d,
                success: function () { }
            });

Hope this will work.

500 error code means parameter values didn't match with required values.

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102793

-- EDIT -- I recall this issue drove me nuts once before, so I went back for another look. Sure enough ... Given the requirement that the Javscript remain as written, I maintain that this is literally impossible with the current release of WCF. Consider the following points:

1) You need to use webHttpBinding, because that's the only binding that supports REST style services (basicHttpBinding and WSHttpBinding both use SOAP wrappers). (ref. here: BasicHttpBinding vs WsHttpBinding vs WebHttpBinding)

2) The AJAX call as written in this question uses a content-type of "application/x-www-form-urlencoded" (you can confirm this using Fiddler).

3) You can also confirm that WCF throws an exception before the service method even gets invoked. The exception is as follows:

The body style 'Bare' is not supported by 'WebScriptEnablingBehavior'. Change the body style to be 'WrappedRequest'.

But "bare" body style is Microsoft-speak for a REST request using basic parameters (ie, not "wrapped" in JSON or XML). That is, there is no possible configuration that would allow WCF to handle this specific AJAX request. You can even implement your own WebContentTypeMapper, and it still won't work. This guy is on to them: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2473190-consider-making-the-system-servicemodel-channels-r

My conclusion is that (given you can't use MVC, which would make this a piece of cake), you need to somehow route this request to a basic .ASPX page, and use the trusty old Webforms methods (Page.IsPostBack, Request.Params["param1"], etc).

-- END EDIT --

Per the other thread above, looks like there are a few parameters you need to add/fix in your AJAX call:

...
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(d)
...

Upvotes: 2

Related Questions