1.创建WCF项目

2.系统自动生成IWcfService

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IWcfService
{ [OperationContract]
string GetData(int value); [OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: 在此添加您的服务操作
} // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello "; [DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
} [DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

(1)服务契约:ServiceContract(服务)和OperationContract  (方法)

(2)数据契约:DataContract(类)和DataMember(属性) 用于类和结构上

(3)消息契约:MessageContract 用于soap消息

3.WCF服务类

    public class WcfService : IWcfService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
} public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}

4.服务配置文件

  <system.serviceModel>
<!--配置绑定节点Start-->
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding0" maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647"/>
<security mode="None" />
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="netTcpBinding0" maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647"/>
<security mode="None" />
</binding>
</netTcpBinding>
<wsHttpBinding></wsHttpBinding>
</bindings>
<!--配置绑定节点End--> <!--配置服务节点Start-->
<services>
<!--配置某一服务,在这里可以指定服务名称-->
<service name="WcfServiceTest.WcfService">
<endpoint address="aaa" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding0"
name="BasicHttpBinding_WcfService" contract="WcfServiceTest.IWcfService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBinding0"
name="NetTcpBinding_WcfService" contract="WcfServiceTest.IWcfService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<!--配置服务节点End--> <behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

5.iis部署WCF服务

6.添加客户端项目并添加服务引用

7.Main程序中添加wcf服务并调用方法

    class Program
{
static void Main(string[] args)
{
var client = new WcfService.WcfServiceClient();
try
{
var str = client.GetData();
Console.WriteLine(string.Format("内容:{0}", str));
client.Close();
}
catch (Exception ex)
{
Console.WriteLine("出现异常!");
client.Abort();
}
Console.ReadLine();
}
}

8.客户端配置文件

    <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_WcfService" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_WcfService">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<!--<endpoint address="http://localhost/WcfServiceTest/WcfService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"
contract="WcfService.IWcfService" name="BasicHttpBinding_WcfService" />-->
<endpoint address="net.tcp://localhost/WcfServiceTest/WcfService.svc"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_WcfService"
contract="WcfService.IWcfService" name="NetTcpBinding_WcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>

【5】.net WCF 简单实例的更多相关文章

  1. C# 一个WCF简单实例

    以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 复制代码 代码如下: namespace WcfDemo { // 注意: 如果更改此处的接口名称 &qu ...

  2. WCF简单实例--用Winform启动和引用

    以订票为例简单应用wcf程序,需要的朋友可以参考下 本篇转自百度文档,自己试过,确实可以用. 以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 namesp ...

  3. .net WCF简单实例

    最近看到网上招聘有许多都需要WCF技术的人员,我之前一直没接触过这个东西,以后工作中难免会遇到,所谓笨鸟先飞,于是我就一探究竟,便有了这边文章.由于是初学WCF没有深入研究其原理,只是写了一个demo ...

  4. Wcf简单实例1

    一.客户端添加服务引用,并调用 1.使用客户端代理同步调用 static void TestTwo() { /*********同步访问********/ Person.PersonServiceCl ...

  5. WCF 学习总结1 -- 简单实例

    从VS2005推出WCF以来,WCF逐步取代了Remoting, WebService成为.NET上分布式程序的主要技术.WCF统一的模型整合了以往的 WebService.Remoting.MSMQ ...

  6. WCF分布式开发步步为赢(9):WCF服务实例激活类型编程与开发

    .Net Remoting的激活方式也有三种:SingleTon模式.SingleCall模式.客户端激活方式,WCF服务实例激活类型包括三种方式:单调服务(Call Service),会话服务(Se ...

  7. WCF小实例以及三种宿主

    WCF小实例以及三种宿主 最近一直在学习WCF相关知识,下面将通过一个小实例对所学的知识进行简单的回顾:本实例是一个简单三层操作数据库,并且也简单实现的三种宿主(控制台宿主,IIS宿主以及Window ...

  8. Hibernate(二)__简单实例入门

    首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...

  9. 最新 Eclipse IDE下的Spring框架配置及简单实例

    前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...

随机推荐

  1. 「HNOI 2014」米特运输

    题目链接 戳我 \(Describe\) 谁出的题目啊?这么长的题面,看完就滚粗了.强烈谴责 给一棵树,每个点有一个权值,要求修改一些权值,使: 一个点的权值必须是其所有儿子的权值之和 一个点的儿子权 ...

  2. 二十四、MongoDB数据库的使用

    首先按照上一篇文章的介绍,启动并连接数据库 然后我们开始学习如何使用MongoDB数据库: 1.创建数据库 第一步,在cmd窗口执行: use dbname dbname是你打算要创建的数据库名称 执 ...

  3. 201621123023《Java程序设计》第7周学习总结

    一.本周学习总结 1.1 思维导图:Java图形界面总结 二.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 关键词:事件源.事件监听器.事件处理方法.事件适配器 ...

  4. [Android] Android MVP 架构下 最简单的 代码实现

    Android  MVP 架构下  最简单的 代码实现 首先看图: 上图是MVP,下图是MVC MVP和MVC的区别,在于以前的View层不仅要和model层交互,还要和controller层交互.而 ...

  5. 扩展欧拉定理【洛谷P4139】 上帝与集合的正确用法

    P4139 上帝与集合的正确用法 \(2^{2^{2^{\dots}}}\bmod p\) 卡最优解倒数第一祭. 带一下扩展欧拉定理就好了. code: #include <iostream&g ...

  6. lexical or preprocessor issue file not found in Xcode

    The very last suggestion is all I needed to fix this issue. Close & re-open Xcode.

  7. BZOJ - 1935 / 1176 cdq分治 三维偏序

    题意:给定n*m的网格,且给出n个(x,y)表示该网格已被占有,q次询问(x1,y1)到(x2,y2)的网格中有多少个被占有,n,m范围1e7,q范围5e5 cdq按x轴排序,树状数组维护y轴 #in ...

  8. php7 中?? 和 ?:的区别

    $b = $a?? $c ;相当于$b= isset($a)?$a:$c; $b = $a?: $c ;则是 $b = !empty($a) ? $a:$c;

  9. Shiro如何使用Ehcache实现Session共享

    最近项目中用到的Session共享场景:两个独立应用,希望实现DB层共享用户,而且用户只需要登录一次. 分析:这种场合,不适用单点,因为用户数据并不需要单独在第三方应用管理,而且添加单点也会增加整个系 ...

  10. window.onresize事件在vue项目中的应用

    //vue页面<template> <div id='echart'> 报表 </div> </template> <script> exp ...