IIS8托管WCF服务
WCF服务程序本身不能运行,需要通过其他的宿主程序进行托管才能调用WCF服务功能,常见的宿主程序有IIS,WAS,Windows服务,当然在学习WCF技术的时候一般使用控制台应用程序或WinForm程序进行托管。本文将详细介绍如何使用IIS8托管WCF服务程序以及解决可能会碰到的一些问题。步骤比较多,还需耐心看完!
1.本机器是Win8操作系统,默认没有安装IIS。安装IIS8很简单,具体步骤是:控制面板→程序和功能→启用或关闭Windows功能,勾选Internet信息服务节点下的部分功能,
如图所示:

2.安装完毕,重启系统后,在浏览器中输入 http://localhost/,即可看见IIS8界面,表示安装成功,如图:

3.启用WCF服务中的HTTP激活功能,具体步骤是:控制面板→程序和功能→启用或关闭Windows功能,勾选“.Net Framework 4.5 高级服务”节点下的部分功能,
如图所示:

1.新建解决方案“IISHostWCF”,添加“WCF服务类库”项目,命名为“WCFService”,如图

该WCF服务的功能很简单,根据参数Id获取相应的价格,代码如下:
1)服务接口代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel; namespace WCFService
{
[ServiceContract]
public interface IGetPrice
{
[OperationContract]
string GetPriceByProductId(int id);
}
}
2)实现接口的服务类代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WCFService
{
public class GetPrice : IGetPrice
{
public string GetPriceByProductId(int id)
{
string price = "0.00";
switch (id)
{
case :
price = "12.34";
break;
case :
price = "45.60";
break;
case :
price = "78.99";
break;
default:
price = "100.00";
break;
}
return price;
}
}
}
2.在解决方案中,添加→新建网站,选择“WCF服务”,命名为“WCFWebSite”,如图

1)把新生成的IService.cs和Service.cs文件删除
2)添加在第一个步骤里新建的WCF服务类库,WCFService.dll

3).修改Service.svc文件

4)使用“WCF服务配置编辑器”编辑web.config文件


web.config代码如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="WCFServiceBehavior" name="WCFService.GetPrice">
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration=""
name="basicEndPoint" contract="WCFService.IGetPrice" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
name="mexEndPoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8002/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>
5)发布“WCF服务网站”





1.打开IIS管理器,添加网站,如图

2.添加完网站后,右键菜单→管理网站→浏览,弹出页面,选择浏览“Service.svc”文件,
打开后,发现会报错:

经过查找资料,原来是IIS8默认没有添加处理svc文件的处理程序,需手动添加:
1)添加MIME类型
文件扩展名:.svc;MIME类型:application/octet-stream。


2)添加处理程序映射
请求路径: *.svc;
类型:System.ServiceModel.Activation.HttpHandler;
名称:svc-Integrated


添加完毕后,重新启动网站,再次浏览即可成功:

1.通过VS自带的WCF测试工具
打开“VS2012开发人员命令提示”工具,输入“wcftestclient”,即可打开“WCF测试客户端”:


添加WCF服务测试地址:http://localhost:8001/Service.svc/mex

测试结果如下:

2.新建控制台客户端来测试WCF服务,添加服务引用

以下是简单的测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
WCFGetPrice.GetPriceClient proxy = new WCFGetPrice.GetPriceClient("basicEndPoint");
Console.WriteLine(proxy.GetPriceByProductId());
Console.ReadKey();
}
}
}
结果如下:

