- 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