4

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 () { }
        });
RuSh
  • 1,643
  • 7
  • 25
  • 39
  • My editorial comment-- I'd recommend using MVC3, which makes it far easier to configure request routes than WCF. I have been underwhelmed by WCF's flexibility, as well as overwhelmed by the unintuitiveness of its configurations (although I guess it would be good for more advanced network setups). – McGarnagle Apr 01 '12 at 22:03
  • MVC is nice but i already have an existing project in web forms. – RuSh Apr 01 '12 at 22:25

3 Answers3

2

-- 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)
...
Community
  • 1
  • 1
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • i don't want to change the parameters to json . i need to leave the ajax code AS IS and change the wcf service to match the ajax. – RuSh Apr 01 '12 at 21:15
  • @sharru gotcha. May I suggest editing your question to emphasize that point? If you don't get a satisfactory answer I would experiment with the attributes of WebInvoke such as RequestFormat: http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute_properties.aspx. – McGarnagle Apr 01 '12 at 21:58
  • 1. thanks for your input. i did emphasize it ,its in bold :) 2.i did experiment with WebInvoke , could not find something that works.. – RuSh Apr 01 '12 at 22:24
1

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.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
0

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.

Anvesh
  • 309
  • 1
  • 8
  • 24