一个WCF RESTSOAP Post例子
生活随笔
收集整理的這篇文章主要介紹了
一个WCF RESTSOAP Post例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- IService1.cs?
?
- 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例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS正则表达式验证账号、手机号、电话和邮
- 下一篇: 第二十一章流 14临时文件