日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个WCF RESTSOAP Post例子

發布時間:2024/9/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个WCF RESTSOAP Post例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • IService1.cs?
IService1.cs 1 using System.Runtime.Serialization; 2 using System.ServiceModel; 3 using System.ServiceModel.Web; 4 5 namespace WcfService1 6 { 7 // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IService1”。 8 [ServiceContract] 9 [XmlSerializerFormat] 10 public interface IService1 11 { 12 13 [OperationContract] 14 string GetData(string value); 15 16 [OperationContract] 17 [WebInvoke(UriTemplate = "/GetDataUsingDataContract", RequestFormat = WebMessageFormat.Xml, BodyStyle= WebMessageBodyStyle.Bare)] 18 CompositeType GetDataUsingDataContract(CompositeType composite); 19 20 // TODO: 在此添加您的服務操作 21 } 22 23 24 // 使用下面示例中說明的數據約定將復合類型添加到服務操作。 25 [DataContract()] 26 public class CompositeType 27 { 28 bool boolValue = true; 29 string stringValue = "Hello "; 30 31 [DataMember] 32 public bool BoolValue 33 { 34 get { return boolValue; } 35 set { boolValue = value; } 36 } 37 38 [DataMember] 39 public string StringValue 40 { 41 get { return stringValue; } 42 set { stringValue = value; } 43 } 44 } 45 }

?

  • Service1.cs
  • Service1.cs using System; using System.ServiceModel; using System.ServiceModel.Web;namespace WcfService1 {[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]public class Service1 : IService1{[WebGet(UriTemplate = "/GetData/{value}", BodyStyle = WebMessageBodyStyle.Wrapped)]public string GetData(string value){return string.Format("You entered: {0}", value);}public CompositeType GetDataUsingDataContract(CompositeType composite){if (composite == null){throw new ArgumentNullException("composite");}if (composite.BoolValue){composite.StringValue += "[Suffix]";}return composite;}} }

    ?

  • Web.config
  • View Code <?xml version="1.0" encoding="utf-8"?> <configuration><appSettings><add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /></appSettings><system.web><compilation debug="true" targetFramework="4.5" /><httpRuntime targetFramework="4.5"/></system.web><system.serviceModel><behaviors><endpointBehaviors><behavior name="HelpBehavior"><webHttp helpEnabled="true" /></behavior><behavior name="poxBehavior"><webHttp /></behavior><behavior name="jsonBehavior"><enableWebScript /></behavior><behavior name="restBehavior"><webHttp /></behavior><behavior name="webHttp"><webHttp/></behavior></endpointBehaviors><serviceBehaviors><behavior name="MyServiceBehavior"><serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /><serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true" /></behavior><behavior name=""><serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /><serviceDebug includeExceptionDetailInFaults="false" /></behavior></serviceBehaviors></behaviors><serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" /><services><service name="WcfService1.Service1" behaviorConfiguration="MyServiceBehavior"><host><baseAddresses><!--note, choose an available port--><add baseAddress="http://localhost:53864/Service1.svc/" /></baseAddresses></host><endpoint name="rest" address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="HelpBehavior"/><endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/><endpoint name="soap" address="soap" binding="wsHttpBinding" contract="WcfService1.IService1" bindingConfiguration="NoSecurity"/><!--<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="WcfService1.IService1"/>--></service></services><bindings><webHttpBinding><binding name="webHttpBinding" ></binding></webHttpBinding><customBinding><binding name="basicConfig"><binaryMessageEncoding/><httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/></binding></customBinding><wsHttpBinding><binding name="NoSecurity"><security mode="None"></security></binding></wsHttpBinding></bindings></system.serviceModel><system.webServer><modules runAllManagedModulesForAllRequests="true"/><!--若要在調試過程中瀏覽 Web 應用程序根目錄,請將下面的值設置為 True。在部署之前將該值設置為 False 可避免泄露 Web 應用程序文件夾信息。--><directoryBrowse enabled="true"/></system.webServer> </configuration>

    ?

  • Rest Request?內容

Sample 來源

http://localhost:53864/Service1.svc/help

?

<CompositeType><BoolValue>true</BoolValue><StringValue>字符串內容3</StringValue></CompositeType>

?

?

  • Soap Request 內容

Content-Type: application/soap+xml; charset=UTF-8

Sample來源

D:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\WcfTestClient.exe

?

View Code <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Header><a:Action s:mustUnderstand="1">http://tempuri.org/IService1/GetDataUsingDataContract</a:Action><a:MessageID>urn:uuid:175a23b5-9b03-4b04-ba03-64fc95e124e4</a:MessageID><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo></s:Header><s:Body><GetDataUsingDataContract xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/"><composite><BoolValue>true</BoolValue><StringValue>222333333</StringValue></composite></GetDataUsingDataContract></s:Body></s:Envelope>

?

?

  • WCF Console Host

Web.config拷貝到 app.config

用管理員運行VS或者wcf host程序

?

View Code namespace WCFConsoleHost {class Program{static void Main(string[] args){using (ServiceHost host = new ServiceHost(typeof(WcfService1.Service1))){if (host.State != CommunicationState.Opening){host.Open();//顯示運行狀態Console.WriteLine("Host is runing! and state is {0}", host.State);}Console.WriteLine("press enter key to end.");Console.ReadLine();}}}

?

  • 本源代碼地址

https://docs.google.com/open?id=0B6FO9TFqRpUbMXZPd3IzSEJyWEE

轉載于:https://www.cnblogs.com/answerlover/archive/2012/10/01/2709523.html

總結

以上是生活随笔為你收集整理的一个WCF RESTSOAP Post例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。