Exemplo n.º 1
0
        /// <summary>
        /// Create the rest service
        /// </summary>
        public RestService CreateService(Type serviceType)
        {
            try
            {
                // Get the configuration
                var sname  = serviceType.GetCustomAttribute <ServiceBehaviorAttribute>()?.Name ?? serviceType.FullName;
                var config = this.m_configuration.Services.FirstOrDefault(o => o.Name == sname);
                if (config == null)
                {
                    throw new InvalidOperationException($"Cannot find configuration for {sname}");
                }
                var retVal = new RestService(serviceType);
                foreach (var bhvr in config.Behaviors)
                {
                    if (bhvr.Type == null)
                    {
                        throw new InvalidOperationException($"Cannot find service behavior {bhvr.XmlType}");
                    }
                    retVal.AddServiceBehavior(
                        bhvr.Configuration == null ?
                        Activator.CreateInstance(bhvr.Type) as IServiceBehavior :
                        Activator.CreateInstance(bhvr.Type, bhvr.Configuration) as IServiceBehavior);
                }
                var demandPolicy = new OperationDemandPolicyBehavior(serviceType);

                foreach (var ep in config.Endpoints)
                {
                    var se = retVal.AddServiceEndpoint(new Uri(ep.Address), ep.Contract, new RestHttpBinding());
                    foreach (var bhvr in ep.Behaviors)
                    {
                        if (bhvr.Type == null)
                        {
                            throw new InvalidOperationException($"Cannot find endpoint behavior {bhvr.XmlType}");
                        }

                        se.AddEndpointBehavior(
                            bhvr.Configuration == null ?
                            Activator.CreateInstance(bhvr.Type) as IEndpointBehavior :
                            Activator.CreateInstance(bhvr.Type, bhvr.Configuration) as IEndpointBehavior);
                        se.AddEndpointBehavior(demandPolicy);
                    }
                }
                return(retVal);
            }
            catch (Exception e)
            {
                Tracer.GetTracer(typeof(RestServiceFactory)).TraceError("Could not start {0} : {1}", serviceType.FullName, e);
                throw new Exception($"Could not start {serviceType.FullName}", e);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create the rest service
        /// </summary>
        public RestService CreateService(Type serviceType)
        {
            try
            {
                // Get the configuration
                var configuration = ApplicationServiceContext.Current.GetService <IConfigurationManager>().GetSection <AgsConfigurationSection>();
                var sname         = serviceType.GetCustomAttribute <ServiceBehaviorAttribute>()?.Name ?? serviceType.FullName;
                var config        = configuration.Services.FirstOrDefault(o => o.Name == sname);
                if (config == null)
                {
                    throw new InvalidOperationException($"Cannot find configuration for {sname}");
                }
                var retVal = new RestService(serviceType);
                foreach (var bhvr in config.Behaviors)
                {
                    retVal.AddServiceBehavior(
                        bhvr.Configuration == null ?
                        Activator.CreateInstance(bhvr.Type) as IServiceBehavior :
                        Activator.CreateInstance(bhvr.Type, bhvr.Configuration) as IServiceBehavior);
                }

                var demandPolicy = new AgsPermissionPolicyBehavior(serviceType);

                foreach (var ep in config.Endpoints)
                {
                    var se = retVal.AddServiceEndpoint(new Uri(ep.Address), ep.Contract, new RestHttpBinding());
                    foreach (var bhvr in ep.Behaviors)
                    {
                        se.AddEndpointBehavior(
                            bhvr.Configuration == null ?
                            Activator.CreateInstance(bhvr.Type) as IEndpointBehavior :
                            Activator.CreateInstance(bhvr.Type, bhvr.Configuration) as IEndpointBehavior);
                        se.AddEndpointBehavior(demandPolicy);
                    }
                }
                return(retVal);
            }
            catch (Exception e)
            {
                Tracer.GetTracer(typeof(AgsService)).TraceError("Could not start {0} : {1}", serviceType.FullName, e);
                throw new Exception($"Could not start {serviceType.FullName}", e);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Starts this service handler
        /// </summary>
        public bool Start()
        {
            RemoteEndpointUtil.Current.AddEndpointProvider(this.GetRemoteEndpointInfo);
            this.Starting?.Invoke(this, EventArgs.Empty);

            // Start up each of the services
            foreach (var itm in ApplicationContext.Current.Configuration.GetSection <AgsConfigurationSection>().Services)
            {
                this.m_tracer.TraceInfo("Starting Application Gateway Service {0}..", itm.Name);
                // Service Behaviors
                RestService service = new RestService(itm.ServiceType);

                service.AddServiceBehavior(new AgsAuthorizationServiceBehavior());
                service.AddServiceBehavior(new AgsMagicServiceBehavior());
                service.AddServiceBehavior(new AgsPermissionPolicyBehavior(itm.ServiceType));

                foreach (var bhvr in itm.Behaviors)
                {
                    this.m_tracer.TraceVerbose("AGS Service {0} has behavior {1}", itm.Name, bhvr.XmlType);
                    service.AddServiceBehavior(this.CreateBehavior <IServiceBehavior>(bhvr));
                }
                // Endpoints
                foreach (var ep in itm.Endpoints)
                {
                    this.m_tracer.TraceInfo("\tEndpoint: {0}", ep.Address);
                    var serviceEndpoint = service.AddServiceEndpoint(new Uri(ep.Address), ep.Contract, new RestHttpBinding());
                    foreach (var bhvr in ep.Behaviors)
                    {
                        this.m_tracer.TraceVerbose("AGS Service {0} endpoint {1} has behavior {2}", itm.Name, ep.Address, bhvr.XmlType);
                        serviceEndpoint.AddEndpointBehavior(this.CreateBehavior <IEndpointBehavior>(bhvr));
                    }
                }

                // Add the specified object to the discovery processor
                ServiceEndpointType apiType = ServiceEndpointType.Other;
                if (typeof(IAmiServiceContract).IsAssignableFrom(itm.ServiceType))
                {
                    apiType = ServiceEndpointType.AdministrationIntegrationService;
                }
                else if (typeof(IHdsiServiceContract).IsAssignableFrom(itm.ServiceType))
                {
                    apiType = ServiceEndpointType.HealthDataService;
                }
                else if (typeof(IBisServiceContract).IsAssignableFrom(itm.ServiceType))
                {
                    apiType = ServiceEndpointType.BusinessIntelligenceService;
                }
                else if (typeof(IAuthenticationServiceContract).IsAssignableFrom(itm.ServiceType))
                {
                    apiType = ServiceEndpointType.AuthenticationService;
                }
                else if (typeof(IApplicationServiceContract).IsAssignableFrom(itm.ServiceType))
                {
                    apiType = ServiceEndpointType.Other | ServiceEndpointType.AdministrationIntegrationService;
                }
                ApplicationServiceContext.Current.GetService <IServiceManager>().AddServiceProvider(new ApiEndpointProviderShim(itm.ServiceType, apiType, itm.Endpoints.First().Address, (ServiceEndpointCapabilities)this.GetServiceCapabilities(service)));
                // Start the service
                this.m_services.Add(service);
                service.Start();
            }

            this.Started?.Invoke(this, EventArgs.Empty);
            return(this.IsRunning);
        }