Microsoft Robotics Studio声明一个通用服务(Generic Service Declaration)

 

这篇文章演示怎样定义一个通用服务协议(generic service contract),通用服务协议类似面向对象编程中的接口,它只提供规范,而不提供具体实现,服务开发者将会为这些规范提供具体的实现,服务使用者不用去关心不同的服务的具体的实现,因为这些服务有相同的状态和操作,也就是通用服务协议所定义的状态和操作。

虽然上面的这个通用协议类似于接口,但是和接口又有很大的不同,这个在Service Tutorial 9 (C#) - Implementing and Extending Service Contracts,会看到。

这个实例由C#写的,你可以在下面的目录中找到它

Samples"ServiceTutorials"Tutorial8"CSharp

第一步:创建项目

DssNewService工具或安装了msrsvs2008创建一个服务项目,在这个项目中我们只需要ServiceTutorial8Types.cs文件,而不需要Servicetutorial8.cs文件,这里只有服务状态和操作的定义,即所说的协议(Contract),如果想用这些定义,可以实现一个新的服务,在另外一个服务中使用这里定义协议。

第二步:设置服务声明属性

AssemblyInfo.cs文件中,使用ServiceDeclaration 属性向DSS基础架构指示这个程序集包含一个通用服务的协议而没有实现。

 

[assembly: ServiceDeclaration(DssServiceDeclaration.DataContract)]

 

很有可能在一个程序集中既有通用协议的声明又有这个服务的实现,前提是他们定义在不同的clr命名空间中,你可以用下面的方式向DSS指示。

“这里命名空间的限制有些不可思议,一个DSS项目中如果有多个服务,DSS要求这些服务必须在不同的命名空间中。”

 

[assembly: ServiceDeclaration(DssServiceDeclaration.DataContract|DssServiceDeclaration.ServiceBehavior)]

 

关于ServiceDeclaration的枚举类型的详细信息见下表:

Member name

Description

NonDssService

Not a Dss Service :程序集中没有DSS服务

ServiceBehavior

Assembly contains service behavior declarations including the port types used to describe message patterns and types Replaces [ServiceBehaviorDeclaration]

程序集包含服务的实现包括端口允许的操作,包括对消息体和消息类型的描述。

DataContract

Assembly contains data contract declarations. Replaces [ServiceDataDeclaration]

程序集包括数据协议(通信时的消息体)的描述。

Proxy

Assembly is a generated Dss Proxy

Transform

Assembly is a generated Dss Transform

第三步:定义通用服务协议(Contract

这个服务的协议和其他的服务的协议没有什么不同,这里的定义域如下:


 1 /// <summary>
 2 /// Generic Service without service implementation
 3 /// </summary>
 4 [DisplayName("Service Tutorial 8: Generic Service Contract")]
 5 [Description("This is a generic contract without an actual service implementation. See Service Tutorial 9 for various ways to use this contract.")]
 6 [DssServiceDescription("http://msdn.microsoft.com/library/bb727256.aspx")]
 7 public sealed class Contract
 8 {
 9     /// <summary>
10     /// The Dss Service contract
11     /// </summary>
12     [DataMember()]
13     public const String Identifier = "http://schemas.tempuri.org/2007/08/servicetutorial8.html";
14 }
15 
第四步:定义通用服务的状态(State)

同样,服务的状态和其他的服务业没什么区别,记得用DataContract, DataMember DataMemberConstructor去标记这个类。

 1 /// <summary>
 2 /// State for the generic service
 3 /// </summary>
 4 [DataContract]
 5 [DisplayName("Generic Service State")]
 6 [Description("Specifies the state of the generic service.")]
 7 public class GenericState
 8 {
 9     string _firstName;
10     string _lastName;
11  
12     [DataMember]
13     [Description("Specifies the first name of a person.")]
14     [DisplayName("First Name")]
15     public string FirstName
16     {
17         get { return _firstName; }
18         set { _firstName = value; }
19     }
20  
21     [DataMember]
22     [DisplayName("Last Name")]
23     [Description("Specifies the last name of a person.")]
24     public string LastName
25     {
26         get { return _lastName; }
27         set { _lastName = value; }
28     }
29 }
30  
31 

第五步:定义通用服务的端口操作(Operations

呵呵,和前面一样,不多说,如下所示,我们定义了GetReplace操作。


 1 /// <summary>
 2 /// Generic Service Main Operations Port
 3 /// </summary>
 4 [ServicePort]
 5 public class GenericServiceOperations :
 6     PortSet<DsspDefaultLookup, DsspDefaultDrop, Get, Replace>
 7 {
 8 }
 9  
10 /// <summary>
11 /// Get Operation
12 /// </summary>
13 [Description("Gets the current state.")]
14 public class Get : Get<GetRequestType, PortSet<GenericState, Fault>>
15 {
16 }
17  
18 /// <summary>
19 /// Replace Operation
20 /// </summary>
21 [Description("Replaces the current state.")]
22 public class Replace : Replace<GenericState, PortSet<DefaultReplaceResponseType, Fault>>
23 {
24 }
25 
第六步:编译

和其他服务的编译一样,编译这个项目会得到一个dll文件,因为没有服务的实现,所有不会生成一个服务,在Service Tutorial 9 (C#) - Implementing and Extending Service Contracts中我们会看到使用不同的方法去实现和扩展这个通用的服务协议。

这个是Service Tutorial 9 (C#) 中提到的Service Tutorial 8 (C#) 还好,文章很短,不过有几个值得注意的地方,比如关于命名空间的限制.......

转自:http://www.elooog.cn/post/64.html

原文地址:https://www.cnblogs.com/hongyin163/p/1488987.html