在本节,我们将介绍.NET WCF服务的创建过程。
- 在Solution Explorer中,右键单击TinyLibraryCQRS,然后选择Add | New Project…菜单,这将打开Add New Project对话框
- 在Installed Templates 选项卡下,选择Visual C# | WCF,然后选择WCF Service Application,确保所选.NET版本为.NET Framework 4,在Name文本框中输入TinyLibrary.Services,然后单击OK按钮
- 右键单击TinyLibrary.Services的References节点,然后选择Add Reference…菜单,这将打开Add Reference对话框
- 在.NET选项卡下,选择Apworks,Apworks.Bus.DirectBus以及Apworks.ObjectContainers.Unity,然后单击OK按钮
- 右键单击TinyLibrary.Services的References节点,然后选择Add Reference…菜单,这将打开Add Reference对话框
- 在Projects选项卡下,选择TinyLibrary.Events,TinyLibrary.EventHandlers,TinyLibrary.Commands,TinyLibrary.CommandHandlers,TinyLibrary.Domain以及TinyLibrary.QueryObjects,然后单击OK按钮
- 将自动生成的Service1.svc文件删掉
- 右键单击TinyLibrary.Services项目,然后单击Add | New Item…菜单,这将打开Add New Item对话框
- 在Installed Templates选项卡下,选择Visual C# | Web,然后选择WCF Service,在Name文本框中,输入CommandService然后单击Add按钮
- 编辑CommandService.svc.cs文件,输入如下代码
using System;using Apworks;using Apworks.Bus;using Apworks.Generators;using TinyLibrary.Commands;6:namespace TinyLibrary.Services8: {class CommandService : ICommandService10: {readonly ICommandBus bus = ObjectContainer.Instance.GetService<ICommandBus>();12:string name)14: {long)IdentityGenerator.Instance.Generate();new RegisterReaderCommand(id, loginName, name);17: bus.Publish(command);18: bus.Commit();return id;20: }21:int pages)23: {long)IdentityGenerator.Instance.Generate();new CreateBookCommand(id,false);27: bus.Publish(createBookCommand);28: bus.Commit();return id;30: }31:long bookId)33: {new BorrowBookCommand(readerId, bookId);35: bus.Publish(borrowBookCommand);36: bus.Commit();37: }38:long bookId)40: {new ReturnBookCommand(readerId, bookId);42: bus.Publish(returnBookCommand);43: bus.Commit();44: }45: }46: } - 用上面相同的方法创建一个QueryService.svc,然后编辑QueryService.svc.cs文件,输入如下代码
using System;using System.Collections.Generic;using Apworks;using Apworks.Queries.Storage;using Apworks.Storage;using TinyLibrary.QueryObjects;7:namespace TinyLibrary.Services9: {class QueryService : IQueryService11: {private IQueryObjectStorage GetQueryStorage()13: {return ObjectContainer.Instance.GetService<IQueryObjectStorage>();15: }16:long id)18: {this.GetQueryStorage())20: {new PropertyBag();, id);return queryObjectStorage.SelectFirstOnly<BookObject>(pb);24: }25: }26:long id)28: {this.GetQueryStorage())30: {new PropertyBag();, id);return queryObjectStorage.SelectFirstOnly<ReaderObject>(pb);34: }35: }36:string loginName)38: {this.GetQueryStorage())40: {new PropertyBag();, loginName);return queryObjectStorage.SelectFirstOnly<ReaderObject>(pb);44: }45: }46:public IEnumerable<BookObject> GetAllBooks()48: {this.GetQueryStorage())50: {return queryObjectStorage.Select<BookObject>();52: }53: }54:long bookId)56: {this.GetQueryStorage())58: {new PropertyBag();, bookId);61:new PropertyBag(););return qos.Select<RegistrationObject>(pbCriteria, pbSort, Apworks.Storage.SortOrder.Descending);65: }66: }67:68:long readerId)70: {this.GetQueryStorage())72: {new PropertyBag();, readerId);75:new PropertyBag(););return qos.Select<RegistrationObject>(pbCriteria, pbSort, Apworks.Storage.SortOrder.Descending);79: }80: }81: }82: } - 右键单击TinyLibrary.Services项目,然后选择Add | New Item…菜单,这将打开Add New Item对话框
- 在Installed Templates选项卡下,选择Visual C# | Web,然后选择Global Application Class,然后单击Add按钮
- 修改Global.asax.cs文件如下
using System;using Apworks.Application;3:namespace TinyLibrary.Services5: {class Global : System.Web.HttpApplication7: {readonly IBootStrapper bootStrapper = BootStrapperFactory.Create();9:object sender, EventArgs e)11: {12: bootStrapper.BootStrap();13: }14:// ... other methods omitted here.16: }17: }
Global.asax文件中定义了,当应用程序启动时,同时会启动Apworks系统。首先,Apworks.Application.BootStrapperFactory类创建了启动实例,然后使用BootStrap方法来初始化应用程序。这个过程是在Apworks的配置文件中定义的,将在后续文章中讨论。