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是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
随机推荐
- MR操作
MR操作————Map.Partitioner.Shuffle.Combiners.Reduce 1.Map步骤 1.1 读取输入文件,解析成k-v对,其中每个k-v对调用一次map函数 1.2 写自 ...
- google开发者工具调试技巧
http://blog.sina.com.cn/s/blog_60a4fcef0102v3vt.html
- MIT 6.828 JOS学习笔记10. Lab 1 Part 3: The kernel
Lab 1 Part 3: The kernel 现在我们将开始具体讨论一下JOS内核了.就像boot loader一样,内核开始的时候也是一些汇编语句,用于设置一些东西,来保证C语言的程序能够正确的 ...
- bing的简单英文字典工具
今天看到园友心白水撰写的<简单翻译工具--必应字典第三方API使用方法>,感觉很不错,所以用Python也写了一个.源码如下: import urllib.request import j ...
- BZOJ2109: [Noi2010]Plane 航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频 发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此, 小X表示很不满意. 在这次来烟台的 ...
- 谷歌浏览器如何查看或获取Cookie字符串
注:此博客仅供非web开发人员查看,以下内容都基于谷歌浏览器. 在网页空白处点击鼠标右键,在弹出菜单中选择[审查元素],可以看到网页下方出现审查元素相关界面. 在审查元素相关界面,点击[Network ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- winform下如何实现右下角弹窗效果
[DllImport("user32")] private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, in ...
- RequireJS与SeaJS模块化加载示例
web应用越变的庞大,模块化越显得重要,尤其Nodejs的流行,Javascript不限用于浏览器,还用于后台或其他场景时,没有Class,没有 Package的Javascript语言变得难以管理, ...
- 奇怪的UnexpectedRollbackException异常
今天在使用一个原来常用的功能的时候,突然发现在某些场景下会报异常,内容如下: 通过断点调试发现一路都很顺畅,就是在从controller层返回前段的时候会报该异常,没办法,只能通过排除法定位问题,后来 ...