標籤:
做代碼統計,方便以後使用:
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服務
C# 建立一個WCF服務