我尝试通过Autofac在Hub中注入我的服务,但我没有取得成功。 我使用了这个autofac doc:http://autofaccn.readthedocs.io/en/latest/integration/signalr.html
AdminHub代码:
public class AdminHub : Hub
{
private readonly IUserSerivce _userService;
public AdminHub(IUserSerivce userService)
{
_userService = userService;
}
引导程序代码:
public class Bootstrapper
{
public static void Run()
{
SetAutofacContainerWebApi();
SetAutofacContainerSignalR();
}
private static void SetAutofacContainerWebApi()
{
var builder = new ContainerBuilder();
// Get your HttpConfiguration.
var config = GlobalConfiguration.Configuration;
// Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerRequest();
builder.RegisterAssemblyTypes(typeof(BondService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(BondRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();
// Set the dependency resolver to be Autofac.
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
private static void SetAutofacContainerSignalR()
{
var builder = new ContainerBuilder();
// Register your SignalR hubs.
builder.RegisterHubs(Assembly.GetExecutingAssembly());
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerRequest();
builder.RegisterAssemblyTypes(typeof(UserService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();
// Set the dependency resolver to be Autofac.
var container = builder.Build();
GlobalHost.DependencyResolver = new AutofacDependencyResolver(container);
}
}
Global.asax代码:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
App_Start.Bootstrapper.Run();
}
}
启动代码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration { };
map.RunSignalR(hubConfiguration);
});
}
}
用户服务代码:
public interface IUserService
{
bool UpdateUser(int UserID, string ConnectionID);
}
public class UserService : IUserService
{
private readonly IUserRepository _repository;
private readonly IUnitOfWork _unitOfWork;
public UserService(IUserRepository repository, IUnitOfWork unitOfWork)
{
_repository = repository;
_unitOfWork = unitOfWork;
}
public bool UpdateUser(int UserID, string ConnectionID)
{
try
{
var EFUser = _repository.GetById(UserID);
if (EFUser != null)
{
if (EFUser.ConnectionID != ConnectionID)
{
EFUser.ConnectionID = ConnectionID;
}
}
EFUser.ConnectionID = ConnectionID;
_unitOfWork.Commit();
return true;
}
catch
{
return false;
}
}
}
如何在我的Hub中注入服务? debbug没有进入AdminHub
函数,如果我从public AdminHub(IUserSerivce userService)
删除userService,那么一切正常,我看到了debbug。