心海巨澜

天行键,君子以自强不息;地势坤,君子以厚德载物!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

      上一章我们介绍了WCF绑定的自定义绑定,这一章我们通过一个DEMO来介绍如何创建一个基于HTTP或者TCP协议的程序。在绑定时,我们要指定WCF的地址:要为不在IIS里承接的服务指定基地址;要为所有相让服务支持的传输协议指定基地址,要指定相对于基地址的终结点地址。不要为终结点指定绝对地址,这样做的优势是:如果修改绑定使用一个不同的传输协议,修改简单的修改在配置中指定的绑定地址,而不在需要修改终结点地址。

      开发环境:Visual Studio 2010 + Net Framework 4.0 。

      1、创建一个WCF Service,主要代码如下:

[ServiceContract(Name = "MediaStreamingContract", Namespace = "http://schemas.xinhaijulan.com/demos/StreamingTransfers")]
public interface IMediaStreaming
{
[OperationContract]
Stream GetMediaStream(
string mediaPath);
}

 

public class MediaStreaming : IMediaStreaming
{
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:

<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/StreamingTransfersServer/MediaStreaming/" />
<add baseAddress="net.tcp://localhost:9001"/>
</baseAddresses>
</host>

 bindings:

<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 address="basicHttp" binding="basicHttpBinding" bindingConfiguration="basicHttpStreaming" contract="StreamingTransfersServer.IMediaStreaming" />
<!--<endpoint binding="netTcpBinding" bindingConfiguration="netTcpStreaming" contract="StreamingTransfersServer.IMediaStreaming" />-->

      从endpoint配置来看,我们注释了netTcpBinding。

      3、添加客户端程序,添加引用,主要代码如下:

static void Main(string[] args)
{
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帮助我们生成了客户端的配置,代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<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 address="basicHttp" binding="basicHttpBinding" bindingConfiguration="basicHttpStreaming" contract="StreamingTransfersServer.IMediaStreaming" />-->
<endpoint binding="netTcpBinding" bindingConfiguration="netTcpStreaming" contract="StreamingTransfersServer.IMediaStreaming" />

      重新编译服务端,并对客户端的引用进行更新,此时,打开客户端app.config文件,我们可以看到客户端的配置已经由basicHttpBinding变为netTcpBinding,相应的配置信息与服务端是一致的,代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<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
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

posted on 2010-10-24 12:44  心海巨澜  阅读(4247)  评论(2编辑  收藏  举报