Step by Step 创建一个WCF Service
原创地址: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的更多相关文章
- C#创建一个Windows Service
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- Step by Step 创建一个 Web Service
原创地址:http://www.cnblogs.com/jfzhu/p/4022139.html 转载请注明出处 (一)创建Web Service 创建第一个项目,类型选择ASP.NET Empty ...
- 转载——Step by Step 创建一个 Web Service
原创地址:http://www.cnblogs.com/jfzhu/p/4022139.html 转载请注明出处 (一)创建Web Service 创建第一个项目,类型选择ASP.NET Empty ...
- C# 创建一个WCF服务
做代码统计,方便以后使用: app.config配置文件设置: <configuration> <system.serviceModel> <bindings> & ...
- [转]VS2010中如何创建一个WCF
本文转自:http://www.cnblogs.com/zhangliangzlee/archive/2012/08/28/2659701.html 转载:http://www.cnblogs.com ...
- 如何在Kubernetes里创建一个Nginx service
Jerry之前的文章如何在Kubernetes里创建一个Nginx应用,已经使用kubectl命令行创建了Pod,但是在kubernetes中,Pod的IP地址会随着Pod的重启而变化,因此用Pod的 ...
- systemd 和 如何修改和创建一个 systemd service (Understanding and administering systemd)
系统中经常会使用到 systemctl 去管理systemd程序,刚刚看了一篇关于 systemd 和 SysV 相关的文章,这里简要记录一下: systemd定义: (英文来解释更为原汁原味) sy ...
- 使用Windows Service Wrapper快速创建一个Windows Service
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- 使用Windows Service Wrapper快速创建一个Windows Service 如nginx
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
随机推荐
- C++字符串格式化库:CPPFormatLibrary
这个是很久之前写的,去年总结了一下,将其单独提取出来,作为一个开源库放到了GitHub上,然而CPPFormat之类的名字都已经被抢注了,结果只好注册了一个这么蛋疼的名字:CPPFormatLibra ...
- 修改socket为keepAlive
参考文章:http://blog.csdn.net/ctthuangcheng/article/details/8596818 [root@mdw- gpadmin]# vi /etc/sysctl. ...
- Ubuntu14.04 64位机上安装cuda8.0 cudnn5.0操作步骤 - 网络资源是无限的
查看Ubuntu14.04 64位上显卡信息,执行: lspci | grep -i vga lspci -v -s 01:00.0 nvidia-smi 第一条此命令可以显示一些显卡的相关信息:如果 ...
- 如何用Qt做SolidWorks二次开发
这个问题困扰了我2年了,之前找到的教程都是MFC的,ATL导入向导或是通过导入类型库的方式来调用控件,我一直都搞不明白. 最近学习了ActiveQT以及通过ActiveQT控制EXCEL.Word.P ...
- 支付宝推AR实景红包,抢红包得拼脑力和体力
近年春节,各大互联网平台都会借机掀起"红包大战",其中支付宝和微信的红包玩法备受用户关注.今年,微信尚未公布春节红包相关的方案信息,不过,今天支付宝率先推出"AR实景红包 ...
- BZOJ4488: [Jsoi2015]最大公约数
Description 给定一个长度为 N 的正整数序列Ai对于其任意一个连续的子序列{Al,Al+1...Ar},我们定义其权值W(L,R )为其长度与序列中所有元素的最大公约数的乘积,即W(L,R ...
- SQLServer触发器创建、删除、修改、查看
一: 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活.所以触发器可以用来实现对表实施复杂的完整性约束. 二: SQL Server为每个触发器 ...
- grunt-connect-proxy解决开发时跨域问题
最近的项目中前后端是完全分离开发的,前端用grunt管理项目.这样就会导致一个问题:开发时前端调用后台的接口时因为不在一个服务器,所以会出现跨域问题.但是也不能用JSONP或CROS方式实现真正的跨域 ...
- STM32之输入捕获以及小小应用(库)
五一之际,先祝大家五一快乐.其实快乐很简单,工作的人有假放,学习的人也有假放,像我,有假放才有更多的时间学自己想学的东西.51假期学51,可惜没有32假期呀.好了..言归正传,大家听过吸星大法吧..在 ...
- sublime text 插件(前端自用)
一.软件安装 ST中文论坛:http://sublimetext.iaixue.com/ 或者 http://sublimetext.iaixue.com/dl/#sublime_text_3103 ...