频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

WCF开发简简单单的六个步骤

来源:博客园 作者:旋风 出处:巧巧读书 2008-03-18 进入讨论组
上一页 1 2 3 

讨论组http://group.qqread.com

G.异常:配置文件中的服务名称一定是:命名空间.实现WCF服务契约类的名称,否则将会发生找到不配置的异常。

name="ConWCF.CustomerService"

异常信息: Service 'ConWCF.CustomerService' has zero application (non-infrastructure) endpoints.

This might be because no configuration file was found for your application, or because no

service

element matching the service name could be found in the configuration file, or because

no endpoints were defined in the service

element.

这个异常搞得我昏了半天,害得我以为从IIS、端口到配置环境排除错误,就是搞不明白为什么会跟类的命称联系起来。不过,最终也解决了。

4、创建一个基本的WCF客服端

WCF服务端创建好啊,创建客户端就容易多了,直接用SVCUTIL 命令行工具去完成代码的生成。我安装了WINDOWS SDK,其带了一个CMDShell 命令行工具,打开后就可以运行SVCUTIL命令,这个命令是运行于 framework 3.0以上环境。查看详细帮助信息可以输入:svcutil /?,回车。

1、启动上几步骤创建好的WCF服务端。

2、在CMDShell工具中用CD 转到你要存放客户端代码的目录下,输入以下命令生成代码和配置文件。

D:"client>svcutil /language:c# /out:CustomerClient.cs /config:app.config http:/

/localhost:8000/conwcfr

上面命令指定了要生成代码的语言,代码文件和配置文件名,WCF服务端地址,注意运行命令时必须确定WCF服务端正在运行中。

5、WCF客服端基本配置

WCF客户端配置就是配置调用WCF服务端的协议,输传宽带,服务地址,安全等等信息。下面就上一步骤命令自动生成的配置文件。

 1 <?xml version="1.0" encoding="utf-8"?>
2 <configuration>
3   <system.serviceModel>
4     <bindings>
5       <wsHttpBinding>
6         <binding name="WSHttpBinding_ICustomerService" closeTimeout="00:01:00"
7           openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
8           bypassProxyOnLocal="false" transactionFlow="false"
               hostNameComparisonMode="StrongWildcard"
9           maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
10           messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
11           allowCookies="false">
12           <readerQuotas maxDepth="32" maxStringContentLength="8192"
                maxArrayLength="16384"
13             maxBytesPerRead="4096" maxNameTableCharCount="16384" />
14           <reliableSession ordered="true" inactivityTimeout="00:10:00"
15             enabled="false" />
16             <security mode="Message">
17                <transport clientCredentialType="Windows" proxyCredentialType="None"
18                   realm="" />
19                <message clientCredentialType="Windows" negotiateServiceCredential="true"
20                   algorithmSuite="Default" establishSecurityContext="true" />
21                </security>
22              </binding>
23             </wsHttpBinding>
24         </bindings>
25         <client>
26             <endpoint address="http://localhost:8000/conwcfr" binding="wsHttpBinding"
27                 bindingConfiguration="WSHttpBinding_ICustomerService"
                     contract="ICustomerService"
28                 name="WSHttpBinding_ICustomerService">
29                 <identity>
30                     <userPrincipalName value="30DA1D0B1D1E4D2\Administrator" />
31                 </identity>
32             </endpoint>
33         </client>
34     </system.serviceModel>
35 </configuration>

6、使用WCF客户端

在客户端项目中项目引用节点右键添加System.ServiceModel引用。

添加第四部中创建的客户端代码文件和配置文件。

客户端调用服务端的服务,只要创建生成客户端类的实例就可调用了,但要确认服务端正在起用状态,如下: 

1using System;
2
3namespace ConWCFCustomerClient
4
5{
6
7    class Program
8
9    {
10
11        static void Main(string[] args)
12
13        {
14
15            CustomerServiceClient client = new CustomerServiceClient();
16
17            string message=client.CustomerInformation();
18
19            Console.WriteLine(message);
20
21            Console.Read();
22
23        }
24
25    }
26
27}

Demo下载

更多文章 更多内容请看Wlan组网----家庭专题专题,或进入讨论组讨论。
上一页 1 2 3 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章