原创地址: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. 执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”

    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for ...

  2. 多栏多列布局(display:flex)

    display:flex 多栏多列布局浏览器支持情况:火狐直接支持w3c无前缀写法,谷歌和opera支持-webkit- 前缀写法,比较适合移动端开发使用, display:flex 这个牛逼的css ...

  3. JavaScript(四) Window窗体操作

    window: 属性(值或者子对象):opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器打开的,则opener是null,可以利用这个属性来关闭源窗口. 方法(函数):事件(事先设置好的 ...

  4. TAQSkinScrollBar 类美化滚动条再讨论

    再说:TAQSkinScrollBar 类美化滚动条,http://www.138soft.com/?p=156  里面有人提到不可以滚动 滚动的改善方法: unit AQSkinScrollBar; ...

  5. Win7下VS2008破解方法

    在Win7系统下,无法像xp下通过“控制面板”卸载的方法重新输入序列号来破解VS2008. 但可以通过以下几个步骤来破解: 1.首先需要安装VS2008,可以安装VS2008专业版90天试用版或VS2 ...

  6. 词法分析程序(C)

    #include <stdio.h> #include <string.h> ],word[],ch; int type,p,i,n,sum; ]={"begin&q ...

  7. WinForm开发笔记

    Button Button默认不产生DoubleClick事件 (MSDN说明:https://msdn.microsoft.com/zh-cn/library/system.windows.form ...

  8. yotaku的开发日志(1)

    2015-12-18 21:17:46 连续看了几天的ThinkPHP框架,目前看到基于角色的用户访问权限控制. 相关代码如下: 数据库 用户表(管理员) mg_id mg_name mg_pwd m ...

  9. mysql中生产表格多列统计问题

    for Example: select date_format(date,'%Y-%m-%d') as day, count(case when xinghao='a' then 1 end) as  ...

  10. Node学习思维导图

    如果看不清楚图片上的内容,右键保存图片或新窗口打开.