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 ...
随机推荐
- #include <windows.h>
1 FindWindowA 2 keybd_event 3 malloc 4 MessageBox 5 MessageBoxA 6 MessageBoxW 7 mouse_event 8 SetC ...
- 怎样在小方框上打对号 小方框内打对勾 word 方框打对勾
在word中做选择时,非常多人遇到须要在小方框上打对勾而不知怎样做,现将可行的各种方法总结例如以下: 1:直接找到一个做好的,保存为图片,在须要的时候插入它: 2:插入文本框,然后边框选择为实线,在文 ...
- uva 10306 - e-Coins(完全背包)
题目链接:10306 - e-Coins 题目大意:给出m和s, 再给出m种电子硬币,每种硬币有两种金额xi,yi.现在要在m种硬币种选若干个硬币,可以重复选同一种硬币, 使得(x1 + x2 + . ...
- java -Xms -Xmx -XX:PermSize -XX:MaxPermSize
java -Xms -Xmx -XX:PermSize -XX:MaxPermSize 在做java开发时尤其是大型软件开发时经常会遇到内存溢出的问题,比如说OutOfMemoryError ...
- poj1160
题目大意:在一个一维坐标轴上有v个(1<=v<=300)村庄,要建p(1<=p<=30)个邮局,每个村庄都到最近的邮局,要求最小的距离和. 四边形不等式,据说黑书上写得很高 ...
- (转)IE劫持原理 BHO
为什么“浏览器劫持”能够如此猖狂呢?放眼众多论坛的求助贴,我们不时可以看到诸如“我的IE被主页被改了,我用杀毒工具扫了一遍都没发现病毒,我把主页改回自己的地址,可是一重启它又回来了!”.“我的系统一开 ...
- Java学习之网络编程实例
转自:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616413.html 多谢分享 网络编程 网络编程对于很多的初学者来说,都是很向往的一 ...
- mysql 索引创建规则
1.表的主键.外键必须有索引:2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引: 5 ...
- Linux下为何都是文件的理解
所谓“文件”,就是在我们的电脑中,以实现某种功能.或某个软件的部分功能为目的而定义的一个单位. Linux都是以文件的形式存在,当我们访问某个文件(Linux中的文件有目录,连接,普通文本),由于Li ...
- Javascript 字符串浏览器兼容问题
先看下不兼容的写法,若我想获取某个字符串的第几位 var str='aavvvcc'; console.info(str[0]); 这种写法 在IE 7以下的浏览器都不兼容,以下提供浏览器全兼容的方式 ...