1. 程式人生 > >【5】.net WCF 簡單例項

【5】.net WCF 簡單例項

1.建立WCF專案

2.系統自動生成IWcfService

    // 注意: 使用“重構”選單上的“重新命名”命令,可以同時更改程式碼和配置檔案中的介面名“IService1”。
    [ServiceContract]
    public interface IWcfService
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: 在此新增您的服務操作
    }


    // 使用下面示例中說明的資料約定將複合型別新增到服務操作。
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
(1)服務契約:ServiceContract(服務)和OperationContract  (方法)

(2)資料契約:DataContract(類)和DataMember(屬性) 用於類和結構上

(3)訊息契約:MessageContract 用於soap訊息

3.WCF服務類

    public class WcfService : IWcfService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }

4.服務配置檔案

  <system.serviceModel>
    <!--配置繫結節點Start-->
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding0" maxReceivedMessageSize="2147483647">
          <readerQuotas maxStringContentLength="2147483647"/>
          <security mode="None" />
        </binding>
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="netTcpBinding0" maxReceivedMessageSize="2147483647">
          <readerQuotas maxStringContentLength="2147483647"/>
          <security mode="None" />
        </binding>
      </netTcpBinding>
      <wsHttpBinding></wsHttpBinding>
    </bindings>
    <!--配置繫結節點End-->
    
    <!--配置服務節點Start-->
    <services>
      <!--配置某一服務,在這裡可以指定服務名稱-->
      <service name="WcfServiceTest.WcfService">
        <endpoint address="aaa" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding0"
          name="BasicHttpBinding_WcfService" contract="WcfServiceTest.IWcfService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBinding0"
          name="NetTcpBinding_WcfService" contract="WcfServiceTest.IWcfService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>
    <!--配置服務節點End-->

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 為避免洩漏元資料資訊,請在部署前將以下值設定為 false -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- 要接收故障異常詳細資訊以進行除錯,請將以下值設定為 true。在部署前設定為 false 以避免洩漏異常資訊 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

5.iis部署WCF服務


6.新增客戶端專案並新增服務引用

7.Main程式中新增wcf服務並呼叫方法

    class Program
    {
        static void Main(string[] args)
        {
            var client = new WcfService.WcfServiceClient();
            try
            {
                var str = client.GetData(2046);
                Console.WriteLine(string.Format("內容:{0}", str));
                client.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("出現異常!");
                client.Abort();
            }
            Console.ReadLine();
        }
    }

8.客戶端配置檔案

    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_WcfService" />
            </basicHttpBinding>
            <netTcpBinding>
                <binding name="NetTcpBinding_WcfService">
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <!--<endpoint address="http://localhost/WcfServiceTest/WcfService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"
                contract="WcfService.IWcfService" name="BasicHttpBinding_WcfService" />-->
            <endpoint address="net.tcp://localhost/WcfServiceTest/WcfService.svc"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_WcfService"
                contract="WcfService.IWcfService" name="NetTcpBinding_WcfService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>