WCF中netTcpBinding使用

1.定義接口IFly

复制代码
namespace TECO
{
[ServiceContract]
public interface IFly
{
[OperationContract]
string Fly(string name);
}
}
复制代码

2.定義服務

复制代码
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,啟動服務

复制代码
   public class Program {
static void Main(string[] args) {

using (ServiceHost host = new ServiceHost(typeof(People)))
{
host.Open();
Console.WriteLine("服務已經啟動");
Console.Read();
}

}
}
复制代码

4.配置app.config 文件

代碼如下:

复制代码
<?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.客戶端使用代碼調用,代碼如下:

复制代码
  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菜單上,點右鍵,選擇"以管理員運行",就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VS2022是微软推出的最新版本的集成开发环境(IDE),它支持使用WCF(Windows Communication Foundation)进行开发。 WCF是一种微软技术,用于构建分布式的应用程序。它提供了一种统一的编程模型,可用于构建基于服务的架构(SOA)。开发人员可以使用WCF创建面向服务的应用程序,这些应用程序可以在不同的平台和技术之间进行通信。 在VS2022使用WCF,开发人员可以轻松地创建、开发和测试WCF服务。VS2022提供了与WCF相关的工具和功能,可以帮助开发人员快速部署和管理WCF服务。 VS2022支持使用.NET Framework进行WCF开发,同时还提供了最新版本的.NET框架(.NET 6),开发人员可以选择使用适合自己需求的版本。此外,VS2022还提供了许多模板和示例代码,使得开发人员可以更快速地创建和部署WCF服务。 使用VS2022进行WCF开发,可以获得更好的集成性和开发体验。开发人员可以使用VS2022的强大调试和测试功能,进行快速迭代和调试。同时,VS2022还提供了丰富的开发工具和插件,可以帮助开发人员提高开发效率和代码质量。 总而言之,VS2022作为最新版本的集成开发环境,提供了对WCF的全面支持。开发人员可以借助VS2022的工具和功能,更轻松地创建、开发和测试WCF服务。无论是.NET Framework还是.NET 6,VS2022都是一个强大的开发环境,可以满足开发人员的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值