2.创建服务主机
创建WEB宿主做服务主机,用来侦听客户端提交的请求,并创建用于处理那些请求的服务类的实例.
<1>代码掩藏文件方式的服务类.
创建成功后如下图所示意.
在App_Code下含有一个带事例代码的Service.cs文件.(本过程直接采用现有代码).Service.svc文件相当于WebService的.asmx(本过程此处不做修改).在Web.config中添加一行
1
<serviceMetadata httpGetEnabled="true"/>
完整的Web.config文件内容如下.
1
<?xml version="1.0"?>
2
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
3
<system.serviceModel>
4
<services>
5
<!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
6
<service name="WCFServiceLibrary.service1" behaviorConfiguration="returnFaults">
7
<endpoint contract="WCFServiceLibrary.IService1" binding="wsHttpBinding"/>
8
</service>
9
</services>
10
<behaviors>
11
<serviceBehaviors>
12
<behavior name="returnFaults" >
13
<serviceDebug includeExceptionDetailInFaults="true" />
14
<serviceMetadata httpGetEnabled="true" />
15
</behavior>
16
</serviceBehaviors>
17
</behaviors>
18
</system.serviceModel>
19
<system.web>
20
<compilation debug="true"/>
21
</system.web>
22
</configuration>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<2>引用预编译程序集的服务类.
添加对服务类的引用.
采用此种方式,可以删除App_Code下的Service.cs文件.修改Service.svc文件内容如下:
1
<% @ServiceHost Language=C# Debug="true" Service="WCFServiceLibrary.service1" %>
同时Web.confing同步修改如下:
1
<?xml version="1.0"?>
2
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
3
<system.serviceModel>
4
<services>
5
<!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
6
<service name="WCFServiceLibrary.service1" behaviorConfiguration="returnFaults">
7
<endpoint contract="WCFServiceLibrary.IService1" binding="wsHttpBinding"/>
8
</service>
9
</services>
10
<behaviors>
11
<serviceBehaviors>
12
<behavior name="returnFaults">
13
<serviceDebug includeExceptionDetailInFaults="true"/>
14
<serviceMetadata httpGetEnabled="true"/>
15
</behavior>
16
</serviceBehaviors>
17
</behaviors>
18
</system.serviceModel>
19
<system.web>
20
<compilation debug="true">
21
<assemblies>
22
<add assembly="System.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
23
<add assembly="Microsoft.Transactions.Bridge, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
24
<add assembly="SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
25
<add assembly="System.IdentityModel.Selectors, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
26
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
27
<add assembly="System.Web.RegularExpressions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
28
<add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
29
<add assembly="System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
30
<add assembly="System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
31
</system.web>
32
</configuration>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
3.客户端配置和实现.
创建控制台程序作为测试客户端程序.
添加服务引用后如下.
客户端做简单的如下编程.
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace WCFCon
6
}
运行结果输出如下.2
3
4
5
6
至此,一个简单的WCF创建演示过程结束.其它详细请参阅MSDN.