WCF執行個體應用,wcf執行個體

來源:互聯網
上載者:User

WCF執行個體應用,wcf執行個體
        通過上篇文章對WCF做了一個簡短的介紹,下面就做一個小例子來熟悉如何構建和應用一個WCF應用。        1.添加WCF服務:            2.開啟剛才建立的服務介面IShowMyName,在其中添加方法(注意不要將服務標籤丟掉,否則訪問不到):
                
        3.添加實現服務的方法:
                
        4.在瀏覽器中查看如所示表示成功:
                
        5.在IIS中對其進行發布,這樣服務端就配置好了。
        6.用戶端的配置,首先要進行設定檔的添加:            (1) 配置好web.config

<configuration>

    <system.web>

        <compilation debug="true" targetFramework="4.0" />

    </system.web>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name="">

          <serviceMetadata httpGetEnabled="true" />

          <serviceDebug includeExceptionDetailInFaults="false" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <serviceHostingEnvironment  multipleSiteBindingsEnabled="true" />

    <bindings>

      <basicHttpBinding>

        <binding name="BasicHttpBinding_Default"

          closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"

          sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false"

          hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"

          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"

          textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

            maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <security mode="None">

            <transport clientCredentialType="None" proxyCredentialType="None"

              realm="" />

            <message clientCredentialType="UserName" algorithmSuite="Default" />

          </security>

        </binding>

      </basicHttpBinding>

    </bindings>

    <client>

<endpoint

address="http://localhost:8080/WCFService/ShowMyName.svc?wsdl"

binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Default"

        contract="Contracts.IShowMyName"

        name="BasicHttpBinding_IShowMyName" />

    </client>

  </system.serviceModel>

</configuration>

 (2) 用戶端調用服務

在用戶端中添加引用:

        using System.ServiceModel.Channels;

         using System.ServiceModel;         後就可以添加代碼,調用WCF的服務了:
 //因為服務是通過通道處理站建立的,故在此需要先執行個體化通道處理站

ChannelFactory<IShowMyName> factory = new ChannelFactory<IShowMyName>("BasicHttpBinding_IShowMyName");

        IShowMyName myobject = factory.CreateChannel();

//調用服務的方法

        string time = myobject.showName(this.txtName.Text);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.