结构:

 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. 爬取微博的数据时别人用的是FM.view方法传递html标签那么jsoup怎么解析呢

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

  2. EF对应null的处理

    原来的代码是 if (string.IsNullOrWhiteSpace(seal)) seal = null; ctx.Terminal.FirstOrDefault(ent=>ent.Sea ...

  3. centos7 新增ip

    1.进入network-scripts目录:cd /etc/sysconfig/network-scripts/ 2.复制ifcfg-eth0: cp ifcfg-eth0 ifcfg-eth0:0 ...

  4. 实现数组(java)

    一,数组 java中有对数组的数据结构:数组就是一个存放固定数据的结构. 数组的声明举例:int [] array=new int [3],与之相同的是private  in [ ] array; a ...

  5. 用户权限,pymysql

    单表查询的完整语法 select [distinct] [*|字段|聚合函数|表达式] from tablewhere group byhaving distinctorder bylimit mys ...

  6. Java用户界面技术

    组件     文本框.复选框.下拉框.文本区域框.列表listbox.按钮.文本标签   布局     layout属性们.panel   java.swing包     JButton     JL ...

  7. 关于Linux学习中的问题和体会

    本科期间未开展过与之相关的课程,所以初次接触Linux难免有些问题!参照老师给的学习资料中内容,逐步解决了一些问题,但还有一些问题没解决,下面列举出自己遇到的一些问题. 1.在环境变量与文件查找专题中 ...

  8. Phalanx (hdu 2859)

    http://acm.hdu.edu.cn/showproblem.php?pid=2859     Time Limit: 10000/5000 MS (Java/Others)    Memory ...

  9. C#将XML转换成JSON 使用 JavaScript 将 XML 转成 JSON

    如何在ASP.NET中用C#将XML转换成JSON [JavaScript]代码 // Changes XML to JSON function xmlToJson(xml) { // Create ...

  10. elasticsearch5环境搭建

    1.下载ElasticSearch https://www.elastic.co/cn/downloads/elasticsearch#ga-release 因为是windows版本,所以下载zip即 ...