做代碼統計,方便以后使用:
app.config配置文件設置:
<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="mySerBeh">
<serviceMetadata httpGetEnabled="true"/>
<!--httpGetUrl="mex"-->
<!-- 要接收故障異常詳細信息以進行調試,請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息-->
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpendBehavior">
<webHttp></webHttp>
</behavior>
</endpointBehaviors>
</behaviors>
<!-- 看到services節,就表明這是在定義服務相關的內容 -->
<services>
<!-- 定義一個服務,name是契約實現類的全名 -->
<service behaviorConfiguration="mySerBeh" name="WCFExample.WCF.UserService">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:21467/"/>
</baseAddresses>
</host>
<!-- 定義一下終節點,address一般為空,如果不為空,最終服務地址就是在baseAddress的基礎上加上這個address,binding指定為basicHttpBinding,
這是最基礎的基於http的綁定方式,contract標明這是為哪個契約服務 -->
<endpoint address="wcfs" behaviorConfiguration="webHttpendBehavior"
binding="webHttpBinding" bindingConfiguration="webBinding" contract="WCFExample.WCF.IService"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
基本內容可以直接創建一個wpf服務會生成基本內容,服務分成兩個,一個去做接口,一個去實現接口:
接口類:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Web; namespace WCFExample.WCF { //需要引用類庫 System.ServiceModel 和 System.ServiceModel.Web [ServiceContract] public interface IService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "GetCon?op={name}", ResponseFormat = WebMessageFormat.Json)] string GetCon(string name); } }
實現方法類:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFExample.WCF { /// <summary> /// 用ServiceBehavior為契約實現類標定行為屬性,此處指定並發模型為ConcurrencyMode.Multiple,即並發訪問 /// </summary> [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class UserService : IService { public string GetCon(string name) { return name; } } }
啟動WCF類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Threading; namespace WCFExample.WCF { public class WCFService { public static void Begion() { //無配置文件App.config的情況下手動綁定 //Uri HttpUri = new Uri("Http://localhost:21467/wcf"); //Type Servicetype = typeof(WCFExample.WCF.UserService); //using (ServiceHost Chost=new ServiceHost (Servicetype,new Uri[]{HttpUri})) //{ // Binding basicHttpBinding = new BasicHttpBinding(); // string address = ""; //Chost.AddServiceEndpoint(typeof(WCFExample.WCF.UserService), basicHttpBinding, address); // Chost.Open(); // Console.WriteLine("Service Running..."); // Console.ReadKey(true); // Chost.Close(); //} //定義一個ServiceHost,注意參數中要使用契約實現類而不是接口 ServiceHost host = new ServiceHost(typeof(WCFExample.WCF.UserService)); host.Open(); while (true) Thread.Sleep(1000); } } }
服務啟動后,即可正常使用WCF服務
