在本节,我们将介绍.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文件,输入如下代码
1: using System;2: using Apworks;3: using Apworks.Bus;4: using Apworks.Generators;5: using TinyLibrary.Commands;6:7: namespace TinyLibrary.Services8: {9: public class CommandService : ICommandService10: {11: private readonly ICommandBus bus = ObjectContainer.Instance.GetService<ICommandBus>();12:13: public long CreateReader(string loginName, string name)14: {15: long id = (long)IdentityGenerator.Instance.Generate();16: RegisterReaderCommand command = new RegisterReaderCommand(id, loginName, name);17: bus.Publish(command);18: bus.Commit();19: return id;20: }21:22: public long CreateBook(string title, string publisher, DateTime pubDate, string isbn, int pages)23: {24: long id = (long)IdentityGenerator.Instance.Generate();25: CreateBookCommand createBookCommand = new CreateBookCommand(id,26: title, publisher, pubDate, isbn, pages, false);27: bus.Publish(createBookCommand);28: bus.Commit();29: return id;30: }31:32: public void BorrowBook(long readerId, long bookId)33: {34: BorrowBookCommand borrowBookCommand = new BorrowBookCommand(readerId, bookId);35: bus.Publish(borrowBookCommand);36: bus.Commit();37: }38:39: public void ReturnBook(long readerId, long bookId)40: {41: ReturnBookCommand returnBookCommand = new ReturnBookCommand(readerId, bookId);42: bus.Publish(returnBookCommand);43: bus.Commit();44: }45: }46: } -
用上面相同的方法创建一个QueryService.svc,然后编辑QueryService.svc.cs文件,输入如下代码
1: using System;2: using System.Collections.Generic;3: using Apworks;4: using Apworks.Queries.Storage;5: using Apworks.Storage;6: using TinyLibrary.QueryObjects;7:8: namespace TinyLibrary.Services9: {10: public class QueryService : IQueryService11: {12: private IQueryObjectStorage GetQueryStorage()13: {14: return ObjectContainer.Instance.GetService<IQueryObjectStorage>();15: }16:17: public BookObject GetBook(long id)18: {19: using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())20: {21: var pb = new PropertyBag();22: pb.Add("AggregateRootId", id);23: return queryObjectStorage.SelectFirstOnly<BookObject>(pb);24: }25: }26:27: public ReaderObject GetReader(long id)28: {29: using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())30: {31: var pb = new PropertyBag();32: pb.Add("AggregateRootId", id);33: return queryObjectStorage.SelectFirstOnly<ReaderObject>(pb);34: }35: }36:37: public ReaderObject GetReaderByLogin(string loginName)38: {39: using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())40: {41: var pb = new PropertyBag();42: pb.Add("LoginName", loginName);43: return queryObjectStorage.SelectFirstOnly<ReaderObject>(pb);44: }45: }46:47: public IEnumerable<BookObject> GetAllBooks()48: {49: using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())50: {51: return queryObjectStorage.Select<BookObject>();52: }53: }54:55: public IEnumerable<RegistrationObject> GetBookRegistrationHistory(long bookId)56: {57: using (IQueryObjectStorage qos = this.GetQueryStorage())58: {59: PropertyBag pbCriteria = new PropertyBag();60: pbCriteria.Add("BookAggregateRootId", bookId);61:62: PropertyBag pbSort = new PropertyBag();63: pbSort.AddSort<DateTime>("RegistrationDate");64: return qos.Select<RegistrationObject>(pbCriteria, pbSort, Apworks.Storage.SortOrder.Descending);65: }66: }67:68:69: public IEnumerable<RegistrationObject> GetReaderRegistrationHistory(long readerId)70: {71: using (IQueryObjectStorage qos = this.GetQueryStorage())72: {73: PropertyBag pbCriteria = new PropertyBag();74: pbCriteria.Add("ReaderAggregateRootId", readerId);75:76: PropertyBag pbSort = new PropertyBag();77: pbSort.AddSort<DateTime>("RegistrationDate");78: 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文件如下
1: using System;2: using Apworks.Application;3:4: namespace TinyLibrary.Services5: {6: public class Global : System.Web.HttpApplication7: {8: private readonly IBootStrapper bootStrapper = BootStrapperFactory.Create();9:10: protected void Application_Start(object sender, EventArgs e)11: {12: bootStrapper.BootStrap();13: }14:15: // ... other methods omitted here.16: }17: }
Global.asax文件中定义了,当应用程序启动时,同时会启动Apworks系统。首先,Apworks.Application.BootStrapperFactory类创建了启动实例,然后使用BootStrap方法来初始化应用程序。这个过程是在Apworks的配置文件中定义的,将在后续文章中讨论。
本文详细介绍了如何使用.NETWCF创建服务,并通过实现命令与查询分离来增强系统的可维护性和可扩展性。具体步骤包括创建项目、引用依赖、生成服务文件、实现命令和服务接口等。
460

被折叠的 条评论
为什么被折叠?



