svc的参考文献_WCF服务三:svc文件详解

本文详细介绍了如何在IIS上部署WCF服务,包括新建WCF服务应用程序、svc文件的Service与CodeBehind属性、自定义ServiceHostFactory的使用,以及通过配置文件管理服务引用。通过示例展示了使用默认工厂和自定义工厂两种方式创建WCF服务,并提供了客户端调用的实现。此外,文章强调了修改服务类名时需使用重构以保持svc文件同步的重要性。
摘要由CSDN通过智能技术生成

在前面的文章中讲述过WCF服务的宿主程序主要包括:三种,在那篇文章中,简单的描述了如何把一个WCF服务寄宿到IIS上面,这篇文章中将具体讲述如何把一个WCF服务寄宿到IIS上面。

一、新建一个WCF服务应用程序:

文件->新建->项目:选择WCF下面的WCF服务应用程序

二、分析WcfSvcDemo项目,该项目的结构如下:

在该项目中,会默认生成一个IService1.cs的文件和Service1.svc文件。Service1.svc文件封装的就是提供给客户端的服务引用。

首先查看IService1.cs文件,从名字上面就可以看出这是一个接口文件,里面定义了一个接口IService1,接口上面使用了ServiceContract,意思是把这个接口声明为服务契约,服务契约是对客户端而言的,就是这个接口可以暴露出来让客户端可以看见。接口里面定义了两个方法,每个方法上面都使用了[OperationContract],意思是把这两个方法声明为操作契约。只有把接口里面的方法声明为操作契约,在客户端里面才可以看到相应的方法,否则在客户端里面看不到在接口里面定义的方法。

在来看Service.svc文件,可以看到下面有一个Service.svc.cs文件,这个文件里面定义了一个继承IService1接口的类Service1,并实现了IService1接口里面的方法。

删除Service.svc.cs文件,可以查看Service.svc文件,该文件里面就一行代码;

1

这里面有两个重要的参数:Service和CodeBehind。Service是属性值是WCF的服务实现类的完全限定名。CodeBehind是服务实现类所在的文件名。在运行的时候,宿主程序从svc文件中的Service属性得到WCF服务的完全限定名,然后从配置文件中找到同名的servicce,进而找到所有的EndPoint,并根据其属性进行实例化。

配置文件中的Service名字必须是Service类名的完全限定名(即Namespace.classname),EndPoint的Contract必须是Service接口的完全限定名。否则,程序就无法从程序集中找到相应的类进行加载。

注意:如果要修改接口实现类的名称,必须使用“重构”的方式进行修改,因为只有利用“重构”的方式修改Servie类名的时候,.svc文件里面Service的属性值才会被修改,利用其它方式修改类名,.svc文件里面Service的属性值会保留原值,这样在运行的时候,根据svc里面Service的属性值查找不到相应的类,程序就会报错。

svc文件里面还有一个重要的参数:ServiceHostFactory。ServiceHostFactory旨在解决从IIS或WAS中访问自定义ServiceHost的问题。因为从ServiceHost派生的自定义宿主是动态配置的并且可能为各种类型,所以宿主环境从不会直接将其实例化。相反,WCF使用工厂模式提供宿主环境和服务的具体类型之间的间接层。除非进行通知,否则它使用返回ServiceHost的实例的ServiceHostFactory的默认实现。(在新建的svc文件中默认实现就是CodeBehind属性的值)。但也可以通过在@ServiceHost指令中指定工厂实现的CLR类型名称来提供自己的工厂(用于返回派生宿主)。

下面是用于返回派生的ServiceHost的自定义ServiceHostFactory:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.ServiceModel;6 usingSystem.ServiceModel.Activation;7

8 namespacePublic.CustomService9 {10 public classCustomServiceHostFactory : ServiceHostFactory11 {12 protected overrideServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)13 {14 CustomServiceHost customServiceHost = newCustomServiceHost(serviceType, baseAddresses);15 returncustomServiceHost;16 }17 }18 }

其中CustomServiceHost是自定义的继承自ServiceHost的类,用于读取配置文件的配置,CustomServiceHost类的定义如下:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.ServiceModel;6 usingSystem.ServiceModel.Activation;7

8 namespacePublic.CustomService9 {10 public classCustomServiceHost :ServiceHost11 {12 public CustomServiceHost(Type serviceType, paramsUri[] baseAddresses)13 : base(serviceType, baseAddresses)14 {15 //加载Web.config的配置

16 log4net.Config.XmlConfigurator.Configure();17 }18 protected override voidApplyConfiguration()19 {20 base.ApplyConfiguration();21 }22 }23 }

