The Restful service of WCF implements POST requests

Source: Internet
Author: User

This program is based on vs2013, ef6.1, and WCF

There are two methods for WCF: soap and restful.

Because the program is based on the PCL (portable Class Library), you cannot directly introduce the WCF Service.

There are some restful articles on the Internet, but none of them solve my problem. Finally, I found a solution on stackoverflow.

Let's get down to the truth. Let's take a look at the code structure first (I am also using it for the first time. The structure may be poor. Please join us)

 

The client is used for testing. Nothing can be ignored.

Contracts is a contract.

Services is the implementation contract

Entity is the EF Entity Framework

Hostingservice is a host service made of Windows service.

1. Contracts

I did not have a deep understanding of the classification of the contract, so I wrote a rough article and looked at the detailed Code directly.

1 [ServiceContract]2     public interface IBoardService3     {4         [OperationContract]5         [WebInvoke(Method = "POST", UriTemplate = "getConfigData/{email}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]6         string GetConfigData(string email);7     }


The post request is required. If get is required, change webinvoke to webget.

Ii. Services

1 public class BoardService : IBoardService2     {3         public string GetConfigData(string email)4         {5             return "successed";6         }7     }

In this way, you will prompt that the addressfilter and endpointdispatcher do not match during the call. I found this, which is also a problem that has plagued me for a long time.

Finally found the answer in http://stackoverflow.com/questions/6919768/rest-wcf-service

Add [servicebehavior (addressfiltermode = addressfiltermode. Any)] to the class.

Then it changes. The error is changed to a mismatch between the contractfilter and endpointdispatcher. Don't worry. Here, it indicates that the WCF part is OK.

Iii. hostingservice

This is the key app. config. If the configuration is correct, you can post it as follows:

 1   <system.serviceModel> 2     <bindings> 3       <webHttpBinding> 4         <binding name="boardServiceBinding"> 5           <security mode="None"/> 6         </binding> 7       </webHttpBinding> 8     </bindings> 9 10     <protocolMapping>11       <add scheme="webHttp" binding="webHttpBinding" />12     </protocolMapping>13 14     <behaviors>15       <serviceBehaviors>16         <behavior name="BoardBehavior">17           <serviceMetadata httpGetEnabled="true" />18           <serviceDebug includeExceptionDetailInFaults="false" />19         </behavior>20       </serviceBehaviors>21 22       <endpointBehaviors>23         <behavior name="REST">24           <webHttp />25         </behavior>26       </endpointBehaviors>27     </behaviors>28 29     <services>30       <service behaviorConfiguration="BoardBehavior" name="BoardServices.Services.BoardService">31         <endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding"32           contract="BoardContracts.ServiceContract.IBoardService">33           <identity>34             <dns value="localhost" />35           </identity>36         </endpoint>37         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />38         

Note the writing of endpointbehaviors and Endpoint

The Post Service is basically OK.

Iv. Entity

By the way, entity. Here I use code first. As for the differences between the three first types, please go to Google and do not discuss them here.

The first problem is that entity is familiar to me and it is not easy to fully describe the relationship between tables.

So I found that vs2013 has a code first mode for importing data from the database. SQL server will use this mode. Create a table first and then import it.

I don't want to use images here. EF import is really simple.

 

The code will be uploaded after going home. It's really messy, so you can find what you need.

Send a GitHub address, you can download it on your own https://github.com/heyixiaoran/BoardService2

The Restful service of WCF implements POST requests

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.