2. 添加一个 WCF service 新项,系统自动会创建 Service.svc、App_Code\Service.cs 等必要文件。
3. 在 Service.cs 文件中完成服务编码。
4. 添加 web.config 文件,并在其位置单击鼠标右键,打开 "Microsoft Service Configuration Editor" 工具完成服务配置。
5. 注意添加 serviceMetadata,否则我们使用浏览器无法查看,也无法创建客户端代理。
web.config 演示 (注意配置文件中并没有提供服务地址)
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="MyService">
<endpoint binding="wsHttpBinding" contract="IMyService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true">
</system.web>
</configuration>
service.svc<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="MyService">
<endpoint binding="wsHttpBinding" contract="IMyService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true">
</system.web>
</configuration>
<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/service.cs" %>
建议将服务放到一个单独的 Library 中,这样更改宿主环境会更方便一点,不过上述步骤要做些修改。1. 安装完 VS Extension 后,我们可以创建一个 WCF service 的网站项目。
2. 添加服务所在类库的引用。
3. 创建 svc 文件,内容也所不同。如 <%@ ServiceHost Debug="true" Service="Learn.Library.WCF.CalculateService" %>,注意使用包含名字空间的全名。
4. 添加 web.config 文件,打开 "Microsoft Service Configuration Editor" 工具完成服务配置。
5. 注意添加 serviceMetadata,否则我们使用浏览器无法查看,也无法创建客户端代理。
web.config
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="Learn.Library.WCF.CalculateService">
<endpoint binding="wsHttpBinding" contract="Learn.Library.WCF.ICalculate"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true">
</system.web>
</configuration>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="Learn.Library.WCF.CalculateService">
<endpoint binding="wsHttpBinding" contract="Learn.Library.WCF.ICalculate"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true">
</system.web>
</configuration>