在VS2010里建立一个最简单的WCF服务,基本流程如下:
一:新建WCF应用
首先,新建一个WCF服务的应用(这里以framework 4.0为例),如下图所示,
二、WCF应用中的契约(Contract)
[ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [Operation Contract] CompositeType GetDataUsingDataContract(CompositeType composite); // Todo: Add your service operations here } // Use a data contract as illustrated in the sample below to add composite types to service operations. [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 {string Value = value; } } }
可以看到,声明了服务契约IService1,以接口形式声明,其中还包括两个操作契约GetData以及GetDataUsingDataContract。还声明了数据契约CompositeType,以类的形式声明,包含两个数据成员BoolValue和StringValue。
三、WCF应用中的服务功能实现(ServiceBehavior)
在生成的WCF应用中,Service1.svc.cs中为
“服务功能实现(ServiceBehavior)”
(本例中契约和服务放在同一个工程下了,实际上也可以分为两个工程),代码如下(Service1.svc还有其它作用,后面再说):
public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}",value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite== null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue+= "Suffix"; } return composite; } }
可以看到,这个Service1类实现了在契约中声明的IService1接口(服务契约),也使用到了CompositeType类(数据契约);实现了GetData以及GetDataUsingDataContract这两个服务契约中功能,这些功能即为WCF服务允许外部程序进行调用的功能。
四、寄宿(Host)WCF服务
有2种常见的寄宿方式:
1)一种是为一组WCF服务创建一个托管的应用程序,通过手工启动程序的方式对服务进行寄宿,所有的托管的应用程序均可作为WCF服务的宿主,比如 Console应用、Windows Forms应用和ASP.NET应用等,我们把这种方式的服务寄宿方式称为自我寄宿(Self Hosting)。
2)另一种则是通过操作系统现有的进程激活方式为WCF服务提过宿主,Windows下的进程激活手段包括IIS、Windows Service或者WAS(Windows Process Activation Service)等
无论采用哪种寄宿方式,在为某个服务创建 ServiceHost的过程中,WCF框架内部会执行一系列的操作,其中最重要的步骤就是为服务创建服务描述(Service Description)
在WinForm工程中添加对要托管的WCF服务的描述,既可以通过代码实现,也可以通过工具生成。本例在VS2010下,通过Tools-WCF Service Configeration Editor工具生成。
这个界面中要选择使用的契约,默认即可,点击下一步,弹出如下界面:
这个界面中要选择使用的网络通讯协议,选择最通用的HTTP协议,并在下一个界面中选择基本的Http服务(Basic WebService interoperablitity),
在接下来的界面中,输入访问地址,其它程序可以通过这个地址来访问本WCF服务。
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="NewBehavior0"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8585/wcf1/Metadata" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="NewBehavior0" name="WcfService1.Service1"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="ep1" contract="WcfService1.IService1" /> <host> <baseAddresses> <add baseAddress="http://localhost:8585/wcf1" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
private void button1_Click(objectsender,EventArgs e) { System.ServiceModel.ServiceHosthost =new System.ServiceModel.ServiceHost(typeof(WcfService1.Service1)); host.open(); this.label1.Text= "opened"; }
编译运行,点击界面中的Button控件,即可以将该WCF服务寄宿到该Frm中(关闭窗体后WCF服务结束)。
注:如果运行时报这个错误(HTTP Could not register URL http://+:8585/wcf1/. Your process does not have access rights to this namespace),有可能是权限不足导致,需要使用管理员权限运行。
五、在客户端访问并调用WCF服务
新建一个WinFrm工程(不要关闭刚才的寄宿界面),在VS菜单中点击Project-Add Service Reference..项,输入刚才在寄宿界面中定义的地址,就可以添加对该WCF服务的引用,如下图:
ServiceReference1.Service1Clientaa=newServiceReference1.Service1Client();
MessageBox.Show(aa.GetData(2));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。