Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF WebInvoke POST - Method not allowed error

I have an OperationContract method where I am trying to query and insert data into the database. I am using a POST method and calling the service from javascript in the browser. The WCF Service is in the same domain, so I should not have to use JSONP. I am also modifying data so it should be a POST request not a GET request. However I am still getting a "Method not allowed error". Has anyone encountered this situation?

My service is being called at

http://some_url.com/services/ProfileService.svc/json/CurrentUser

Strangely, it seems to be called via a GET request when I go this url even though I specifiy a POST. On the page load, however, it seems to be attempting a POST request.

Browser response when going to the url:

Request URL:http://some_url.com/services/ProfileService.svc/json/CurrentUser
Request Method:GET
Status Code:405 Method Not Allowed
Request Headersview parsed
GET /services/ProfileService.svc/json/CurrentUser HTTP/1.1

Here is my method that I am trying to call:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]

public HIPUser GetCurrentUser()
{
    string[] domainUser;
    string Auth_User = HttpContext.Current.User.Identity.Name.ToString().ToLower();
    domainUser = Auth_User.Split('\\');
    string user = domainUser[1];
    Debug.WriteLine(user);

    ProfileManager pm = new ProfileManager();
    var results = pm.GetUserByUserName(user);

    if (results.Length > 0)
    {
        return results.First();
    }
    else
    {
        Debug.WriteLine("IS NULL");
        var x = pm.CreateUser(user, null, null);
        Debug.WriteLine(x.UserName);
        return x;
    }
}

Client:

function getCurrentUser() {

$.ajax({
    type: "POST",
    url: "services/ProfileService.svc/json/GetCurrentUser",
    contentType: "application/json; charset=utf-8",
    data: null,
    dataType: "json",
    error: function (request, error, u) {
        alert('blargherror: ' + error);
    },
    success: function (result, status) {
        alert(result.d);
    }
});
}

Not sure if needed but Web.Config:

<behaviors>
   <endpointBehaviors>
        <behavior name="jsonBehavior">
            <enableWebScript />
        </behavior>
    </endpointBehaviors>

    <serviceBehaviors>
        <behavior name="metaBehavior">
            <serviceDebug includeExceptionDetailInFaults="true" />
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment 
    aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />

<services>
    <service name="ProfileService" behaviorConfiguration="metaBehavior">
        <endpoint address="/json" 
            behaviorConfiguration="jsonBehavior"
            binding="webHttpBinding" 
            bindingConfiguration="secure" 
            contract="ProfileService" />
        <endpoint address="" 
            binding="basicHttpBinding" 
            bindingConfiguration="secure" 
            contract="ProfileService" />
    </service>
</services>

Edit to Web.Config - Adding bindings

<bindings>
        <webHttpBinding>
            <binding name="secure">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows"/>
                </security>
            </binding>
        </webHttpBinding>
        <basicHttpBinding>
            <binding name="secure">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
like image 667
jocey Avatar asked Jul 10 '12 18:07

jocey


1 Answers

I was able to figure this out. There was an issue with the return object being an unknown type, so it wasn't able to be serialized into a JSON object. Wasn't sure how to serialize the object to get it to work with my method, so I ended up just changing the method to return a string constructing my own custom JSON string, and using the eval() function to turn it into a JSON object.

like image 66
jocey Avatar answered Oct 19 '22 12:10

jocey