WindowForm+WCF的簡單開發流程

參考:http://www.cnblogs.com/huangxincheng/p/4558747.html  十五天精通WCF——第一天 三種Binding讓你KO80%的業務html

理論:WCF的通信方式基於HTTP協議,傳輸消息爲soapide

 

一、接口spa

第一步須要定義一個接口code

並不知道不定義該接口是否可行,接口既然是做爲一種行爲規範,因此不定義應該也是可行的orm

注意:必定要引入 System.ServiceModel;xml

 1   using System.Runtime.Serialization;
 2   using System.ServiceModel;
 3   
 4   namespace MyService
 5   {
 6       [ServiceContract] //只是接口或類在應用程序中定義的服務協議 ——能夠理解爲該標記將類或接口設置爲服務協議
 7       public interface IHomeService
 8       {
 9           [OperationContract] //指定方法定義一個操做,該操做是該協議的一部分 ——該標記將該方法定義爲協議中必須實現的
10          int GetLength(string name);
11      }
12  }

 

二、實現類htm

有接口就必定會有實現該接口的類blog

注意:引用 System.Messaging; System.Threading;dns

 1 using System;
 2 using System.Messaging;
 3 using System.Threading;
 4 
 5 namespace MyService
 6 {
 7     public class HomeService : IHomeService
 8     {
 9         public int GetLength(string name)
10         {
11             return name.Length;
12         }
13     }
14 }

三、啓動服務接口

到這一步,WCF能夠說基本配置成型,但還不能被外部程序調用

目前你能夠在WCF中添加一個Form.cs調用以前實現的方法,也可按找參考文章中的作法

具體看需求

 

四、配置config文件

完成這一步,WCF將能夠被外部程序調用

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="IHomeServiceBinding" />
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="WCF連接地址" />  <!--設置能夠被遠程調用-->
          <serviceDebug includeExceptionDetailInFaults="true" />  
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service name="MyService.HomeService">
        <endpoint address="http://127.0.0.1:1920/HomeService" binding="basicHttpBinding" contract="MyService.IHomeService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://127.0.0.1:1920"/>
          </baseAddresses>
        </host>
      </service>
    </services>

  </system.serviceModel>
</configuration>

五、經過WindowForm添加服務引用調用WCF

VS中與添加Web Service操做相同。添加成功後將會在App.config中自動配置節點。

相關文章
相關標籤/搜索