用VS 2008开发WCF(一)——最快速的WCF入门

本文详细介绍如何在Visual Studio 2008中创建和运行WCF服务,包括项目设置、代码实现及服务引用添加步骤,适合初学者快速入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步,打开VS 2008,然后新建一个项目,项目使用WCF类型,具体选择“WCF类库”。

什么都不用改,直接设置新建好了的WCF类库项目为启动项目,Ctrl+F5开始运行。

什么?类库不能直接运行?你且试试。

系统托盘会出现一个WCF服务主机的小图标,点击,查看这个WCF项目被分配为什么访问路径。

这样我们就新建好了一个WCF服务,其中的代码应该是默认的。

 

IService1.cs

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibrary { // 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。 [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // 任务: 在此处添加服务操作 } // 使用下面示例中说明的数据协定将复合类型添加到服务操作 [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } }

 

Service1.cs

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibrary { // 注意: 如果更改此处的类名“IService1”,也必须更新 App.config 中对“IService1”的引用。 public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } } }

 

App.config

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <!-- 部署服务库项目时,必须将配置文件的内容添加到 主机的 app.config 文件中。System.Configuration 不支持库的配置文件。--> <system.serviceModel> <services> <service name="WcfServiceLibrary.Service1" behaviorConfiguration="WcfServiceLibrary.Service1Behavior"> <host> <baseAddresses> <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary/Service1/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否则地址将与上面提供的基址相关 --> <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary.IService1"> <!-- 部署时,应删除或替换下列标识元素,以反映 在其下运行部署服务的标识。删除之后,WCF 将 自动推导相应标识。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元数据交换终结点由服务用于向客户端做自我描述。--> <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WcfServiceLibrary.Service1Behavior"> <!-- 为避免泄漏元数据信息, 请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> <serviceMetadata httpGetEnabled="True"/> <!-- 要接收故障异常详细信息以进行调试, 请将下值设置为 true。在部署前 设置为 false 以避免泄漏异常信息--> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>

 

下面我们新建一个WindowsConsole项目(方便而已,你完全可以新建一个其它的,只要能输出结果就行)

在项目上右击,添加服务引用。

注:必须最低是.NET 3.0项目才会出现此选项,如果发现没有,请查看是否该项目创建版本低于3.0了

服务引用的地址就是刚才系统默认分配的地址

添加完成后,在代码中调用下面的代码,即可看到调用成功了

IService1 serv = new Service1Client(); Console.WriteLine(serv.GetData(2));

 

根本不需要特意写一个HOST,因为VS已经提供WCF的服务主机了。

先跨进WCF的门槛,然后再慢慢研究如果托管,不是更好吗。

转载于:https://www.cnblogs.com/vanpan/archive/2009/02/11/3583045.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值