Post request WCF Library Project

316 views Asked by At

I am trying to create a POST request on a WCF Service to add to server AllocateCreditToken to a specific MSISDN.

I am having a problem that when I start instance of the project and load the URL with the value I want to add, i get a message on the browser saying "Method Not Allowed"

I am not sure if my AllocateCreditToken() method is correct or not.

Please let me know if you need to see the code as it is kind of a long piece of code in that method.

Thanks

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "AllocateCreditToken?MSISDN={MSISDN}")]
Stream AllocateCreditToken(string MSISDN);
1

There are 1 answers

0
Navoneel Talukdar On

Couple of things you should check

1) On WCF service class below attribute is added.

[AspNetCompatibilityRequirements(RequirementsMode = 
AspNetCompatibilityRequirementsMode.Allowed)]

2) You are using message body style as bare which means WCF will not accept more than one parameter.If you wish to pass more than one parameter, set the BodyStyle property to Wrapped.

Wrapped: Both requests and responses are wrapped.

WrappedRequest: We can wrap requests but cannot wrap the responses.

WrappedResponse: We can wrap responses but cannot wrap the requests.

Bare: Both requests and responses are not wrapped. This is the default member.

3) Use POSTMAN tool to send the request to test the response.

Apart from all these here is a short example,notice as it is a restful service I am sending the single value in url itself as part of OData notation. I have not explicitly mentioned message body style as Bare as it defaults to.

[WebInvoke(Method = "DELETE", UriTemplate = "Employee/{empId}", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
void DeleteEmployee(string empId);

and in ajax call

        $.ajax({
              type: "DELETE",
              url: "http://localhost:8080/MyService/EmployeeService/Employee/2",
              data: "{}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (data) {
                 alert(data);
              },
              error: function (msg) {
                  alert(msg);
              }
          });