原文:http://www.cnblogs.com/Ming8006/p/3772221.html

通过WCF Service Configuration Editor的配置
修改Client端

参考

在上篇文章创建一个简单的WCF程序中, 通过编码的方式进行终结点的添加和服务行为的定义,但在进行真正的WCF应用开发时,一般会直接是通过配置的方式进行。

对于初学者来说,WCF的配置显得过于复杂,直接对配置文件进行手工编辑不太现实。在这种情况下,可以直接使用VS提供的配置工具WCF Service Configuration Editor工具生成XML文件来进行WCF的配置。

通过WCF Service Configuration Editor的配置[1]

以下是使用WCF Service Configuration Editor的的操作步骤:

1.打开VS,在Hosting项目中右键,新建一个App.config文件。

2.点击菜单栏Tools选项,在下拉菜单中选择WCF Service Configuration Editor。

3.在弹出的工具窗口中选择“File->open->Config File”。找到刚才建的App.config文件,并打开。

4.新建一个服务,如下图所示,先点击“创建新的服务”链接,再找到Service项目中的WcfServices.Services.CalculatorService服务。

5.点击下一步,找到Contracts项目中的ICalculator契约。

6.下一步,选择Http的通信方式。

7.点击下一步,选择Basic Web Service Interoperability。

8.点击下一步,输入服务端Endpoint地址: http://localhost:8080/calculatorservice 。下一步Finish。

9.为服务添加行为(Behavior),这步很重要。在Advanced目录下,右键新建一个Service行为,New Service Behavior Configuraton,然后对行为重命名为CalculatorBehavior。新建一个Stack Element 'serviceMetadata', 并设置它的HttpGetEnabled为true。如下图所示:

10.这些做好了之后,我们回到最上面的Service目录,为Calculator服务添加刚才配的CalculatorBehavior行为配置。如下图所示:

11.接着配置Host的地址,选中Host,然后点击右下方的New Base Address,输入: http://localhost:8080/calculatorservice

12.可以新添加一个服务端的Endpoint,用于配置WS-MetadataExchange,当然也可以不加。在Services目录下的Endpoint右键,新建一个Endpoint,名字和地址随意,保证Binding是mexHttpBinding。

13.Ctrl+S保存,这样App.config文件就自动写满了,如下:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="CaclulaterBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="CaclulaterBehavior" name="WcfServices.Services.CalculatorService">
<endpoint address="http://localhost:8080/calculatorservice" binding="basicHttpBinding"
bindingConfiguration="" contract="WcfServices.Contracts.ICalculator" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/calculatorservice" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

14。修改Hosting类的代码,删改后如下:

 using System;
using System.ServiceModel;
using WcfServices.Services; namespace WcfServices.Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Opened += delegate
{
Console.WriteLine("CalculaorService have started, press any key to stop it!");
}; host.Open();
Console.Read();
}
}
}
}

注意

1 <endpoint address="" binding="basicHttpBinding" name="Calculator"   Contract="Contracts.ICalculator" >
2 <host>
3 <baseAddress>
4 <add baseAddress="http://localhost:8080/CalculatorService"/>
5 </baseAddress>
6 </host>

endpiont的地址为空,只是配了Host的基地址。当然也可以直接配Endpoint的地址,不配Host的基地址。但如果host了多个服务呢?有多了Endpoint挂在同一个host下,那么配基地址就显得很重要。

修改Client端


返回

打开客户端的项目(服务端不要关闭),选择Client项目下的Service Reference,在你的服务命名空间上右键,点击Update Service Reference。会生成新的app.config文件。

然后Rebuild Client端项目即可,ClientApp的代码无须改变。

配置客户端[2]

在真正的WCF应用中,大都采用配置的方式进行终结点的定义。对于客户端,我们也可以通过下面的配置指定终结点的三要素,并为相应的终结点指定一个终结点配置名称(calculatorservice)。 在客户端项目添加Application Configuration file (App.config),内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8080/calculatorservice" binding="basicHttpBinding" contract="WcfServices.Contracts.ICalculator" name="calculatorservice" />
</client>
</system.serviceModel>
</configuration>

对客户端项目,我们将添加的服务引用移除,并为Client项目添加对Contracts项目的引用。借助于这个服务契约,并通过ChannelFactory<ICalculator>创建服务代理对象,直接进行相应的服务调用。