若要使用此工厂,而不使用默认工厂,则应该在@ServiceHost指令中提供相应的类型名称:

1

其中Service是实现类的完全限定名,Factory是自定义ServiceHostFactory的完全限定名,Public是一个dll文件。

若要使用此工厂,而不使用默认工厂,则应该在@ServiceHost指令中提供相应的类型名称:

尽管对于从CreateServiceHost返回的ServiceHost可以执行什么操作没有技术限制,但建议您尽可能使工厂实现简单化。如果有大量的自定义逻辑,最好将这些逻辑放入宿主内而不是工厂内,以便可以重用它们。

应在这里提及另一个承载API的层。WCF还具有ServiceHostBase和ServiceHostFactoryBase,可从中分别派生ServiceHost和ServiceHostFactory。对于您必须通过自己的自定义创建来交换元数据系统的大型组件的更高级方案,存在上述这些特性。

下面通过两个具体的示例程序分别实现上面描述的默认工厂和自定义工厂。

三、使用默认工厂方式

删除新建项目时自动创建的IService1.cs和Service1.svc文件,然后添加一个svc文件,在项目上面右键->添加->新建项:

在新建项里面选择web里面的WCF服务,命名为MyService:

点“添加”,除了创建MyService.svc文件以外,还会自动创建一个名为IMyService.cs的接口文件,MyService.svc.cs里面的MyService默认实现IMyService接口.

删除IMyService接口里面自动生成的方法,添加一个GetCurrentTime的方法,用来返回当前的时间,IMyService接口定义如下:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Runtime.Serialization;5 usingSystem.ServiceModel;6 usingSystem.Text;7

8 namespaceWcfSvcDemo9 {10 //注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IMyService”。

11 [ServiceContract]12 public interfaceIMyService13 {14 ///

15 ///获取当前时间16 ///

17 ///

18 [OperationContract]19 DateTime GetCurrentTime();20 }21 }

4、MyService.svc.cs里面的MyService类实现IMyService接口,MyService类定义如下:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Runtime.Serialization;5 usingSystem.ServiceModel;6 usingSystem.Text;7

8 namespaceWcfSvcDemo9 {10 //注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“MyService”。11 //注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 MyService.svc 或 MyService.svc.cs,然后开始调试。

12 public classMyService : IMyService13 {14 ///

15 ///返回当前时间16 ///

17 ///

18 publicDateTime GetCurrentTime()19 {20 returnDateTime.Now;21 }22 }23 }

5、修改配置文件,增加service、binding等节点,修改后的配置文件如下:

1 <?xml version="1.0"?>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

42

43

44

主要是修改service节点里面的name是服务实现类的完全限定名,contract是服务接口的完全限定名。

6、把WCF服务部署到IIS上面

在IIS上面网站->添加网站:

配置网站名称、路径、IP地址和端口:

网站配置完成以后,浏览.svc文件,验证网站是否配置成功,如出现下面的截图,说明网站配置成功:

7、创建代理类

客户端引用WCF的时候一般是静态引用,直接添加服务引用,这种方式如果IP地址和端口号变了,需要用代码重新编译然后在部署,这样不方便。这里使用svcutil代理类的方式进行客户端的调用。

使用svcutil生成代理类:

新建一个项目,选择类库项目,把刚才生成的类文件添加到类库项目中,项目结构如下:

在类库项目中新添加一个类,命名为:MyServiceProxy,使用这个类来调用代理类,MyServiceProxy类的定义如下:

1 usingPublic.ConfigBinding;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Configuration;5 usingSystem.Linq;6 usingSystem.ServiceModel;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9

10 namespaceMyProxyService11 {12 public classMyServiceProxy13 {14 private staticMyServiceClient _databaseService;15

16 private staticMyServiceClient DatabaseService17 {18 get

19 {20 if (_databaseService == null)21 {22 string sApServer1 = ConfigurationManager.AppSettings["ApServer1"];23

24 if (sApServer1 == null)25 {26 _databaseService = newMyServiceClient();27 }28 else

29 {30 EndpointAddress endPointAddr = new EndpointAddress(string.Format("{0}/MyService.svc", sApServer1));31 _databaseService = newMyServiceClient(HttpBinding.BasicHttpBinding, endPointAddr);32 }33 }34

35 if (_databaseService.State ==CommunicationState.Faulted)36 {37 string sApServer2 = ConfigurationManager.AppSettings["ApServer2"];38

39 if (sApServer2 == null)40 {41 _databaseService = newMyServiceClient();42 }43 else

44 {45 EndpointAddress endPointAddr = new EndpointAddress(string.Format("{0}/MyService.svc", sApServer2));46 _databaseService = newMyServiceClient(HttpBinding.BasicHttpBinding, endPointAddr);47 }48 }49

50 return_databaseService;51 }52 }53

54 ///

55 ///返回当前时间56 ///

57 ///

58 public staticDateTime GetCurrentTime()59 {60 returnDatabaseService.GetCurrentTime();61 }62 }63 }

