標籤:
WCF服務可以承載與iis、winform、console、window服務中,下面重點介紹以console為載體,對外提供服務(服務滿足web訪問以及soap方式訪問)
1.服務類的實現
wcf服務類一般有兩種實現方式,下面分別對兩種方式進行介紹:
1.1 使用介面進行實現
1 namespace Example 2 { 3 [ServiceContract] 4 public interface IService 5 { 6 7 [OperationContract] 8 string GetData(int value); 9 // TODO: 在此添加您的服務作業10 }11 }12 13 namespace Example14 {15 16 public class Service1 : IService17 {18 public string GetData(int value)19 {20 return string.Format("You entered: {0}", value);21 }22 }23 }View Code
1.2 直接在類上面進行實現
1 namespace Example 2 { 3 [ServiceContract] 4 public class Service1 5 { 6 [OperationContract] 7 public string GetData(int value) 8 { 9 return string.Format("You entered: {0}", value);10 }11 }12 }View Code
當我們需要同時支援web方式訪問的時候,我們只要給類添加一些特性即可進行功能的拓展
可以給類添加[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
可以給方法添加[WebInvoke()]或者[WebGet()]特性
注意:[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]只能添加在類上面 而[WebInvoke()]和[WebGet()]可以添加在介面中,也可以添加在類中
2. Config的配置
與服務相關的所有的配置資訊全部在system.serviceModel節點中,主要有bindings、behaviors、services需要進行配置,下面主要說一下behaviors和services的配置
1 <behaviors> 2 <serviceBehaviors> 3 <behavior name=""> 4 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 5 <serviceDebug includeExceptionDetailInFaults="false" /> 6 </behavior> 7 </serviceBehaviors> 8 <endpointBehaviors> 9 <behavior name="Example.Service">10 <webHttp />11 </behavior>12 </endpointBehaviors>13 </behaviors>14 <services>15 <service name="template.Example">16 <endpoint address="" behaviorConfiguration="Example.Service" binding="webHttpBinding" contract="Example.Service" />17 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />18 <endpoint address="Soap" binding="basicHttpBinding" contract="Example.Service" />19 <host>20 <baseAddresses>21 <add baseAddress="http://localhost:8733/template/Example/" />22 </baseAddresses>23 </host>24 </service>25 </services>
View Code
其中service節點將example作為服務發布出去host描述了當前的服務發布的基礎地址後面的調用或restful調用都是相對改地址的 endpoint有三個,
1.名字為空白的表示將服務發布,並可以供web調用,其中的關鍵是binding="webHttpBinding", web調用時,調用的方式為:基礎地址"http://localhost:8733/template/Example/"+address(“”)+函數名(當定義了uritemplate時,為uritemplate的值)
2.名字為“mex”的表示將服務的Metadata發布出去,並且可以訪問,當去掉該endpoint是,無法擷取中繼資料資訊
3.名字為“soap”的表示將服務作為soap服務發布,供soap進行訪問,去掉後無法進行soap訪問。訪問的路徑為:基礎地址+“soap”
比較1和3的endpoint可以發現主要的區別在與binding的值,當要進行一些其它的功能是,進行功能配置後,為配置取名字,將名字付給需要的屬性
3. 跨域訪問
說到底soap協議是基於http協議的,當存在訪問安全問題時,可以使用常用的web開發時跨域訪問的問題進行解決。我們在請求的response頭中添加“Access-Control-Allow-Origin”即可解決某些情況下web跨域訪問的安全問題。
通過WebOperationContext.Current我們可以擷取到當前請求的上下文,在當前請求的上下文中的response對象的頭部添加“Access-Control-Allow-Origin”
代碼如下:
1 WebOperationContext ctx = WebOperationContext.Current;2 ctx.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");View Code
既然擷取了當前請求的內容物件,需要做一些其它事情的也可以幹自己想乾的了,並不僅僅局限於跨域存取控制了
4. ServiceHost
我們可以通過ServiceHost對象將服務承載於一個console application 或winform application中,具體的代碼如下:
static void Main(string[] args){ ServiceHost host = new ServiceHost(typeof(CaAgent)); host.Open(); Console.WriteLine("資料服務開啟"); while(Console.ReadLine()!="quit"){ } host.Close();}
下面附上在github上面的example的地址:WCF_Ajax_CrossDomain
在控制台中承載WCF服務,並提供Ajax訪問和SOAP訪問,以及跨域訪問