WCF usage Summary: (1) how to process the received http post data by WCF

Source: Internet
Author: User

In WCF 4.0, it provides better support for creating restful APIs. By defining uritemplate, webinvoke can quickly develop API interfaces.

 

Here I will record how to receive post data during HTTP POST.

 

1. Method 1: stream inputstream input stream method (check the method carefully)

 

For example, my code

[Operationcontract]
[Webinvoke (method = "Post", responseformat = webmessageformat. JSON, bodystyle = webmessagebodystyle. Bare,
Uritemplate = "JSON/UI/{projectname}/{entityname }? Q = {querystring} & id = {indentity} ")]
Updateorinsertentityresponse

(String projectName, String entityName, String queryString, String indentity, Stream pstream );

 

UriTemplate defines the parameter matching relationship.

"Json/ui/{projectName}/{entityName }? Q = {queryString} & id = {indentity }"

Corresponding Parameter

String projectName, String entityName, String queryString, String indentity

The name must be the same; otherwise, the name cannot match. All fields must be of the String type.

 

How to obtain POST data information.

Define the Stream pstream parameter.

 

If you run the application now, an error message is displayed on the page:

System. invalidoperationexception: For request in operation updateorinsertentityresponse to be a stream the operation

Must have a single parameter whose type is Stream.

 

 

 

How to solve the problem.

Step 1: Modify your own Service. svc file.

Convert the original

<% @ ServiceHost Language = "C #" Debug = "true" Service = "Vine. DataMan. RestfulService. EntityService"

CodeBehind = "EntityService. svc. cs" %>

 

Add new attributes:

Factory = "System. ServiceModel. Activation. WebServiceHostFactory"

 

The final result is:

<% @ ServiceHost Language = "C #" Debug = "true" Service = "Vine. DataMan. RestfulService. EntityService"

CodeBehind = "EntityService. svc. cs" Factory = "System. ServiceModel. Activation. WebServiceHostFactory" %>

 

Modify the configuration file Web. config

<System. serviceModel>

<Services>

<Service name = "Vine. DataMan. RestfulService. EntityService">

<Endpoint binding = "webHttpBinding"

Contract = "Vine. DataMan. RestfulService. ServiceContracts. IEntityCommonService"

BehaviorConfiguration ="Webhttp"/>

</Service>

</Services>

<Behaviors>

<EndpointBehaviors>

<Behavior name ="Webhttp">

<WebHttp/>

</Behavior>

</EndpointBehaviors>

<ServiceBehaviors>

<Behavior>

<! -- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

<ServiceMetadata httpGetEnabled = "true"/>

<! -- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->

<ServiceDebug includeExceptionDetailInFaults = "true"/>

</Behavior>

</Servicebehaviors>

</Behaviors>

<Servicehostingenvironment aspnetcompatibilityenabled = "true"/>

</System. servicemodel>

 

Note the bold text. The webhttp behavior must be defined.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.