心海巨澜

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

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  34 随笔 :: 1 文章 :: 148 评论 :: 18万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

      上一章我们介绍了WCF绑定的基本知识,这一章我们通过一个DEMO来介绍如何创建一个自定义绑定(CustomBinding)的程序。从本章起,我们假定读者对WCF一有定的了解,DEMO创建的过程介绍将省略一些简单的步骤。自定义绑定(CustomBinding)与前面介绍的WCF程序的区别主要在于通过修改配置文件来达到创建自定义绑定(CustomBinding)的目的。

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

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

复制代码
接口代码
[ServiceContract(Namespace = "http://schemas.xinhaijulan.com/demos/CustomBinding")]
public interface IHelloWCFService
{
[OperationContract]
string HelloWCF(string inputMsg);
}
复制代码

 

复制代码
实现类代码
public class HelloWCFService:IHelloWCFService
{
public string HelloWCF(string inputMsg)
{
return "The message returned from server:" + inputMsg;
}
}
复制代码

2、修改App.config文件:

      对于WCF Service项目来说我们可以通过配置工具来进行修改App.config。

      2.1、右键点击App.config,选中Edit WCF Configuration,如下图:

      2.2、添加自定义绑定项,如下图:

      2.3、修改EndPoint,采用customBinding的方式,修改Binding和BindingConfiguration,如下图:

      2.4、关闭配置工具,保存,自定义绑定的配置文件修改完毕。

      3、添加客户端项目,并添加对CustomBindingServer的引用,主要代码如下:

复制代码
客户端代码
static void Main(string[] args)
{
using (CustomBindingServer.HelloWCFServiceClient client = new CustomBindingServer.HelloWCFServiceClient())
{
Console.WriteLine(
"Please input some keys:");
string inputMsg = Console.ReadLine();
Console.WriteLine(
"The sever will return some message below:");
Console.WriteLine(client.HelloWCF(inputMsg));
}

Console.ReadLine();
}
复制代码

      4、此时我们可以打开客户端的app.config文件,可以看到在此文件中自动生成的配置方式为我们服务端配置的customBinding方式,与服务端是一致的,如下:

复制代码
客户端生成的配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IHelloWCFService">
<reliableSession acknowledgementInterval="00:00:00.2000000" flowControlEnabled="true"
inactivityTimeout
="00:10:00" maxPendingChannels="4" maxRetryCount="8"
maxTransferWindowSize
="8" ordered="true" reliableMessagingVersion="Default" />
<binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
maxSessionSize
="2048">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead
="4096" maxNameTableCharCount="16384" />
</binaryMessageEncoding>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288"
maxReceivedMessageSize
="65536" allowCookies="false" authenticationScheme="Anonymous"
bypassProxyOnLocal
="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
keepAliveEnabled
="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
realm
="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
useDefaultWebProxy
="true" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/CustomBindingServer/HelloWCFService/"
binding
="customBinding" bindingConfiguration="CustomBinding_IHelloWCFService"
contract
="CustomBindingServer.IHelloWCFService" name="CustomBinding_IHelloWCFService" />
</client>
</system.serviceModel>
</configuration>
复制代码

      5、设置客户端为启动项,F5运行,我们将看到此DEMO正常运行,输出如下:

Please input some keys:
You are a good boy
!
The sever will
return some message below:
The message returned from server:You are a good boy
!

     

      至此,自定义绑定(CustomBinding)介绍完毕。

      点击下载DEMO

 

作者:心海巨澜
出处:http:
//xinhaijulan.cnblogs.com
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on   心海巨澜  阅读(5123)  评论(10编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示