【返回导航】
在简单了解了Orleans 之后我们可以通过几个例子去加深印象
这个例子也是跟着《Microsoft Orleans 之 入门指南》(https://www.cnblogs.com/endv/p/6147976.html)这篇文章做的
上篇笔记中的连个例子说明了开发的具体步骤以及需要引用的类库,还有客户端如何去调用,接下来的例子是如何去使用创建相应的单元测试,
这次我们通过Orleans Tools for Visual Studio去创建Orleans 的项目
首先使用模板先创建
Sample.Interfaces1
Sample.Implements1
Server1
三个项目,具体代码参考上篇笔记的第二个例子。
接下来我们创建一个单元测试程序库 Sample.Test1
引用项目Sample.Implements1、Sample.Interfaces1
引用NuGet包 Microsoft.Orleans.TestingHost
项目结构如下:
我们创建一个测试类就叫UserServiceTest,继承自Orleans 测试库TestingSiloHost
测试代码如下:
namespace Sample.Test { [TestClass] public class UserServiceTest: TestingSiloHost { [ClassCleanup] public static void ClassCleanup() { // Optional. // By default, the next test class which uses TestignSiloHost will // cause a fresh Orleans silo environment to be created. StopAllSilosIfRunning(); } [TestMethod] public async void TestExist() { var grain = GrainFactory.GetGrain<IUserService>(10); bool bo = await grain.Exist("18612478956"); Assert.IsTrue(bo); } } }
记得增加两个配置文件,
ClientConfigurationForTesting.xml
<?xml version="1.0" encoding="utf-8" ?> <ClientConfiguration xmlns="urn:orleans"> <Gateway Address="localhost" Port="40000"/> <!-- To turn tracing off, set DefaultTraceLevel="Off" and have no overrides. For the trace log file name, {0} is replaced by "Client" and {1} is the current time. --> <Tracing DefaultTraceLevel="Info" TraceToConsole="false" TraceToFile="{0}-{1}.log" BulkMessageLimit="1000"> <TraceLevelOverride LogPrefix="Runtime" TraceLevel="Info" /> <TraceLevelOverride LogPrefix="Application" TraceLevel="Info" /> <TraceLevelOverride LogPrefix="AssemblyLoader" TraceLevel="Warning" /> </Tracing> <Statistics MetricsTableWriteInterval="300s" PerfCounterWriteInterval="30s" LogWriteInterval="300s" WriteLogStatisticsToTable="true" StatisticsCollectionLevel="Info"/> <Messaging ResponseTimeout="30s" ClientSenderBuckets="8192" MaxResendCount="0"/> </ClientConfiguration>
OrleansConfigurationForTesting.xml
<?xml version="1.0" encoding="utf-8"?> <OrleansConfiguration xmlns="urn:orleans"> <Globals> <StorageProviders> <Provider Type="Orleans.Storage.MemoryStorage" Name="MemoryStore" /> <Provider Type="Orleans.Storage.MemoryStorage" Name="Default" /> <!--<Provider Type="Orleans.Storage.AzureTableStorage" Name="AzureStore"/>--> </StorageProviders> <SeedNode Address="localhost" Port="22222"/> <Messaging ResponseTimeout="30s"/> </Globals> <Defaults> <Networking Address="localhost" Port="22222"/> <ProxyingGateway Address="localhost" Port="40000" /> <Tracing DefaultTraceLevel="Info" TraceToConsole="false" TraceToFile="{0}-{1}.log" PropagateActivityId="false" BulkMessageLimit="1000"> <TraceLevelOverride LogPrefix="Application" TraceLevel="Info" /> <!-- <TraceLevelOverride LogPrefix="Runtime.Dispatcher" TraceLevel="Verbose" /> <TraceLevelOverride LogPrefix="AssemblyLoader.Silo" TraceLevel="Warning" /> --> </Tracing> <Statistics MetricsTableWriteInterval="30s" PerfCounterWriteInterval="30s" LogWriteInterval="300s" WriteLogStatisticsToTable="true" StatisticsCollectionLevel="Info"/> </Defaults> </OrleansConfiguration>
更改xml文件属性为:如果较新则复制
测试项目结构如下:
按照文档里面所述,测试项目的就已经编写完成了,直接可以开始测试,但是当我调试test的时候,却导致了一个错误,如下图所示:
这个问题没找到解决方案,下面将直接对官方文档以及官方文档上提供的教程开始学习。