WCF面向服务应用程序系列之十:绑定-标准绑定(HTTP/TCP)
上一章我们介绍了WCF绑定的自定义绑定,这一章我们通过一个DEMO来介绍如何创建一个基于HTTP或者TCP协议的程序。在绑定时,我们要指定WCF的地址:要为不在IIS里承接的服务指定基地址;要为所有相让服务支持的传输协议指定基地址,要指定相对于基地址的终结点地址。不要为终结点指定绝对地址,这样做的优势是:如果修改绑定使用一个不同的传输协议,修改简单的修改在配置中指定的绑定地址,而不在需要修改终结点地址。
开发环境:Visual Studio 2010 + Net Framework 4.0 。
1、创建一个WCF Service,主要代码如下:
public interface IMediaStreaming
{
[OperationContract]
Stream GetMediaStream(string mediaPath);
}
{
public Stream GetMediaStream(string mediaPath)
{
FileInfo fileInfo = new FileInfo(mediaPath);
if (!fileInfo.Exists)
{
throw new FileNotFoundException("File does not exist:{0}", mediaPath);
}
FileStream fileStream = null;
try
{
fileStream = new FileStream(mediaPath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch
{
if (fileStream != null)
{
fileStream.Close();
fileStream.Dispose();
fileStream = null;
}
}
return fileStream;
}
}
在实现类中,我们打开一个文件,并返回文件流到客户端。
2、右键点击App.config文件,选中Edit WCF Configuration进行编辑,我们添加2个baseAddress,一个是基于HTTP协议的;一个是基于TCP协议的。同时添加2个bindings项,一个是basicHttpBinding;一个是netTcpBinding。同时添加2个endpoint,一个是指向basicHttpBinding,一个是指向netTcpBinding。主要配置如下:
baseAddress:
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/StreamingTransfersServer/MediaStreaming/" />
<add baseAddress="net.tcp://localhost:9001"/>
</baseAddresses>
</host>
bindings:
<basicHttpBinding>
<binding name="basicHttpStreaming" maxBufferSize="40000000" maxReceivedMessageSize="40000000"
messageEncoding="Mtom" transferMode="Streamed" />
</basicHttpBinding>
<netTcpBinding>
<binding name="netTcpStreaming" transferMode="Streamed" maxBufferSize="40000000"
maxReceivedMessageSize="40000000" />
</netTcpBinding>
</bindings>
endpoint:
<!--<endpoint binding="netTcpBinding" bindingConfiguration="netTcpStreaming" contract="StreamingTransfersServer.IMediaStreaming" />-->
从endpoint配置来看,我们注释了netTcpBinding。
3、添加客户端程序,添加引用,主要代码如下:
{
Console.WriteLine("waiting...");
using (SoundPlayer soundPlayer = new SoundPlayer())
{
using (StreamingTransfersServer.MediaStreamingContractClient client = new StreamingTransfersServer.MediaStreamingContractClient())
{
using (Stream serverStream = client.GetMediaStream(@"D:\1.wav"))
{
soundPlayer.Stream = serverStream;
Console.WriteLine("playing");
soundPlayer.Play();
Console.WriteLine("played");
}
}
}
Console.ReadLine();
}
我们从客户端传入一个wav的文件路径,服务端根据这个路径获取到此文件流,并返回给客户端。客户端根据此文件流,播放文件。
在添加引用时,Visual Studio帮助我们生成了客户端的配置,代码如下:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_MediaStreamingContract" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="40000000" maxBufferPoolSize="524288" maxReceivedMessageSize="40000000"
messageEncoding="Mtom" 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:8732/Design_Time_Addresses/StreamingTransfersServer/MediaStreaming/basicHttp"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MediaStreamingContract"
contract="StreamingTransfersServer.MediaStreamingContract"
name="BasicHttpBinding_MediaStreamingContract" />
</client>
</system.serviceModel>
</configuration>
我们可以看到,此时客户端与服务端通信使用的是basicHttpBinding,然后,我们修改服务端的配置,把endpoint从basicHttpBinding改为netTcpBinding,代码如下:
<endpoint binding="netTcpBinding" bindingConfiguration="netTcpStreaming" contract="StreamingTransfersServer.IMediaStreaming" />
重新编译服务端,并对客户端的引用进行更新,此时,打开客户端app.config文件,我们可以看到客户端的配置已经由basicHttpBinding变为netTcpBinding,相应的配置信息与服务端是一致的,代码如下:
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_MediaStreamingContract" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="40000000" maxConnections="10"
maxReceivedMessageSize="40000000">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:9001/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_MediaStreamingContract"
contract="StreamingTransfersServer.MediaStreamingContract"
name="NetTcpBinding_MediaStreamingContract">
<identity>
<userPrincipalName value="CS\Administrator" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
无论是basicHttpBinding还是netTcpBinding,运行代码后,都能够听到wav文件的播放声音。至此标准绑定(HTTP/TCP)介绍完毕,其它基于MSMQ或IPC的绑定我们以后专门介绍。
点击下载DEMO。
出处:http://xinhaijulan.cnblogs.com
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
转载于:https://www.cnblogs.com/xinhaijulan/archive/2010/10/24/1859700.html
WCF面向服务应用程序系列之十:绑定-标准绑定(HTTP/TCP)相关推荐
- WCF面向服务应用程序系列之一:Hello WCF
WCF全称为Windows Communication Foundation,是Microsoft平台上的SOA架构,用于构建分布式和可交互操作的应用程序.它统一ASMX, .NET Remoting ...
- 【培训稿件】构建WCF面向服务的应用程序(包含ppt,源代码)
今天在整理电脑的时候,从目录中搜出了之前公司的培训演讲稿件,这个是我第一次在部门里面做内部培训,当时反映还不错.于是整理下这份稿件.大家如果有兴趣可以下载看看. 构建WCF面向服务的应用程序(ppt, ...
- WCF 面向服务的4个原则
边界清晰 在面向服务里,服务可以与每个其它的服务通过消息交互.换句话说,服务可以穿越边界发送消息给其它服务.服务可以发送和接收消息,并且能被发送和接受的消息形状定义了服务的边界.这些边界被良好地定义, ...
- AJAX应用服务器端:面向服务与WCF
WCF作为AJAX服务平台的优势 WCF为启用AJAX的Web服务提供管道形式服务,并未服务器与客户端之间的通信提供了统一的编程模式.WCF运行环境非常灵活,它包含了能 ...
- [.NET领域驱动设计实战系列]专题二:结合领域驱动设计的面向服务架构来搭建网上书店...
原文:[.NET领域驱动设计实战系列]专题二:结合领域驱动设计的面向服务架构来搭建网上书店 一.前言 在前面专题一中,我已经介绍了我写这系列文章的初衷了.由于dax.net中的DDD框架和Bytear ...
- WCF学习之旅—WCF服务的WAS寄宿(十二)
上接 WCF学习之旅-WCF服务部署到IIS7.5(九) WCF学习之旅-WCF服务部署到应用程序(十) WCF学习之旅-WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...
- 我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承...
当今的IT领域,SOA已经成为了一个非常时髦的词,对SOA风靡的程度已经让很多人对SOA,对面向服务产生误解.其中很大一部分人甚至认为面向服务将是面向对象的终结,现在的面向对象将会被面向服务完全代替. ...
- Sharepoin学习笔记—架构系列—Sharepoint服务(Services)与服务应用程序框架(Service Application Framework) 1
Sharepoin学习笔记-架构系列-Sharepoint服务(Services)与服务应用程序框架(Service Application Framework) 1 Sharepoint服务是Sha ...
- 一起谈.NET技术,使用WCF实现SOA面向服务编程—— 架构设计
SOA本身就是一种面向企业级服务的系统架构,简单来说,SOA就是一种进行系统开发的新的体系架构,在基于SOA架构的系统中,具体应用程序的功能是由 一些松耦合并且具有统一接口定义方式的组件(也就是ser ...
最新文章
- Python炫技操作:模块重载的五种方法
- skylake服务器处理器M系列,行业——华为三大v5机架式服务器改用Skylake处理器
- spring boot controller构造方法_面试前突击Spring,我只需要十分钟,那么你呢?
- 几种字符串到byte[] 数组转化为String 的方法
- 日常开发中的几个常用跨域处理方式
- 苹果 macOS Monterey 12 正式发布
- 跟“网址”有关的$_SERVER[ ]变量
- C语言 - 常见面试题整理
- label怎么换行 vb_VB中怎么换行 | 学步园
- 作为产品经理,懂接口是必须的
- banner设圆角_如何规范运营Banner设计?
- 东方财富股票接口解析优缺点
- matlab中contourf函数怎么用_有人可以解释如何使用contourf在MATLAB中绘制这个总和吗?...
- Java回炉之排序算法
- PowerBI-时间智能函数-DATEADD
- |app自动化测试之Appium问题分析及定位
- 三极管---初识(图文并茂)
- Git版本工具系列之一:Git常用命令
- 第三次自考第一阶段总结
- Golang 023. 《孙子算经》之鸡兔同笼