How to pass parameter(s) to a WCF post method (a restful services)

五迷三道 提交于 2019-12-19 04:54:27

问题


I am working on a WCF rest based services. I have written Get and Post methods in my service and Get methods are able to working (fetching data) when I typed in URL (in JSON format.

Problem is when I try to do the same for POST methods, the url was navigating to some other page "Page not found...".

I understood that a POST method requires a form submission to process the request.

For that the reason, I have tried chrome extensions (Simple Rest client, Advanced Rest client, Post man rest client) and Fiddler also.

Here I am posting my service method - Get method (interface method declaration).

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, 
ResponseFormat =   WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, 
UriTemplate = "GetCategoryTypes/")]
List<CategoryType> GetCategoryTypes();

and this is my POST method

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddOrders/", 
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int AddOrders(decimal amount, int tableID, DateTime orderDate, int isActive);

Here is my web.config file for the service.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
   </system.web>  
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ServiceBehaviour" allowCookies="true" messageEncoding="Mtom" />
      </basicHttpBinding>
      <webHttpBinding>
        <binding name="ServiceBehaviour1" allowCookies="true"/>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="EMC.DD.ServiceLayer.Service1" ehaviorConfiguration="ServiceBehaviour">
        <endpoint address="http://localhost/EMCService/Service1.svc" 
           binding="basicHttpBinding" contract="EMC.DD.ServiceLayer.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>      
      </service>
      <service name="EMC.DD.ServiceLayer.Service2" 
         behaviorConfiguration="ServiceBehaviour1">
        <endpoint address="http://localhost/EMCService/Service2.svc" 
          binding="webHttpBinding" behaviorConfiguration ="web" 
          contract="EMC.DD.ServiceLayer.IService2">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>     
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name ="ServiceBehaviour">     
          <serviceMetadata httpGetEnabled="true" />      
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name ="ServiceBehaviour1">       
          <serviceMetadata httpGetEnabled="true" />        
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name ="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors> 
  </system.serviceModel>
 <system.webServer>
        <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

I am not really sure whether there is any mistake in my method constructing (POST method) or else the way I need to test it.

I need an help from all of your experts and I am fighting with this issue for the past 2 days and atlast I came here to post it.

Any help highly appreciated.


回答1:


Finally after many trials and many things, I got the working solution. Here I am posting what I did for the POST method to work.

  [OperationContract]
  [WebInvoke(Method = "POST", UriTemplate = "AddOrders", RequestFormat =   
  WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle = 
  MessageBodyStyle.Bare)]
  int AddOrders(RequestData orderRequestData);

This is for implementation in the operation contract.

Client application:-

        WebClient WC = new WebClient();
        WC.Headers.Add("Content-Type", "application/json");
        WC.Encoding = Encoding.UTF8;

        MemoryStream MS = new MemoryStream();
        DataContractJsonSerializer JSrz = new 
        DataContractJsonSerializer(typeof(RequestData));
        JSrz.WriteObject(MS, order);
        string data = Encoding.UTF8.GetString(MS.ToArray(), 0, (int)MS.Length);

        byte[] res1 = 
        WC.UploadData("http://localhost/EMCService/Service2.svc/AddOrders", "POST",MS.ToArray());

        MS = new MemoryStream(res1);
        JSrz = new DataContractJsonSerializer(typeof(int));
        int result = (int)JSrz.ReadObject(MS);

I did not made any config settings and still using the old settings of web.config what I have posted in the above question, and it is working.



来源:https://stackoverflow.com/questions/25830289/how-to-pass-parameters-to-a-wcf-post-method-a-restful-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!