类库 System.ServiceModle

WCF类库

契约IUser1,实现User1

[ServiceContract]
public interface IUser1
{
[OperationContract]
string GetUser1Name(string name);
} public class User1 : IUser1
{
public string GetUser1Name(string name)
{
return "我是usre1" + name;
}
}

契约IUser2 ,实现User2

    [ServiceContract]
public interface IUser2
{
[OperationContract]
string GetUser1Name(string name);
} public class User2 : IUser2
{
public string GetUser1Name(string name)
{
return "我是user2" + name;
}
}

契约IUnity1和IUnity2,实现Unity(一个实现继承了两个契约,主意看一下配置文件如何配置)

    [ServiceContract]
public interface IUnity1
{
[OperationContract]
int GetUnityCount();
} [ServiceContract]
public interface IUnity2
{
[OperationContract]
string GetUnityString();
} public class Unity : IUnity1, IUnity2
{
public int GetUnityCount()
{
return ;
} public string GetUnityString()
{
return "Unity";
}
}

宿主

        static void Main(string[] args)
{
ServiceHost sh1 = new ServiceHost(typeof(WcfLib.User1));
sh1.Open();
Console.WriteLine("服务1开启");
ServiceHost sh2 = new ServiceHost(typeof(WcfLib.User2));
sh2.Open();
Console.WriteLine("服务2开启");
ServiceHost sh3 = new ServiceHost(typeof(WcfLib.Unity.Unity));
sh3.Open();
Console.WriteLine("服务3开启");
Console.ReadKey();
}

  服务配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<services>
<!--name="命名空间名称.实现类名称"-->
<service name="WcfLib.User1" behaviorConfiguration="mexBehaviour">
<endpoint address="MyServices1" binding="basicHttpBinding" contract="WcfLib.IUser1">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9999/"/>
</baseAddresses>
</host>
</service>
<service name="WcfLib.User2" behaviorConfiguration="mexBehaviour">
<host>
<baseAddresses>
<add baseAddress="http://localhost:6666/"/>
</baseAddresses>
</host>
<endpoint address="MyServices2" binding="basicHttpBinding" contract="WcfLib.IUser2"></endpoint>
</service>
<service name="WcfLib.Unity.Unity" behaviorConfiguration="mexBehaviour">
<host>
<baseAddresses>
<add baseAddress="http://localhost:7777/"/>
<add baseAddress="net.tcp://localhost:7776/"/>
</baseAddresses>
</host>
<endpoint address="MyServices3" binding="basicHttpBinding" contract="WcfLib.Unity.IUnity1" ></endpoint>
<!--错误:找不到具有绑定 NetTcpBinding 的终结点的与方案 net.tcp 匹配的基址。注 册的基址方案是 [http]。 解决:TCP通讯 地址必须是TCP的(TCP不能宿主在IIS上) net.tcp://localhost:7776/-->
<endpoint address="MyServices4" binding="netTcpBinding" contract="WcfLib.Unity.IUnity2"></endpoint>
</service>
</services> <behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<!--设置未false被人不能发现,一般当客户端已经加载好服务代理就可以设置为false了。配置修改,服务要重启,才能生效-->
<serviceMetadata httpGetEnabled="true" />
</behavior>
<behavior>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

客户端

右键添加服务

配置文件自动生成

        static void Main(string[] args)
{
WCFServiceUser1.User1Client cli1 = new WCFServiceUser1.User1Client();
WCFServiceUser2.User2Client cli2 = new WCFServiceUser2.User2Client();
WCFServiceUnity.Unity1Client cli3 = new WCFServiceUnity.Unity1Client();
WCFServiceUnity.Unity2Client cli4 = new WCFServiceUnity.Unity2Client();
Console.WriteLine(cli1.GetUser1Name("name1"));
Console.WriteLine(cli2.GetUser1Name("name2"));
Console.WriteLine(cli3.GetUnityCount());
Console.WriteLine(cli4.GetUnityString());
}

WCF 调用服务标准写法

static void Main(string[] args)
{
//不适用using,原因using在网络中断时,wcf不能关闭。websevice可以是用using释放,websevice标准写法是用using
CustomService.UserServiceSoapClient ucli = null;
try
{
ucli = new CustomService.UserServiceSoapClient();
ucli.GetStr("");
//手动释放,
ucli.Close();
}
catch (Exception)
{
if (ucli != null)
{
ucli.Abort();
}
}
}

