locked
WebInvoke Post Error : Method Not Allowed RRS feed

  • Question

  • Hello,

    I have created one WCF Data Service with simple entity as below.

    namespace DataService
    {
        using System;
        using System.Collections.Generic;
        
        public partial class MemoryPackageData
        {
            public long c1 { get; set; }
            public long c2 { get; set; }
            public long c3 { get; set; }
            public long c4 { get; set; }
        }
    }

    Below is the method I have defined as a service operation.

    namespace DataService
    {
        public class WCFDataService : DataService<DBEntities>
        {
            // This method is called only once to initialize service-wide policies.
            public static void InitializeService(DataServiceConfiguration config)
            {
                // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
                // Examples:
                config.SetEntitySetAccessRule("MemoryPackageDatas", EntitySetRights.All);
                config.SetServiceOperationAccessRule("InsertEntityData", ServiceOperationRights.All);
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
            }
    
            //[WebInvoke(UriTemplate = "InsertEntityData/?package_id={package_id}&package_size={package_size}")]
            [WebInvoke(UriTemplate = "InsertEntityData?package_id={package_id}")]
            public void InsertEntityData(Int64 package_id, Int64 package_Size = 10)
            {
    // some stuff
    }
    

    Now, when I run this service in firefox and pass one parameter which is mandatory in the URL.

    I have tried many different ways to call this method here. But not sure how to deal with these parameters list.

    Method is inserting data to table.

    Can any one please guide me here?

    Thank you,

    Mittal.

    Thursday, February 19, 2015 10:18 AM

Answers

  • Hello MittalPatel,

    It seems that you are calling the post operation from the URL bar of the web browser, if so, as far as I know, the browser requests the service using HTTP GET while the service method is mapped to HTTP POST, Method = "POST", thus resulting in the error "Method not allowed".

    To fix, you could create a client project as using console application:

    string URI = "http://localhost:46060/WcfDataService1.svc/InsertEntityData?&package_id=1";
    
                string postData = "test=75";
    
                byte[] buffer = Encoding.ASCII.GetBytes(postData);
    
                HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(URI);
    
                WebReq.Method = "POST";
    
                WebReq.ContentType = "application/x-www-form-urlencoded";
    
                WebReq.ContentLength = buffer.Length;
    
                Stream PostData = WebReq.GetRequestStream();
    
                PostData.Write(buffer, 0, buffer.Length);
    
                PostData.Close();
    
                HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    
                Stream Answer = WebResp.GetResponseStream();
    
                StreamReader _Answer = new StreamReader(Answer);
    
                string Text = _Answer.ReadToEnd();
    

    Or try calling the service method from a tool that supports POST, e.g. Fiddler or WcfTestClient.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 20, 2015 2:32 AM
    Moderator