类库 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. linux系列(十九):firewall-cmd命令

    1.命令格式 firewall-cmd [选项] [参数] 2.命令功能: 简单来说是一个防火墙管理工具. 3.简单使用: systemctl start firewalld # 启动, system ...

  2. ckeditor自定义工具栏

    /** * 获取编辑器工具栏自定义参数 * @param type 类型 simple=极简版 basic=基本版 full=完整版 */ function get_ckeditor_toolbar( ...

  3. P1069 细胞分裂——数学题,质因数分解

    P1069 细胞分裂 我们求的就是(x^k)|(m1^m2) k的最小值: 先给m1分解质因数,再给每个细胞分解: 如果m1有的质因数,细胞没有就跳过: 否则就记录答案: 注意整数除法下取整的原则: ...

  4. CF1174E Ehab and the Expected GCD Problem(动规+数论+分解)

    做法 先来填第一个数,为了保证\(f(p)\)最大,第一个数分解一下为\(\prod\limits_{p_i}p_i^{k_i}\)使得\(\sum\limits_{k_i}\)最大 显然第一个数为\ ...

  5. Hadoop namenode连接journalnode限制导致集群启动失败

    错误1:刚搭建的新集群,启动journalnode以后,格式化namenode节点,出现如下错误 注意其中划红线的地方. 出现这个错误的原因是journalnode节点还没有准备好,而namenode ...

  6. linux process cycle

    As already discussed, a new process is created through fork() and if a new executable is to be run t ...

  7. エンジニア死滅シタ世界之学べない学校 [MISSION LEVEL: C]-Python3

    答案 # coding: utf-8 # 自分の得意な言語で # Let's チャレンジ!! N=input() w_a=0 w_b=0 gpc_dict={ "gg":0,&qu ...

  8. IOS开发环境搭建

    前置条件 1. 必要:一台装有Mac OS X操作系统的电脑:经济允许的话可以买一部Mac book:否则的话,可以试试黑苹果或虚拟机. 2.必要:一个有可用的Apple ID:免费,在Apple的官 ...

  9. Java 面向对象(十四)

    反射 反射是框架设计的灵魂 一.类的加载时机 当程序要使用某个类时,如果该类还未被加载到内存中,系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载 :就是指将class文件读入内存,并为 ...

  10. osg #ifdef _WIN32 osg

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <osgViewer/Viewer> #include ...