/// <summary> /// Initializes the service. /// </summary> public override void Initialize() { if (CheckIsInitialized) { return; } try { InitializeDisposeLock.EnterWriteLock(); if (CheckIsInitialized) { return; } ServiceBehavior behavior = new ServiceBehavior(Protocol.Http, Transport.EnableSsl); behavior.SetBehavior(ServiceHost); ServiceHost.Open(); SubscribeToDataRequests(); } finally { InitializeDisposeLock.ExitWriteLock(); } }
/// <summary> /// Registers a <see cref="IDataServiceBase{T}" />. /// All <see cref="DataRequest" />s are automatically subscribed to. /// Other <see cref="Broker" />s can access the service by publishing <see cref="DataRequest" />s. /// </summary> /// <typeparam name="T"></typeparam> public void RegisterDataService <T>() where T : IDataServiceBase <DbContext>, new() { if (_isServiceRegistered) { throw new DataContextException("A dataservice is already registered with this context."); } try { InitializeDisposeLock.EnterWriteLock(); if (_isServiceRegistered) { throw new DataContextException("A dataservice is already registered with this context."); } IDataServiceBase <DbContext> dataService = Activator.CreateInstance(typeof(T)) as IDataServiceBase <DbContext>; if (dataService == null) { throw new DataContextException( string.Format("Could not create an instance of the given '{0}' of type '{1}'.", typeof(IDataServiceBase <DbContext>).Name, typeof(T).AssemblyQualifiedName())); } _serviceType = dataService.GetType(); _dbContextType = TypeHelper.GetGenericArgumentOfBaseType(dataService.GetType(), typeof(DbContext)); if (_dbContextType == null) { throw new DataContextException( string.Format("Could not determine the '{0}' type on the given '{1} of type '{2}'.", typeof(DbContext).Name, typeof(IDataServiceBase <DbContext>).Name, typeof(T).AssemblyQualifiedName())); } _dbContext = Activator.CreateInstance(_dbContextType) as DbContext; ServiceHost = new System.Data.Services.DataServiceHost(_serviceType, new[] { new Uri(_peer.Node.Contact.DataContextUrl()) }); _isServiceRegistered = true; } finally { InitializeDisposeLock.ExitWriteLock(); } }