假设所有工程的命名空间是demo。

    1. 新建一个C#的ClassLibrary(类库)工程。
    2. 在工程引用中加入System.ServiceModel引用。
    3. 定义接口,你可以删除自动生成的代码,或者直接修改代码来添加接口。
[ServiceContract]

Interface IMath {

[Operationcontract]

            Int add (int a, int b);

}
    1. 实现接口

添加一个新类,如Math实现该接口。

Public class Math : IMath{

            Int add(int a,int b){

                        Return (a+b);

}

}
    1. 编写寄宿应用程序,新建一个控制台或者Winform程序。添加对System.ServiceModel和System.ServiceModel.Channels的引用。
      1. 通过代码绑定。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels;

namespace HelloServiceHost

{

class Program

{

static void Main(string[] args)

{

using (MyHelloHost host = new MyHelloHost())

{

host.openService();

}

}

}

public class MyHelloHost : IDisposable

{

private ServiceHost serviceHost = null;

/// <summary>

/// base address

/// </summary>

//public const string baseAddress = "net.pipe://localhost";

public const string baseAddress = "http://localhost";

/// <summary>

/// 服务名称

/// </summary>

public const string serviceAddress = "MathService";

/// <summary>

/// 实现服务的契约

/// </summary>

public static readonly Type serviceType=typeof(demo.Math);

/// <summary>

/// 接口契约

/// </summary>

public static readonly Type contractType = typeof(HelloService.IHelloService);

/// <summary>

/// 定义一个服务绑定

/// </summary>

//public static readonly Binding helloBinding = new NetNamedPipeBinding();

public static readonly Binding helloBinding = new BasicHttpBinding();

/// <summary>

/// 构造服务宿主

/// </summary>

private void createHelloServiceHost()

{

///创建服务对象

serviceHost = new ServiceHost(serviceType,new Uri[]{new Uri(baseAddress)});

///添加一个终结点

serviceHost.AddServiceEndpoint(contractType, helloBinding, serviceAddress);

}

public ServiceHost ServiceHost

{

get { return serviceHost; }

}

/// <summary>

/// 打开服务

/// </summary>

public void openService()

{

Console.WriteLine("Service is starting...");

serviceHost.Open();

Console.WriteLine("Service running...");

}

public MyHelloHost()

{

createHelloServiceHost();

}

public void Dispose()

{

if (null != serviceHost)

{

(serviceHost as IDisposable).Dispose();

}

}

}

}

                    1. 通过配置文件绑定。如果是控制台程序,请添加App.config文件,并修改如下,如果是Winform直接修改App.config文件。

1a)  修改App.Config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<services>

<service name="demo.Math" behaviorConfiguration="TestBehavior">

<host>

<baseAddresses>

<add baseAddress="http://localhost:8001/MathService"/>

</baseAddresses>

</host>

<endpoint address="" binding="basicHttpBinding" contract="demo.IHello" />

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="TestBehavior">

<serviceMetadata httpGetEnabled="true"/>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

1b)  修改代码如下

class Program

{

static void Main(string[] args)

{

ServiceHost host = new ServiceHost(typeof(demo.Math));

host.Open();

Console.ReadKey();

}

}

WCF学习笔记 -- 如何用C#开发一个WebService的更多相关文章

  1. VSTO 学习笔记(十一)开发Excel 2010 64位自定义公式

    原文:VSTO 学习笔记(十一)开发Excel 2010 64位自定义公式 Excel包含很多公式,如数学.日期.文本.逻辑等公式,非常方便,可以灵活快捷的对数据进行处理,达到我们想要的效果.Exce ...

  2. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  3. WCF学习笔记之传输安全

    WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...

  4. WCF 学习笔记之异常处理

    WCF 学习笔记之异常处理 1:WCF异常在配置文件 <configuration> <system.serviceModel> <behaviors> <s ...

  5. WCF 学习笔记之双工实现

    WCF 学习笔记之双工实现 其中 Client 和Service为控制台程序 Service.Interface为类库 首先了解契约Interface两个接口 using System.Service ...

  6. Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建

    Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...

  7. Android:日常学习笔记(8)———探究UI开发(5)

    Android:日常学习笔记(8)———探究UI开发(5) ListView控件的使用 ListView概述 A view that shows items in a vertically scrol ...

  8. Android:日常学习笔记(7)———探究UI开发(4)

    Android:日常学习笔记(7)———探究UI开发(4) UI概述  View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...

  9. Android:日常学习笔记(8)———探究UI开发(3)

    Android:日常学习笔记(8)———探究UI开发(3) 详解四种基本布局 前言 布局定义用户界面的视觉结构,如Activity或应用小部件的 UI.您可以通过两种方式声明布局: 在 XML 中声明 ...

随机推荐

  1. UVa10562 Undraw the Trees

      注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...

  2. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) A. Bear and Poker 分解

    A. Bear and Poker Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/573/pro ...

  3. [原]生产环境下的nginx.conf配置文件(多虚拟主机)

    [原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...

  4. ConfigurationManager.GetSection()方法的使用

    GetSection方法读取的是configSections节点,这个节点在web.config配置文件中,它比较特殊,必须放置于首节点,也就是说,在它之前不能有其它类型的节点.configSecti ...

  5. c++中的强制转换static_cast、dynamic_cast、reinterpret_cast的不同用法儿

    c++中的强制转换static_cast.dynamic_cast.reinterpret_cast的不同用法儿   虽然const_cast是用来去除变量的const限定,但是static_cast ...

  6. 高级I/O之readn和writen函数

    管道.FIFO以及某些设备,特别是终端.网络和STREAMS设备有下列两种性质: (1)一次read操作所返回的数据可能少于所要求的数据,即使还没有达到文件尾端也可能是这样.这不是一个错误,应当继续读 ...

  7. struts2.1笔记03:AOP编程和拦截器概念的简介

    1.AOP编程 AOP编程,也叫面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用A ...

  8. 沈逸老师PHP魔鬼特训笔记(2)

    一.这一课会学习到几个懒人函数: 1.file_put_contents (PHP 5, PHP 7) file_put_contents — 将一个字符串写入文件 说明 int file_put_c ...

  9. 标签切换JS代码

    //标签切换 var nav = $('.index-nav'); var content = $('.index-nav-content li'); function hoverNav ($eleA ...

  10. Scala中的偏函数与部分应用函数

    Scala中有PartialFunction的概念, 同时还要一个概念叫Partial Applied Function. 前者译作偏函数, 后者译作"偏应用函数"或"部 ...