New Features of wcf4.0-Simplified configuration, webget support, convenient Message format control, and webfaultexception

Source: Internet
Author: User

The introduction of WCF integrates. Net remoting, WebService, and so on. It simplifies the communication development on the Microsoft platform and is expected to be the integration direction of the communication processing on the Microsoft platform.

[Simplified configuration]

I may remember that the configuration in WCF 3.0 is indeed complex enough. However, Microsoft has released some streamlined configuration methods in this 4.0 release.

For example, if we want to host a serviceCodeIs it much more convenient?

 
   1.0 "?> 
   
    
    
      v4.0 " SKU = "
     . netframework, version = v4.0  "/> 
    
    
    
     
      
       
       
         true "/> 
       
       
      
     
     
     
       wshttpbinding " Scheme = "
       HTTP "/> 
     
     
    
   ---- 
Webservicehost host =NewWebservicehost (Typeof(Echoservice ),NewUri ("Http: // localhost: 8080/echoservice"); Host. open (); console. writeline ("Press any key to stop this service."); Console. readkey (); host. Close ();

[Webget support]

For convenient webget support, you only need to make a few changes in servicecontract. For example:
[Servicecontract]Public InterfaceIecho {[operationcontract, webget (uritemplate ="Echo/{input}")]StringEcho (StringInput);} so that you can use http: // serviceaddress/Echo/Inputvalue.

[Message format control]

If you want to return JSON or text? 1. Define servicecontract

[Operationcontract]
Message getuser1 (string count );

2. Implement services

PublicSystem. servicemodel. channels. Message getuser1 (StringCount) {list <userinfo> result = getuser (count); weboperationcontext. Current. outgoingresponse. contenttype ="Application/JSON";ReturnWeboperationcontext. Current. createjsonresponse <list <userinfo> (result );}

The JSON format is returned. Of course, there are other formats for convenient support.

[Webfaultexception]
You can easily throw such an error, and the browser will receive the HTTP 400 bad request message.

 
 Throw NewWebfaultexception <String> ("Format Error", System. net. httpstatuscode. badrequest );

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.