1.定義接口IFly
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
namespace TECO
{
[ServiceContract]
public interface IFly
{
[OperationContract]
string Fly(string name);
}
}
2.定義服務
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
namespace TECO
{
public class People:IFly
{
#region IFly 成員
public string Fly(string name)
{
return string.Format("{0}:會飛",name);
}
#endregion
}
}
其中:<service behaviorConfiguration="MessageBehavior" name="TECO.People">
中的name為實際的操作類。
3.定義宿主 host,啟動服務
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class Program {
static void Main(string[] args) {
using (ServiceHost host = new ServiceHost(typeof(People)))
{
host.Open();
Console.WriteLine("服務已經啟動");
Console.Read();
}
}
}
4.配置app.config 文件
代碼如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MessageBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MessageBehavior" name="TECO.People">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="Binding1"
contract="TECO.IFly" />
<endpoint address="net.tcp://localhost:4503/People/mex" binding="mexTcpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:1234/People" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="Binding1"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
transactionFlow="false"
transferMode="Buffered"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
listenBacklog="10"
maxBufferPoolSize="524288"
maxBufferSize="65536"
maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<reliableSession ordered="true"
inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
5.客戶端使用代碼調用,代碼如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class Program
{
static void Main(string[] args)
{
//使用代理類調用
ServiceReference1.FlyClient clent = new ServiceReference1.FlyClient();
Console.WriteLine(clent.Fly("zzy"));
//直接代碼調用
NetTcpBinding myBinding = new NetTcpBinding(SecurityMode.None);
EndpointAddress myEndpoint = new EndpointAddress("net.tcp://localhost:1234/People");
ChannelFactory<IFly> myChannelFactory = new ChannelFactory<IFly>(myBinding, myEndpoint);
// Create a channel.
IFly wcfClient1 = myChannelFactory.CreateChannel();
string s = wcfClient1.Fly("gsw");
Console.WriteLine(s.ToString());
((IClientChannel)wcfClient1).Close();
myChannelFactory.Close();
Console.Read();
}
}
需要說明: NetTcpBinding myBinding = new NetTcpBinding(SecurityMode.None);定義綁定類型
EndpointAddress myEndpoint = new EndpointAddress("net.tcp://localhost:1234/People");定義url,提供服務地址
由於 客戶端沒有IFly接口,因此,需要在客戶端定義一個接口,接口要與服務器端的接口一樣。
6.使用代理類調用服務:
6.1 添加引用,如果添加引用,服務器配置文件需要做如下處理:
加入另一個總結點:
<endpoint address="net.tcp://localhost:4503/People/mex" binding="mexTcpBinding"
contract="IMetadataExchange" />
為了使用此終結點,需要定義如下:
<behaviors>
<serviceBehaviors>
<behavior name="MessageBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
其中:<serviceMetadata httpGetEnabled="false"/> 表示元數據發佈,只有元數據發佈后,才能在客戶端,添加引用。
點擊添加引用:輸入路徑為net.tcp://localhost:4503/People/mex,此時需要先啟動服務。如果服務啟動了,那麼可以看到服務的內容。
點確定會生成客戶端代理。我這裡是:ServiceReference1
6.2 在客戶端使用代理類操作
代碼如下:
//使用代理類調用
ServiceReference1.FlyClient clent = new ServiceReference1.FlyClient();
Console.WriteLine(clent.Fly("zzy"));
7.題外話:
在win7下面,運行vs調試環境的wcf服務,需要以管理員身份打開vs。在vs菜單上,點右鍵,選擇"以管理員運行",就可以了。