源码下载

WCF-初识DEMO的更多相关文章

  1. WCF简单Demo

    WCF,光看书的原理,稍微有点枯燥,通过自己动手,会更容易理解契约声明,面向服务,分布式等概念. 1.创建WCF服务. 2.WcfService1.CS中声明新的契约. namespace WcfSe ...

  2. WCF学习笔记之WCF初识

    这篇博客将介绍WCF的最基础内容,让我们对WCF有一个基本的认识.后续的博客中将会介绍WCF其他方面内容.本篇博客将通过一个简单的例子,介绍如何创建WCF服务,并承载这个服务,让客户端来访问它.下面请 ...

  3. WCF初识

    WCF能干什么? 在win32中,应用程序是运行在进程的线程中的,.NET出现之后,出现了AppDomain,其实就相当于在进程和线程之间又又了一层包装层,类似于子进程的概念,在一个进程或者应用程序域 ...

  4. WCF 初识(一)

    WCF的前世今生 在.NETFramework 2.0以及前版本中,微软发展了Web Service(SOAP with HTTP communication),.NET Remoting(TCP/H ...

  5. C# WCF初识

    原文:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html 方式1: 需引用 System.ServiceModel namespa ...

  6. WCF:初识

    结构: using System.ServiceModel; namespace MyServices { [ServiceContract] public interface IHomeServic ...

  7. wcf,jquery,post,跨域

    参照了网上的很多资料,vs2012 项目是wcf服务. .demo地址http://files.cnblogs.com/files/dswyzx/WcfServiceDemoa.rar

  8. UI层调用WCF服务实例(源码)

    WCF原理性的东西,暂时还没有深入研究,只是在公司的项目中使用到了,会调用,然后再多做了一些了解,现在将它抽出来了一个小实例,写了一个WCF的demo. 我写的这个WCF.Demo主要包括数据契约和服 ...

  9. WCF服务部署到IIS上,然后通过web服务引用方式出现错误的解决办法

    本文转载:http://www.cnblogs.com/shenba/archive/2012/01/06/2313932.html 昨天在用IIS部署一个WCF服务时,碰到了如下错误: 理解了文档内 ...

  10. websocket初识

    一.官网 官网地址:http://www.websocket.org/ 二.websocket初识demo <input id="sendTxt" type="te ...

随机推荐

  1. jenkins之docker安装(jenkins/jenkins:lts)

    建议使用此镜像安装,不要使用官网推荐的jenkinsci/blueocean镜像,使用它构建node程序会出现问题. 1.宿主服务器jenkins_home目录权限 为了方便安装插件,升级,迁移,因此 ...

  2. mysql解压文件安装

    mysql.zip版本的安装教程   MySQL zip版本安装 一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在M ...

  3. LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. ngx.shared.DICT.get 详解

    ngx.shared.DICT.get 原文: ngx.shared.DICT.get syntax: value, flags = ngx.shared.DICT:get(key) context: ...

  5. 搭建JavaWeb应用开发环境——Tomcat服务器

    学习web开发,需要先安装一台web服务器,然后再在web服务器中开发相应的web资源,供用户使用浏览器访问. 搭建JavaWeb应用开发环境——Tomcat服务器 1.疑问:学习web开发,为什么必 ...

  6. Linux内存使用情况以及内存泄露情况

    1. 内存使用情况分析 http://www.360doc.com/content/15/1118/13/17283_514054063.shtml https://www.linuxidc.com/ ...

  7. PyTorch Tutorials 5 数据并行(选读)

    %matplotlib inline 数据并行(选读) Authors: Sung Kim and Jenny Kang 在这个教程里,我们将学习如何使用 DataParallel 来使用多GPU. ...

  8. Android高频单词

    Display 显示 Camera 照相机 Bluetooth 蓝牙 Flash Memory 闪存 Audio 音频 Management 管理 SurFace 界面 Media 多媒体 Frame ...

  9. 5G && 物联网

    可打电话的 2G.能够上网的 3G.满足移动互联网用户需求的 4G 相比,逐步可以商用的 5G 在多重性能上更胜一筹,如 高数据率: 低延迟: 更节能: 有效地降低通信成本: 具备更高的系统容量: 更 ...

  10. 123457123456#0#-----com.threeapp.renZheDadishu02-----忍者版打地鼠

    com.threeapp.renZheDadishu02-----忍者版打地鼠