/// <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(); } }
private static void Main() { System.Data.Services.DataServiceHost host = null; try { host = new System.Data.Services.DataServiceHost(typeof(MenuDataService), new[] { new Uri(ServiceAddress) }); host.Open(); Console.WriteLine("service running"); Console.WriteLine("Press return to exit"); Console.ReadLine(); } catch (CommunicationException communicationEx) { Console.WriteLine(communicationEx.Message); } finally { if (host != null && host.State == CommunicationState.Opened) { host.Close(); } } }