WCF应用方法比较灵活,它可以帮助开发人员轻松打造一个跨平台的可依赖性的解决方案。那么如何才能正确的使用WCF开发呢?在这里我们将会为大家详细介绍一下WCF客户端的一些创建步骤。#t#
在这里我就用一个据于一个简单的场景:服务端为客服端提供获取客户信息的一个接口读取客户信息,来完成WCF开发入门的六个步骤。
WCF客户端创建步骤1. 定义WCF服务契约
A. 项目引用节点右键添加System.ServiceModel引用。
B. 在代码文件里,添加以下命名空间的引用
using System.ServiceModel;
using System;
C. 新建一个命为ICustomerService 接口,并添加一个获取客户信息的方法定义名为CustomerInfomation,返回字符串类型的客户信息。
D. 为接口ICustomerService添加ServiceContract的属性修饰使它成为WCF服务中公开的接口。
E. 为方法CustomerInfomation添加OperationContract的属性修饰使它成为WCF服务公开接口中公开的成员。
F. 代码:
using System;
using System.ServiceModel;
namespace ConWCF
{ [ServiceContract(Namespace =
"http://Microsoft.ServiceModel.Samples")]
public interface CustomerService
{
[OperationContract]
String CustomerInformation();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
WCF客户端创建步骤2. 实现WCF服务契约
实现WCF服务契约很简单,就是实现上一步聚定义的WCF服务契约定义的接口就可以。下面看代码
using System;
using System.ServiceModel;
namespace ConWCF
{ [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICustomerService
{
[OperationContract]
String CustomerInformation();
}
public class CustomerService:ICustomerService
{
#region ICustomerService 成员
public string CustomerInformation()
{
return "这是客户的信息!";
}
#endregion
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
WCF客户端创建步骤3. 启动WCF服务
A.添加一个应用程序配置文件,文件件名为App.config。
B.配置WCF服务的基本地址,如下所示
< host>
< baseAddresses>
< addbaseAddressaddbaseAddress="http://localhost:8000/conwcfr"/>
< /baseAddresses>
< /host>
- 1.
- 2.
- 3.
- 4.
- 5.
C.配置WCF服务的端口。Address=“”,意思就是使用上面配置的基本地址,当然也可以在这里指定。Bingding=“wsHttpBinding”,意思是WCF服务使用的是HTTP协议。再接下来就是配置WCF服务契约了(命名空间.服务契约接口名),如下所示:
< endpointaddressendpointaddress=""
binding="wsHttpBinding"
contract="ConWCF.ICustomerService" />
- 1.
- 2.
- 3.
D.配置文件
E.启动服服就简单了
ServiceHost host = new ServiceHost(typeof(CustomerService));
host.Open();
Console.WriteLine("客户信息服务已启动");
Console.WriteLine("按任意键结束服务!");
Console.Read();
host.Close();
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
F.当服务启动时,在IE栏中输入: http://localhost:8000/conwcfr,将会收到一些帮助的提示信息。
G.异常:配置文件中的服务名称一定是:命名空间.实现WCF服务契约类的名称,否则将会发生找到不配置的异常。
WCF客户端创建步骤4. 创建一个基本的WCF客服端
WCF服务端创建好啊,创建客户端就容易多了,直接用SVCUTIL 命令行工具去完成代码的生成。我安装了WINDOWS SDK,其带了一个CMDShell 命令行工具,打开后就可以运行SVCUTIL命令,这个命令是运行于 framework 3.0以上环境。查看详细帮助信息可以输入:svcutil /?,回车。
1. 启动上几步骤创建好的WCF服务端。
2. 在CMDShell工具中用CD 转到你要存放客户端代码的目录下,输入以下命令生成代码和配置文件。
WCF客户端创建步骤5. WCF客服端基本配置
WCF客户端配置就是配置调用WCF服务端的协议,输传宽带,服务地址,安全等等信息。下面就上一步骤命令自动生成的配置文件。
< ?xml version="1.0" encoding="utf-8"?>
< configuration>
< system.serviceModel>
< bindings>
< wsHttpBinding>
< binding name="WSHttpBinding_ICustomerService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
< readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
< reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
< security mode="Message">
< transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
< message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
< /security>
< /binding>
< /wsHttpBinding>
< /bindings>
< client>
< endpoint address="http://localhost:8000/conwcfr" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ICustomerService"
contract="ICustomerService"
name="WSHttpBinding_ICustomerService">
< identity>
< userPrincipalName value="30DA1D0B1D1E4D2\Administrator" />
< /identity>
< /endpoint>
< /client>
< /system.serviceModel>
< /configuration>
- 1.
- 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.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
WCF客户端创建步骤6. 使用WCF客户端
在客户端项目中项目引用节点右键添加System.ServiceModel引用. 添加第四部中创建的客户端代码文件和配置文件。 客户端调用服务端的服务,只要创建生成客户端类的实例就可调用了,但要确认服务端正在起用状态,如下
using System;
namespace ConWCFCustomerClient
{
class Program
{
static void Main(string[] args)
{
CustomerServiceClient client = new CustomerServiceClient();
string message=client.CustomerInformation();
Console.WriteLine(message);
Console.Read();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.