C# WCF之用接口創建服務契約、部署及客戶端連接


服務契約描述了暴露給外部的類型(接口或類)、服務所支持的操作、使用的消息交換模式和消息的格式。每個WCF服務必須實現至少一個服務契約。使用服務契約必須要引用命名空間System.ServiceModel 。
 
ServiceContractAttribute
OperationContractAttribute
 
1,先創建一個類庫為ClassLibrary1,在創建一個接口interface1。
2,添加引用和命名空間,System.ServiceModel
3,在接口里代碼如下:
namespace ClassLibrary1
{    //服務契約
    [ServiceContract]
   public interface Interface1
    { //操作契約
        [OperationContract]
        string Hello();
    }
}
4,創建一個窗體或控制台程序並創建一個類HelloClass.cs
5,添加引用(項目里面)ClassLibrary1
6,HelloClass1.cs里代碼如下

class HelloClass:ClassLibrary1.Interface1
    {

        public string Hello()
        {
          return   "Hello wcf!";  

}
    }

基本創建一個服務。創建之后需要部署。一般分為配置文件部署和代碼部署。

一,配置文件部署
服務的三要素

A:Address 意味着在哪里(也含有傳輸方式信息)

B:Binding 意味着怎么做(與地址的傳輸方式要匹配)

C:Contract意味着做什么(服務契約)

 

<system.ServiceModel>

<services>

<service>

<endpoint/>     /*服務和終結點*/

</service>

</services>

 

<bindings>     /*綁定(可選)*/

<binding>

</binding>

</bindings>

 

<behaviors>    /*行為(可選) */

<behavior>

</behavior>

</behaviors>

</system.ServiceModel>

終結點的地址由EndpointAddress 類表示,該類包含一個表示服務地址的統一資源定位符(URI),大多數傳輸的地址URI 包含四個部分。
例如,

“http://www.sina.com.cn:3200/mathservice”這個URI 具有以下四個部分:

– 方案:http:

– 計算機:www.sina.com.cn

– (可選)端口:3200

– 路徑:/mathservice

在上面例題上繼續完善,打開app.config文件
<system.serviceModel>
    <services>
      <service name="ConsoleApplication1.HelloClass" behaviorConfiguration="testBehavior"> <!--name為實現該契約的類-->
        <host>
          <baseAddresses>
            <add baseAddress=" http://localhost:8002/test"></add><!--基地址-->
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="ClassLibrary1.Interface1"></endpoint>
          <!--已有baseAddress基地址,address可為空;binding為綁定類型,對應Http協議;contract為所公開的協議,即所創建的服務契約接口-->
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="testBehavior"> <!--與上面behaviorConfiguration="testBehavior"保持一致,可為空-->
          <serviceMetadata httpGetEnabled="true"/> <!--指定是否要發布元數據以HTTP/Get獲取-->
        </behavior>
      </serviceBehaviors>
    </behaviors>
    </system.serviceModel> 
之后需要啟動服務
在ConsoleApplication1的program.cs里添加應用和命名空間System.ServiceModel和以下代碼

           ServiceHost host = null;

           host = new ServiceHost(typeof(ConsoleApplication1.HelloClass));
            host.Open();
            Console.WriteLine("服務已經啟動!");
            Console.ReadLine();

運行代碼后,將 http://localhost:8002/test在瀏覽器打開將會看到相關服務信息。
 
二,代碼部署
先創建一個控制台程序 ConsoleApplication2,添加一個類HelloClass.cs和引用及命名空間,服務接口
在program.cs 添加命名空間
            ServiceHost host = null;
            host = new ServiceHost(typeof(ConsoleApplication2.HelloClass));
            NetTcpBinding tcpBind = new NetTcpBinding();//設定綁定類型
            string address = "net.tcp://localhost:3200/hello";
            host.AddServiceEndpoint(typeof(ClassLibrary1.Interface1), tcpBind, address);//在服務終結點添加,協議,綁定類型,終結點地址
            host.Opened += delegate { Console.WriteLine("服務已啟動!"); Console.ReadLine(); };
            host.Open();
當服務部署成功后,客戶端可以連接服務並調用方法,一般有兩種方式
一,如上一節講的直接添加服務引用來實現
二,純代碼實現
先創建一個客戶端,client控制台程序以及相關引用、命名空間、服務接口引用

           //綁定形式
            NetTcpBinding bind = new NetTcpBinding();
            //提供客服端與服務建立連接的地址
            EndpointAddress address = new EndpointAddress("net.tcp://localhost:3200/hello");
            //客戶端通關通道工廠將消息發送到不同配置的服務終結點
            ChannelFactory<ClassLibrary1.Interface1> factory = new ChannelFactory<ClassLibrary1.Interface1>(bind, address);
            //通過通道工廠對象來獲取指定類型
            ClassLibrary1.Interface1 myobject = factory.CreateChannel();
            string s = myobject.Hello();
            Console.WriteLine(s);
            Console.ReadLine();

先啟動服務,在運行客戶端。

 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM