结构:

 using System.ServiceModel;
namespace MyServices
{
[ServiceContract]
public interface IHomeService
{
[OperationContract]
int GetLength(string name);
}
}

契约

 namespace MyServices
{
public class HomeService:IHomeService
{
public int GetLength(string name)
{
return name.Length;
}
}
}

实现类

 using System;
using System.ServiceModel; namespace MyServices
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HomeService)))
{
try
{
host.Open();
Console.WriteLine("服务开启!");
Console.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}

服务启动

 using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HomeServiceReference.HomeServiceClient client = new HomeServiceReference.HomeServiceClient();
//HomeServiceClient homeServiceClient = new HomeServiceClient();
var r = client.GetLength("abc");
Console.WriteLine(r);
Console.ReadKey();
}
}
}

调用

配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="IHomeServiceBinding" />
</netTcpBinding>
</bindings> <behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="MyService.HomeService">
<endpoint address="http://127.0.0.1:1920/HomeService" binding="basicHttpBinding" contract="MyService.IHomeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:1920"/>
</baseAddresses>
</host>
</service>
</services> </system.serviceModel>
</configuration>

basicHttpBinding

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mxbehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="MyService.HomeService" behaviorConfiguration="mxbehavior">
<endpoint address="net.tcp://localhost:1921/HomeService" binding="netTcpBinding" contract="MyService.IHomeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:1921/HomeService"/>
</baseAddresses>
</host>
</service>
</services> </system.serviceModel>
</configuration>

netTcpBinding

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mxbehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="msmqbinding">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
<services>
<service name="MyService.HomeService" behaviorConfiguration="mxbehavior">
<endpoint address="net.msmq://localhost/private/homequeue" binding="netMsmqBinding"
contract="MyService.IHomeService" bindingConfiguration="msmqbinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:1922/HomeService"/>
</baseAddresses>
</host>
</service>
</services> </system.serviceModel>
</configuration>

netMsmqBinding

WCF:初识的更多相关文章

  1. WCF学习笔记之WCF初识

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

  2. WCF初识

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

  3. WCF 初识(一)

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

  4. C# WCF初识

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

  5. WCF编程系列(一)初识WCF

    WCF编程系列(一)初识WCF   Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型.WCF的基本概念:  地址:定义服务的 ...

  6. 我们一起学习WCF 第一篇初识WCF(附源码供对照学习)

    前言:去年由于工作需要我学习了wcf的相关知识,初期对wcf的作用以及为何用怎么样都是一知半解,也许现在也不是非常的清晰.但是通过项目对wcf的运用在脑海里面也算有了初步的模型.今天我就把我从开始wc ...

  7. WCF系列教程之初识WCF

    本随笔参考自WCF编程系列(一)初识WCF,纯属读书笔记,加深记忆. 1.简介:Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程 ...

  8. 初识WCF

    以前,总是说自己的基础知识不牢靠,就是因为自己总是不总结.昨天,学费交了,顿时感觉不一样了,心里有劲也有力了,知道了以前的自己到底为什么会那样了,因为没有压力. --题记 我参加过浩哥的招标项目,参加 ...

  9. WCF(一):初识WCF

    目录: 一.什么是WCF 二.WCF能做什么 三.WCF的模型 四.WCF的基本概念 五.WCF的快速创建 1.WCF是什么 A.WindowsCommunication Foundation(WCF ...

随机推荐

  1. python约束 异常 MD5 日志处理

    一.约束 1.用父类执行约束 子类继承父类,用重写方法,对子类的方法进行约束. class Foo: def login(self): # 对子类进行约束,该方法需要重写 # 没有执行的错误 rais ...

  2. linux复制文件并修改文件名

    #!/bin/bash #复制/casnw/backup/db203oradata/目录下的所有后缀名为dmp的文件拷贝到/casnw/backup/dbmonthbak 目录下cp -f /casn ...

  3. C++STL 预定义函数对象和函数适配器

    预定义函数对象和函数适配器 预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象,#include <functional> 必须包含. 1使用预定义函数对象: void ...

  4. Mybatis-Plus 实战完整学习笔记(九)------条件构造器核心用法大全(上)

    一.Mybatisplus通用(公共方法)CRUD,一共17种(3.0.3版),2.3系列也是这么多,这个新版本一定程度进行了改造和删减. 二.构造器UML图(3.0.3)-----实体包装器,主要用 ...

  5. create table b1 as select * from b建表锁表测试

    A: create table a1 like a; insert into a1 as select * from a; B: create table b1 as select * from b; ...

  6. Unicode和多字节的相互转换

    多字节转Unicode 四步: Step1 #include <iostream> #include "windows.h" using namespace std; ...

  7. DS8800后端的光纤通道交换式互连方式

    DS8800 使用SAS 硬盘.使用了FC 到SAS 转换,光纤通道交换技术被用于DS8800 后端. FC 技术是普遍用于在一个光纤通道仲裁环路(Fibre Channel Arbitrated L ...

  8. Node的关系型数据库ORM库:bookshelf

    NodeJs 关系数据库ORM库:Bookshelf.js bookshelf.js是基于knex的一个关系型数据库的ORM库.简单易用,内置了Promise的支持.这里主要罗列一些使用的例子,例子就 ...

  9. android testview + listview 整体滚动刷新

    listview滚动刷新不再讲述怎么实现 因为想实现整体滚动的效果,初始计划scrollView嵌套listview实现. 问题一:scrollview嵌套listview时,listview只能显示 ...

  10. 第73讲:Scala界面和事件处理编程进阶实战

    今天学习了王家林老师讲解的scala编程的第73讲,主要是文件选择器的使用.让我们通过代码来亲身体验一下. import scala.swing._import java.io.Fileimport ...