这个通信方式本人实验了好久,需要一个重要的条件是服务端和客户端的发送内容方式都是相同的声明,需要在配置文件写入,客户端:

 <system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="" maxReceivedMessageSize="" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength="" maxBytesPerRead="" maxNameTableCharCount="" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" />
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>

客户端代码:

  try
{
var myBinding = new WSHttpBinding("wsHttpBinding");//根据后台的binding名字选中后台的binding
var myEndpoint =
new EndpointAddress(
new Uri("http://localhost:12857/UserService.svc"));
var myChannelFactory = new ChannelFactory<IUserBussiness>(myBinding, myEndpoint);
IUserBussiness client = myChannelFactory.CreateChannel();
var res = client.DoWork("");
myChannelFactory.Close();
}
catch (Exception ex)
{
//do something proper here
}

服务端的配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WCFTestImp.UserBussiness" behaviorConfiguration="metadataBehavior">
<!--这里指定要绑定的binding,contract指定的是WCF的接口-->
<endpoint bindingConfiguration="wsHttpBindings" address="" name="WCFTestImp.UserBussiness" binding="wsHttpBinding" contract="WCFTestImp.IUserBussiness" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<!--为避免泄漏元数据信息,请在部署前将以下值设置为 false-->
<serviceMetadata httpGetEnabled="true"/>
<!--要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceSecurityAudit auditLogLocation="Default" suppressAuditFailure="True" serviceAuthorizationAuditLevel="SuccessOrFailure" messageAuthenticationAuditLevel="Success" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" /> <bindings>
<!--这里声明服务端的binding也是wsHttpBinding的方式-->
<wsHttpBinding>
<binding name="wsHttpBindings" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" />
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
</security>
</binding>
</wsHttpBinding>
</bindings> </system.serviceModel>
<system.webServer> <modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/> </system.webServer>
</configuration>

不管是BasicHttpBinding还是WSHttpBinding又或者其他的绑定方式,服务端的接口一定要带有标记写法如下:

  [ServiceContract]
public interface IUserBussiness
{
[OperationContract]
string DoWork(string name);
}

实现上面也要有标记,如下:

    [ServiceBehavior]
public class UserBussiness:IUserBussiness
{
public string DoWork(string name)
{
return string.Format("hello Word by {0}", name);
}
}

编写WsHttpBinding的WCF通信方式的更多相关文章

  1. c# 编写REST的WCF

    REST(Representational State Transfer)即 表述性状态传递 ,简称REST,通俗来讲就是:资源在网络中以某种表现形式进行状态转移. RESTful是一种软件架构风格. ...

  2. 1 学习wcf 编写简单的WCF服务流程 并发布在IIS上

    学习笔记 学习大佬的博客 https://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.html  写一遍加深印象 图片看不清楚的 可 ...

  3. WCF使用地址去调用服务端的方法

    前面的章节已经讲过了WCF的代码和SVC页面的分离,这里是分离后,客户端调用代码如下: try { var myBinding = new BasicHttpBinding(); var myEndp ...

  4. WCF 配置服务 (02)

    配置服务概述 • 在设计和实现服务协定后,即可配置服务. 在其中可以定义和自定义如何向客户端公开服务,包括指定可以找到服务的地址.服务用于发送和接收消息的传输和消息编码,以及服务需要的安全类型. • ...

  5. WCF服务端调用client.

    wcf服务端 1,新建一个"windows窗口程序"名称为WCFServer2. 2.然后加入一个"WCF服务"名称为Service1. 详细步骤为:解决方式试 ...

  6. WCF 之 生成元数据和代理

    在WCF开发概述中讲解了手工方式的WCF应用,其实实际开发中使用更多的使用配置方式和元数据来实现WCF,下面我们来看一个具体的Demo,这个例子和WCF开发概述中使用的是同一个例子,只是实现方式不同, ...

  7. Log4net入门(WCF篇)

    在上一篇Log4net入门(ASP.NET MVC 5篇)中,我们讲述了如何在ASP.NET MVC 5项目中使用log4net.在这一篇中,我们将讲述如何在WCF应用中使用log4net,为了讲述这 ...

  8. Topshelf + ServiceModelEx + Nlog 从头构建WCF

    前言 Topshelf可以很方便的构建windows service,而且在本地开发时也可以构建Console宿主,因此很方便WCF的开发. ServiceModelEx则提供了很多便利的方法来配置w ...

  9. WCF SOA服务应用

    WCF是微软官方推出的一个基于服务的整合框架,它整合了以前的Web Service.MSMQ.Remoting等通信技术,通过灵活的配置,让服务编程更加容易.可扩展.这篇文章主要目的就是带领大家从开发 ...

随机推荐

  1. window下安装composer步骤(linux待研究)

    window下安装composer步骤--注意(安装完之后需要重启电脑才能生效) 转发:https://blog.csdn.net/wengedexiaozao/article/details/798 ...

  2. python之01电脑和操作系统简史

    电脑简史 早期计算方式发展 :手指和石头 ->结绳 ->算筹->计算尺 -> 算盘 19岁时(1642),帕斯卡发明了人类有史以来第一台机械计算机——帕斯卡加法器.它是一种系列 ...

  3. “Enterprise Architect”和数据库的不解之缘

    前言 在这个大数据盛行的时代,和数据打交道变的必不可少了,所有如果有工具来规范我们的数据库会更加方便我们的生活.这次机房,我们利用EA(Enterprise Architect)自动生成SQL语句来达 ...

  4. 2017-10-20 NOIP模拟赛2

    P98 a [问题描述]你是能看到第一题的 friends 呢.——hja世界上没有什么比卖的这么贵的弹丸三还令人绝望的事了,所以便有了这么一道题.定义?(?)为满足(? × ?)|?的有序正整数对( ...

  5. nodejs + webpack4 + babel6 结合写Chrome浏览器插件记录

    最近任务不忙,有时间了看一下Chrome插件相关的东西,于是想用nodejs + webpack写一个能直出插件的小工具. 1.nodejs + babel6 + webpack4 在写之前,是有把它 ...

  6. dblink 简单使用

    create extension dblink查看连接:select dblink_get_connections()断开所有连接:select dblink_disconnect()断开指定名称的连 ...

  7. PAT天梯赛L3-004 肿瘤诊断

    题目链接:点击打开链接 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是每 ...

  8. CSS之flex兼容

    我觉得写的很好的文章,但是我又没有时间去整理的. https://blog.csdn.net/u010130282/article/details/52627661 百分比 是在宽度自适应的时候要用 ...

  9. RabbitMQ权限

    RabbitMQ 引言 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队 ...

  10. n个点的基环树数量

    某裴姓蒟蒻上午提了一个小问题(rt)..然后他升华了..升华之前感受到了神犇的力量... 方法一: g[n][k]表示n个点,k条边的无向图(不一定连通) f[n][k]表示表示n个点,k条边的无向连 ...