您的位置:首页 > 其它

一个简单的WCF程序创建过程

2007-12-29 09:43 543 查看
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>引用预编译程序集的服务类.

<% @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>
3.客户端配置和实现.

创建控制台程序作为测试客户端程序.

using System;

2using System.Collections.Generic;

3using System.Text;

4

5namespace WCFCon

6{

7 class Program

8 {

9 static void Main(string[] args)

10 {

11 localhost.MyServiceClient wcf = new WCFCon.localhost.MyServiceClient();

12 Console.WriteLine("请输入数据:");

13 string str = Console.ReadLine();

14 Console.WriteLine("你输入的数据是:{0}", wcf.MyOperation1(str));

15 localhost.DataContract1 da = new WCFCon.localhost.DataContract1();

16 Console.WriteLine("请输入FirstName:");

17 da.FirstName = Console.ReadLine();

18 Console.WriteLine("请输入LastName:");

19 da.LastName = Console.ReadLine();

20 Console.WriteLine("FirstName+LastName={0}", wcf.MyOperation2(da));

21 }

22 }

23}
运行结果输出如下.



至此,一个简单的WCF创建演示过程结束.其它详细请参阅MSDN.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: