原创地址:http://www.cnblogs.com/jfzhu/p/4025448.html

转载请注明出处

(一)创建WCF Service

(1)创建WCF Service类库

创建一个Class Library的项目:

删除掉默认的Class1.cs文件,然后添加一个WCF Service项目:

Visual Studio会自动帮助你生成两个文件:HelloService.cs 和 IHelloService.cs,另外还自动添加了System.ServiceModel引用,它是WCF的核心。

修改IHelloService.cs和HelloService.cs文件。

IHelloService.cs:

namespace HelloService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.
[ServiceContract]
public interface IHelloService
{
[OperationContract]
string GetMessage(string name);
}
}

HelloService.cs:

namespace HelloService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in both code and config file together.
public class HelloService : IHelloService
{ public string GetMessage(string name)
{
return "Hello " + name;
}
}
}

(2)创建WCF的Host

添加一个新的ASP.NET Empty Web Application:

添加一个新Item WCF Service

删除HelloService.svc.cs和IHelloService.cs文件。

添加HelloService Class Library的项目引用:

修改HelloService.svc为:

<%@ ServiceHost Language="C#" Debug="true" Service="HelloService.HelloService" %>

Web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService" behaviorConfiguration="metaBehavior">
<endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metaBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

其中,service name=”命名空间.类名”,behaviorConfiguration是用来关联下面behavior的定义的。

endpoint address中定义的是相对地址,与baseAddress结合起来为完整地址

endpoint contract=”命名空间.接口名”

两个endpoint,第一个binding是basicHttpBinding,用于HTTP协议;第二个endpoint用于交换metadata,binding为mexHttpBinding。

其中behavior的定义是用来允许交换metadata的。

Build解决方案,如果没有错误就进行到下一步,部署WCF Service到IIS

(二)部署WCF Service到IIS

(1)Publish HelloServiceIISHost项目

(2)部署到IIS

浏览HelloService.svc

(三)创建一个Windows Form来调用WCF Service

添加一个服务引用:

private void button1_Click(object sender, EventArgs e)
{
HelloService.HelloServiceClient client = new HelloService.HelloServiceClient();
label1.Text = client.GetMessage(textBox1.Text);
}

运行代码,效果如下:

(四)总结

svc文件中,包含着服务指令,Service属性指明文件指向的是哪个服务

<%@ ServiceHost Language="C#" Debug="true" Service="HelloService.HelloService" %>

service的代码可以在

(1) XXX.svc.cs的文件中

(2) 一个独立的Assembly(如同本文)

(3) App_Code文件夹下

Step by Step 创建一个WCF Service的更多相关文章

  1. C#创建一个Windows Service

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  2. Step by Step 创建一个 Web Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4022139.html 转载请注明出处 (一)创建Web Service 创建第一个项目,类型选择ASP.NET Empty ...

  3. 转载——Step by Step 创建一个 Web Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4022139.html 转载请注明出处 (一)创建Web Service 创建第一个项目,类型选择ASP.NET Empty ...

  4. C# 创建一个WCF服务

    做代码统计,方便以后使用: app.config配置文件设置: <configuration> <system.serviceModel> <bindings> & ...

  5. [转]VS2010中如何创建一个WCF

    本文转自:http://www.cnblogs.com/zhangliangzlee/archive/2012/08/28/2659701.html 转载:http://www.cnblogs.com ...

  6. 如何在Kubernetes里创建一个Nginx service

    Jerry之前的文章如何在Kubernetes里创建一个Nginx应用,已经使用kubectl命令行创建了Pod,但是在kubernetes中,Pod的IP地址会随着Pod的重启而变化,因此用Pod的 ...

  7. systemd 和 如何修改和创建一个 systemd service (Understanding and administering systemd)

    系统中经常会使用到 systemctl 去管理systemd程序,刚刚看了一篇关于 systemd 和 SysV 相关的文章,这里简要记录一下: systemd定义: (英文来解释更为原汁原味) sy ...

  8. 使用Windows Service Wrapper快速创建一个Windows Service

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  9. 使用Windows Service Wrapper快速创建一个Windows Service 如nginx

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

随机推荐

  1. Dw CS 破解

    据说,CS5的破解也可以用CS6的破解方法,不过可能本人太菜,有所失误,总是不成功,安装成功后,打开总是提示 : 我们无法开始您的Adobe Dreamweaver cs5 subscription ...

  2. 浩瀚技术团队... 安卓智能POS移动PDA开单器 开单器 进销存系统 进销存系统

    浩瀚技术团队... 智能POS移动PDA开单器 开单器 进销存系统 进销存系统 点餐 会员管理 会员管理 深度解读 手机APP移动办公到底是什么? 快速打单POS·不仅仅是快那么简单!  

  3. mongoose数据库连接和操作

    var mongoose = require('mongoose') mongoose.connect('mongodb://localhost:27017/hometown'); var db = ...

  4. C#开发笔记

    Dictionary 检查后获取值:Dictionary.TryGetValue() KeyValuePair<T, K> 的非泛型形式:DictionaryEntry List 由ILi ...

  5. Visual Studio 常用快捷键

    作为一个使用VisualStudio的程序员,使用快捷键会为你的开发提供助力. 下附个人开发过程中感觉比较实用的快捷键: 开始运行"devenv",启动相应版本的VisualStu ...

  6. Mysql在windows系统下的配置

    因为项目测试需求,不得不在本地装一个Mysql才能更方便地进行程序调试,整个过程虽然简单,但也遇到了一点麻烦,所以贴出来当是备忘. 这里采用MySQL Community Server  5.7.12 ...

  7. HTML5 学习笔记(三)——本地存储

    目录 一.HTML4客户端存储 1.1.提交表单发送到服务器的信息 1.2.客户端本地存储概要 二.localStorage 2.1.添加 2.2.取值 2.3.修改 2.4.删除 2.5.跨页面与跨 ...

  8. python基础04 运算

    数学运算 print 2+2  #加法 print 1.3-4 #剪法 print 3*5 #乘法 print 4.5/1.5 #除法 print 3**2   #乘方 print 10%3   #求 ...

  9. 我的前端故事----Ajax方式和jsonp的实现区别

    很久没有更新博客了,毕业2个月了,这段时间一直在忙于工作,一直没有时间更新,最近做的活动突然发现之前的经验居然忘记了...索性想想还是重新开始用博客记录平日里的工作经验吧,吐槽就到这里了,这篇记录的是 ...

  10. Linux命令dos2unix 从windows转换到linux --- nuix2dos从linux转换到windows

    dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter).DOS下的文本文件是以\r\n作为断行标志的 ...