ApServer1和ApServer2是在配置文件中配置的IP地址和端口号,这样如果IP地址和端口号变了,只需要修改配置文件就可以。

GetCurrentTime()方法是调用的代理类里面的方法,把该方法定义为静态方法。

8、创建客户端调用

在解决方案中,新建一个winform程序,界面上面只有一个button按钮,点击按钮,弹出当前时间。需要添加对MyProxyService.dll文件的引用,在配置文件中增加ApServer1和ApServer2两个节点,配置文件如下:

1 <?xml version="1.0" encoding="utf-8"?>

2

3

4

5

6

7

8

9

10

button按钮事件代码如下:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;10 usingMyProxyService;11

12 namespaceWinformClient13 {14 public partial classForm1 : Form15 {16 publicForm1()17 {18 InitializeComponent();19 }20

21 private void btn_CurrentTime_Click(objectsender, EventArgs e)22 {23 DateTime dtNow =MyServiceProxy.GetCurrentTime();24 MessageBox.Show("当前时间:" +dtNow.ToString());25 }26 }27 }

点击按钮后,运行结果如下:

四、使用自定义工厂的方式

1、新添加一个WCF服务,命名为CustomService,把默认生成的CustomService.svc.cs文件删掉,重新添加一个类:CustomService,该类继承自生成的ICustomService接口,项目结构如下:

修改CustomService.svc文件:

1

CustomServiceHostFactory是在另外的Public.dll里面创建的工厂类,用来返回ServiceHost,Public.dll的项目结构如下:

CustomServiceHost类继承自ServiceHost类,代码如下:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.ServiceModel;6 usingSystem.ServiceModel.Activation;7

8 namespacePublic.CustomService9 {10 public classCustomServiceHost :ServiceHost11 {12 public CustomServiceHost(Type serviceType, paramsUri[] baseAddresses)13 : base(serviceType, baseAddresses)14 {15 //加载Web.config的配置

16 log4net.Config.XmlConfigurator.Configure();17 }18 protected override voidApplyConfiguration()19 {20 base.ApplyConfiguration();21 }22 }23 }

CustomServiceHostFactory是工厂类,继承自ServiceHostFactory,用来返回ServiceHost,代码如下;

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.ServiceModel;6 usingSystem.ServiceModel.Activation;7

8 namespacePublic.CustomService9 {10 public classCustomServiceHostFactory : ServiceHostFactory11 {12 protected overrideServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)13 {14 CustomServiceHost customServiceHost = newCustomServiceHost(serviceType, baseAddresses);15 returncustomServiceHost;16 }17 }18 }

HttpBinding代码如下:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.ServiceModel;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 usingSystem.Xml;8

9 namespacePublic.ConfigBinding10 {11 public classHttpBinding12 {13 private staticBasicHttpBinding _BasicHttpBinding;14

15 public staticBasicHttpBinding BasicHttpBinding16 {17 get

18 {19 if (_BasicHttpBinding == null)20 {21 _BasicHttpBinding = newBasicHttpBinding();22

23 //接收的讯息大小上限,默认值为65,536字节,24 //目前设定1k * 512,如果资料量大于这个值,请提出讨论,ex:8000笔资料大概128k

25 _BasicHttpBinding.MaxReceivedMessageSize = 400 * 8192 * 512;26

27 //由于回传String长度过长在反串行化时会出错!28 //所以放大最大字符串长度

29 _BasicHttpBinding.ReaderQuotas.MaxStringContentLength = 8192 * 1022;30 _BasicHttpBinding.ReaderQuotas.MaxArrayLength = 8192 * 1022;31 _BasicHttpBinding.SendTimeout = new TimeSpan(0, 5, 0);32

33 }34

35 return_BasicHttpBinding;36 }37 }38 }39 }

把CustomService.svc部署到IIS上面、创建代理类的方法、客户端调用和默认工厂里面的方法一样,此处不在描述。

项目代码下载路径:https://files.cnblogs.com/files/dotnet261010/WCFStudyDemo.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值