Program.cs
---------------------------------------------------------------
Main方法 里:
注释这句 (new Processing.LoopQuery()).Run(); //测试跑不成,暂时注释掉
---------Program.cs改过-----------
namespace HospitalMonitorServer
{
public class Program
{
private static Program s_instance;
public static Program GetInstance()
{
if(null == s_instance)
{
s_instance = new Program();
}
return s_instance;
}
public static void Main(string[] args)
{
Program program = GetInstance();
program.SetArgs(args);
program.Run();
}
public event Action<IServiceProvider> EventApplicationStarted;
public ILoggerFactory _ILogger;
private string[] m_arrayArgs;
private Program()
{
}
public void Run()
{
IWebHost host = BuildWebHost(m_arrayArgs);
ILoggerFactory loggerFactory = (ILoggerFactory)host.Services.GetService(typeof(ILoggerFactory));
_ILogger = loggerFactory;
ILogger<Notifications.WebSocketConnector> loggerWebSocketConnector = loggerFactory.CreateLogger<Notifications.WebSocketConnector>();
Notifications.WebSocketConnector websocket = Notifications.WebSocketConnector.GetInstance();
websocket.SetLogger(loggerWebSocketConnector);
websocket.Run();
// (new Processing.LoopQuery()).Run();
host.Run();
}
public void SetArgs(string[] arrayArgs)
{
m_arrayArgs = arrayArgs;
}
private IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(ConfigureServices)
.UseStartup<Startup>()
.Build();
private void ConfigureServices(IServiceCollection services)
{
if (null != EventApplicationStarted)
{
HostParams hostparams = new HostParams
{
delegateApplicationStarted = OnApplicationStarted
};
services.AddSingleton(hostparams);
}
}
private void OnApplicationStarted(object objState)
{
IServiceProvider provider = objState as IServiceProvider;
if(null == provider)
{
return;
}
EventApplicationStarted?.Invoke(provider);
}
}
class HostParams
{
public Action<object> delegateApplicationStarted;
}
}
---------Program.cs改前-------------
namespace HospitalMonitorServer
{
public class Program
{
public static event Action eventApplicationStarted;
public static ILoggerFactory _ILogger;
public static void Main(string[] args)
{
IWebHost host = BuildWebHost(args);
ILoggerFactory loggerFactory = (ILoggerFactory)host.Services.GetService(typeof(ILoggerFactory));
_ILogger = loggerFactory;
ILogger<Notifications.WebSocketConnector> loggerWebSocketConnector = loggerFactory.CreateLogger<Notifications.WebSocketConnector>();
Notifications.WebSocketConnector websocket = Notifications.WebSocketConnector.GetInstance();
websocket.SetLogger(loggerWebSocketConnector);
websocket.Run();
//(new Processing.LoopQuery()).Run(); 测试跑不成,暂时注释掉
host.Run();
}
private static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(ConfigureServices)
.UseStartup<Startup>()
.Build();
private static void ConfigureServices(IServiceCollection services)
{
if (null != eventApplicationStarted)
{
HostParams hostparams = new HostParams
{
delegateApplicationStarted = eventApplicationStarted
};
services.AddSingleton(hostparams);
}
}
}
class HostParams
{
public Action delegateApplicationStarted;
}
}
发现俊哥还改了如下地方
ServerFixture.cs 内容改为
-----------------------------------------------
namespace HospitalMonitorServer.Test
{
public class ServerFixture : IDisposable
{
public ServerFixture()
{
Program program = Program.GetInstance();
Semaphore semApplicationStarted = new Semaphore(0, 1);
program.EventApplicationStarted += (provider) =>
{
ServiceProvider = provider;
semApplicationStarted.Release();
};
Task.Run(() => program.Run());
semApplicationStarted.WaitOne();
}
void IDisposable.Dispose()
{
}
public IServiceProvider ServiceProvider { get; private set; }
}
}
原来没改的时候是
namespace HospitalMonitorServer.Test
{
class ServerFixture : IDisposable
{
public ServerFixture()
{
Semaphore semApplicationStarted = new Semaphore(0, 1);
Program.eventApplicationStarted += () => semApplicationStarted.Release();
Task.Run(() => Program.Main(new string[0]));
semApplicationStarted.WaitOne();
}
void IDisposable.Dispose()
{
}
}
[CollectionDefinition("Collection")]
public class TestCollection : ICollectionFixture<ServerFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
}
}
UnitTest1.cs部分
-----------------------------------------------------
using Microsoft.Extensions.DependencyInjection;
[Collection("Collection")]
public class UnitTest1 : IClassFixture<ServerFixture>
{
private ServerFixture m_fixture;
public UnitTest1(ServerFixture fixture)
{
m_fixture = fixture;
}
[Theory] //这个不写不会执行的
[InlineData("2020")] //意思是走两遍 2020是参数
[InlineData("2121")]
public void EmployeesControllerTest(string strEmpNo)
{
using (Permission.Controllers.EmployeesController controller = m_fixture.ServiceProvider.GetService<Permission.Controllers.EmployeesController>())
{
IActionResult resultAction;
resultAction = controller.GetName(strEmpNo);
if ("2121" == strEmpNo)
{
Assert.IsAssignableFrom<NotFoundObjectResult>(resultAction);
}
else if ("2020" == strEmpNo)
{
Assert.IsAssignableFrom<OkObjectResult>(resultAction);
}
if ("2020" != strEmpNo)
{
return;
}
resultAction = controller.Login(strEmpNo, strEmpNo);
Assert.IsAssignableFrom<OkObjectResult>(resultAction);
resultAction = controller.ChangePassword(strEmpNo, strEmpNo, "123");
Assert.IsAssignableFrom<OkResult>(resultAction);
resultAction = controller.Login(strEmpNo, strEmpNo);
BadRequestObjectResult resultBadRequest = Assert.IsAssignableFrom<BadRequestObjectResult>(resultAction);
Assert.Equal("用户名或密码不正确", resultBadRequest.Value.ToString());
resultAction = controller.Login(strEmpNo, "123");
Assert.IsAssignableFrom<OkObjectResult>(resultAction);
}
}
}
注意上方这一句:Permission.Controllers.EmployeesController controller = m_fixture.ServiceProvider.GetService<Permission.Controllers.EmployeesController>()
(这句如果有问题尝试引用下using Microsoft.Extensions.DependencyInjection;)
里面的GetService 怎么来的呢
(注意:这个文件夹的结构是这样的,也就是一开始是只有PermissionController,EmployeesController是我们自己加的)
在PermissionService.cs加了一句
问了俊哥这旁边的Action.cs是干嘛的,说是其实是类似controller的,controller是客户端要调用服务取数据的时候用的,而Action.cs是服务的其他模块要用本模块时要在Action.cs里面写方法,本质是类似的
namespace HospitalMonitorServer.Permission
{
class PermissionService : IService
{
void IService.Configure(IServiceCollection services)
{
services.AddScoped<Controllers.EmployeesController>();//就是在这里加了,如果以后加了新的controller,也要加一句
services.AddScoped<Controllers.PermissionController>();
services.AddSingleton<PermissionAction>();
services.AddTransient<IStartup, DAL.PermissionContext>();
services.AddTransient<DAL.PermissionContext>();
}
}
}