至此使用IIS托管WCF服务应用程序详细步骤完毕。
好困啊~~~
IIS8托管WCF服务的更多相关文章
- Azure开发者任务之七:在Azure托管服务中托管WCF服务角色
在一个托管服务中托管一个WCF服务角色和托管一个ASP.Net Web Role基本类似. 在上一篇文章中,我们学习了如何使用WCF Service Web Role. 在本文中,我会对上一篇文章进行 ...
- 在IIS8添加WCF服务支持
最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF ...
- 【IIS8】在IIS8添加WCF服务支持
最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF ...
- WCF入门(七)---自托管消费WCF服务
费自托管WCF服务的整个过程,一步步地解释以及充足的编码和屏幕截图是非常有必要. 第1步:服务托管,现在我们需要实现的代理类客户端.创建代理的方式不同. 使用svcutil.exe,我们可以创建代理类 ...
- 让IIS8支持WCF的最简单方法
以前在IIS8中使用WCF时,总是参考在IIS8添加WCF服务支持这篇博文进行手工设置: 1. 首先添加MIME类型:扩展名“.svc”,MIME类型 “application/octet-strea ...
- WCF服务编程 读书笔记——第1章 WCF基础(1)
第1章 WCF基础 本章主要介绍WCF的基本概念.构建模块以及WCF体系架构,以指导读者构建一个简单的WCF服务.从本章的内容中,我们可以了解到WCF的基本术语,包括地址(Address).绑定(Bi ...
- 翻译:使用 CoreWCF 升级 WCF 服务到 .NET 6
翻译:使用 CoreWCF 升级 WCF 服务到 .NET 6 原文地址:https://devblogs.microsoft.com/dotnet/upgrading-a-wcf-service-t ...
- IIS 中托管基于TCP绑定的WCF服务
IIS 中托管基于TCP绑定的WCF服务 一.创建一个基于TCP绑定的WCF服务 1.创建一个的简单的服务具体代码如下 服务契约定义 namespace SimpleService { // 注意: ...
- IIS8.5 的环境下添加配置WCF服务!!!!!
添加步骤: 1.打开iis8.5,先部署wcf服务. 2.首先添加MIME类型 扩展名:“.svc” MIME类型:“application/octet-stream” 3.添加 处理程序映射 请求路 ...
随机推荐
- BT9034: 仅 IE 和 Opera 支持 HTMLFrameElement 和 HTMLIFrameElement 的 document 属性
标准参考 根据 DOM-2 中的描述,HTMLFrameElement 和 HTMLIFrameElement 都没有 'document' 属性. 关于 HTMLFrameElement 对象的详细 ...
- 取消界面的title
在setContentView(R.layout.activity_main)方法上面添加代码(继承Activity的写法): requestWindowFeature(Window.FEATURE_ ...
- ORMLiteDatabase的简单使用并且与其他的表相互联系
1.下载jar 具体到Android,需要在 http://ormlite.com/releases 页面下载两个jar 包(本文基于ORMLite的版本是:ormlite 4.49-SNAPSHOT ...
- delphi TFileStream.create
Value Meaning fmCreate Create a file with the given name. If a file with the given name exists, op ...
- 一个封装好的CSV文件操作C#类代码
using System.Data; using System.IO; namespace DotNet.Utilities { /// <summary> /// CSV文件转换类 // ...
- jquery.validate新的写法(jquery.validate1.13.js)
<script src="../js/jquery.js"></script> <script src="../js/jquery.vali ...
- IOS下载资源zip到本地然后读取
思路是 1.ios下载服务器上的zip资源包(图片,声音等经过zip压缩的资源包)到本地 2.解压zip到程序目录 3.从程序目录加载资源文件 一.下载zip资源 [cpp]-(NSString*)D ...
- Python学习教程(learning Python)--3.3.2 Python的关系运算
如果if的condition不用布尔表达式来做条件判断而采用关系表达式,实际上关系表达式运算的结果要么是True要么是False.下面我们先了解一些有关关系运算符的基础知识,如下表所示. 做个小程序测 ...
- wpf 获取DataGrid某一个单元格,设置此单元格ToolTip内容和背景颜色
public void GetCell() { for (int i = 0; i < this.datagrid1.Items.Count; i++) ...
- 做自己的ORMapping Framework ---- 前序
做一个应用系统,当然大多情况都会对数据库进行操作,什么样的model设计更加合理,怎样的数据库操作更有效率,什么样的额代码结构更好维护等等这些问题相信一定会困扰大多企业级系统开发的小伙伴们. 鉴于我正 ...