结构:

 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. 求一个数的n次幂

    1.当这个数是2的多少次幂: 求(2^m)^n  =  2^(m*n) = 1<<m*n; 2.快速幂(要考虑底数a,和m的正负) int quick_mod(int a,int m){ ...

  2. 爬取微博的数据时别人用的是FM.view方法传递html标签那么jsoup怎么解析呢

    使用JSOUP就行 这里给出点思路 我只做了自己的首页和其他人的微博首页的抓取 其他的抓取没尝试(不好意思 比较懒...) 首先是利用JSOUP进行登陆 获取页面 看了下微博的登陆表格 发现用了aja ...

  3. 2018.12.18 bzoj2242: [SDOI2011]计算器(数论)

    传送门 数论基础题. 对于第一种情况用快速幂,第二种用exgcdexgcdexgcd,第三种用bsgsbsgsbsgs 于是自己瞎yyyyyy了一个bsgsbsgsbsgs的板子(不知道是不是数据水了 ...

  4. Centos7 yum install vim 出现“could not retrieve mirrorlist”

    ps:来源 https://www.cnblogs.com/justphp/p/5959655.html 办法一:改dns解析 vim /etc/resolv.conf 添加: nameserver ...

  5. auto和decltype(c++11)

    1.auto 1)auto是一个类型说明符(类型说明符就是像int.double这样的),用来定义一个变量,它可以让编译器去分析表达式的类型,并使用该表达式的值去初始化变量 //auto定义的变量必须 ...

  6. lf-8.4 数据的增删改

    MySQL数据操作: DML 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 ...

  7. c# 动态数组-----“动态”数组

    其实在大多数工作中我们能通过前处理来确定我们的数组有多大,这样我们就可以声明相应大小的数组了.我感觉这种“动态”数组就够我用了.比如我要处理excel中数据,数据有m行*n列,这样我就可以通过读取ex ...

  8. Java交流分享(522818473)

    今天来分享哈自己的技术交流群,记得还是刚接触Java建立的群,那时候学习Java很有动力,经常和群友谈论问题,现在都专注公司业务和技术这一块,很多后端框架都没用除了restful,其他都是封装的,不过 ...

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

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

  10. 深入浅出javascript(八)this、call和apply

    _________此篇日志属于重要记录,长期更新__________ this,call,apply这三个是进阶JS的重要一步,需要详细的记录. ➢ this 一.作为对象的方法调用. 当函数作为对象 ...