WCF Service Configuration Editor的使用
原文: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();
}
}
}
}
参考:
[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的使用的更多相关文章
- WCF中Service Configuration Editor的使用方法
1.在App.config文件上右击,选择Edit WCF Configuration.... 或者打开Program Files\Microsoft Visual Studio 8\Common7\ ...
- WCF Service 配置文件注释(转)
VS 2008 SP1(不确定是否不打SP1是否有)自带的一个编辑工具,可以更快的帮助定制配置文件, 以前看到过没有注意, 昨天正好一个同事提起, 这里记录一笔:打开VS 2008->Tools ...
- 如何创建一个RESTful WCF Service
原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html 转载请注明出处 (一)web.config文件 要创建REST WCF Service,endpoin ...
- 如何创建一个AJAX-Enabled WCF Service
原创地址:http://www.cnblogs.com/jfzhu/p/4041638.html 转载请注明出处 前面的文章中介绍过<Step by Step 创建一个WCF Servi ...
- 用JavaScript调用WCF Service
原创地址:http://www.cnblogs.com/jfzhu/p/4039604.html 转载请注明出处 前面介绍过<Step by Step 创建一个WCF Service>和& ...
- Step by Step 创建一个WCF Service
原创地址:http://www.cnblogs.com/jfzhu/p/4025448.html 转载请注明出处 (一)创建WCF Service (1)创建WCF Service类库 创建一个Cla ...
- WCF - Consuming WCF Service
WCF services allow other applications to access or consume them. A WCF service can be consumed by ma ...
- WCF - Hosting WCF Service
After creating a WCF service, the next step is to host it so that the client applications can consum ...
- 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 ...
随机推荐
- Linux Makefile文件编写详细步骤与实践
Linux Makefile文件编写详细步骤与实践 1.makefile概述 Windows环境下IDE会帮你完成makefile文件的编写,但在UNIX环境下你就必须自己写makefile了,会不会 ...
- 使用CocoaPods找不到头文件解决方法
Project->Info->Configurations
- 轻量级C语言实现的minixml解析库入门教程
svn上的minixml源码下载. svn co http://svn.msweet.org/mxml/tags/release-2.7/ 按照下载回来的源代码进行编译和安装.本教程只针对新手做一个 ...
- new到底做了什么?
下面是一个实例化自定义的对象,我们将要对他进行分析 //定义构造函数 function A(){ this.b = 1 //在这个对象里增加一个属性 //不可以拥有返回对象的return语句 } va ...
- asp.net BulletedList绑定数据及vs2013添加数据库文件
首先是在网页中添加一个BulletedList控件,通过编辑项来添加显示的数据. 这是一种添加数据的方式,另一种是通过绑定数据源来实现.在此之前,要先添加一个sql server数据库: 点开右键菜单 ...
- android webview点击返回键返回上一级activity
android webview点击返回键返回上一个activity @Override public boolean onKeyDown(int keyCode, KeyEvent event) { ...
- MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions -(亲测可用)
MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions - See more at: http://www.co ...
- iOS极光推送的基本使用
昨天花了一下午的时间研究了下极光推送,也前也是没做过,不知道从何下手!才开始的时候一看官方的SDK感觉好难,不过经过一系列的捣鼓之后,手机收到了推送信息,感觉其实并没有那么难! 1.配置开发证书(得有 ...
- 20151211--EL表达式语言
- php7 install memcached extension
#download source code package from git $ git clone https://github.com/php-memcached-dev/php-memcache ...