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);