using System;
using System.ServiceModel;
using WcfServices.Contracts; namespace WcfServices.Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
{
ICalculator proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", , , proxy.Add(, ));
}
Console.Read();
}
}
}
}

源代码:SimpleWCFWithConfig.zip

参考:

[1] WCF Service Configuration Editor的使用  http://www.cnblogs.com/judastree/archive/2012/08/26/2657555.html

[2] 我的WCF之旅(1):创建一个简单的WCF程序  http://www.cnblogs.com/artech/archive/2007/06/14/782845.html

WCF Service Configuration Editor的使用的更多相关文章

  1. WCF中Service Configuration Editor的使用方法

    1.在App.config文件上右击,选择Edit WCF Configuration.... 或者打开Program Files\Microsoft Visual Studio 8\Common7\ ...

  2. WCF Service 配置文件注释(转)

    VS 2008 SP1(不确定是否不打SP1是否有)自带的一个编辑工具,可以更快的帮助定制配置文件, 以前看到过没有注意, 昨天正好一个同事提起, 这里记录一笔:打开VS 2008->Tools ...

  3. 如何创建一个RESTful WCF Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html 转载请注明出处 (一)web.config文件 要创建REST WCF Service,endpoin ...

  4. 如何创建一个AJAX-Enabled WCF Service

      原创地址:http://www.cnblogs.com/jfzhu/p/4041638.html 转载请注明出处   前面的文章中介绍过<Step by Step 创建一个WCF Servi ...

  5. 用JavaScript调用WCF Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4039604.html 转载请注明出处 前面介绍过<Step by Step 创建一个WCF Service>和& ...

  6. Step by Step 创建一个WCF Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4025448.html 转载请注明出处 (一)创建WCF Service (1)创建WCF Service类库 创建一个Cla ...

  7. WCF - Consuming WCF Service

    WCF services allow other applications to access or consume them. A WCF service can be consumed by ma ...

  8. WCF - Hosting WCF Service

    After creating a WCF service, the next step is to host it so that the client applications can consum ...

  9. Fixing common issues when hosting a .NET 4.0 WCF service in IIS 7

    http://sandrinodimattia.net/fixing-common-issues-when-hosting-a-net-4-0-wcf-service-in-iis-7/ Until ...

随机推荐

  1. ACM学习-POJ-1004-Financial Management

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1003-Financial Management Financial Management Time Limi ...

  2. javascript实现小九九乘法口诀

    <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...

  3. 用代码场景Spine人物

    在使用Spine动画的时候可能会需要用代码创建,所以就小小的研究了一下 /// <summary> /// 加载一个spine的骨骼动画 /// </summary> /// ...

  4. VB.NET的反射机制

    1.前提 Net的应用程序由几个部分:‘程序集’.‘模块’.‘类型’组成. 装配件是.Net应用程序执行的最小单位,编译出来的.dll..exe都是装配件. 2.概念 反射是获得运行时类型的方式. 概 ...

  5. [android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found

    google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展.可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了.之前导4.0 ...

  6. javascript高级知识分析——灵活的参数

    代码信息来自于http://ejohn.org/apps/learn/. 使用数量可变的参数对编程很有好处 function merge(root){ for(i = 0 ; i < argum ...

  7. some knowledge

    注意 关于cornerstone无法上传library文件的问题  上面是我要添加的library文件,网上提供的方法是 在CornerStone的菜单栏里面 View->ShowIgnoreI ...

  8. Qt 控件

    一.布局管理器 QHBoxLayout 水平布局 QVBoxLayout 垂直布局 QGridLayout 格点布局 QFormLayout 关联布局 QSplitter 分裂器 Spacers 间隔 ...

  9. Module 模式 以及 揭示模式。

    ---恢复内容开始--- Module模式 : 在传统软件工程中为类提供私有和公有封装的方法. 在js中: Module 模式 使用闭包封装 私有状态和组织. 该模式,返回一个公有的API,而其他的一 ...

  10. PHP中mktime() 函数对于日期运算和验证

    mktime() 函数对于日期运算和验证非常有用.它可以自动校正越界的输入: // 语法:mktime(hour,minute,second,month,day,year) echo(date('Y- ...