Ejemplo n.º 1
0
        /// <summary>An IAppHost extension method that adds the plugins from assembly to 'assembliesWithPlugins'.</summary>
        ///
        /// <param name="appHost">              .</param>
        /// <param name="assembliesWithPlugins">A variable-length parameters list containing assemblies with plugins.</param>
        public static void AddPluginsFromAssembly(this IAppHost appHost, params Assembly[] assembliesWithPlugins)
        {
            foreach (Assembly assembly in assembliesWithPlugins)
            {
                var pluginTypes =
                    from t in assembly.GetExportedTypes()
                    where t.GetInterfaces().Any(x => x == typeof(IPlugin))
                    select t;

                foreach (var pluginType in pluginTypes)
                {
                    try
                    {
                        var plugin = pluginType.CreateInstance() as IPlugin;
                        if (plugin != null)
                        {
                            EndpointHost.AddPlugin(plugin);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error adding new Plugin " + pluginType.Name, ex);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>Process the request.</summary>
        ///
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        ///
        /// <param name="httpReq">      The HTTP request.</param>
        /// <param name="httpRes">      The HTTP resource.</param>
        /// <param name="operationName">Name of the operation.</param>
        public override void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {
            try
            {
                EndpointHost.Config.AssertFeatures(format);

                if (EndpointHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return;
                }

                httpReq.ResponseContentType = httpReq.GetQueryStringContentType() ?? this.HandlerContentType;
                var callback = httpReq.QueryString["callback"];
                var doJsonp  = EndpointHost.Config.AllowJsonpRequests &&
                               !string.IsNullOrEmpty(callback);

                var request = CreateRequest(httpReq, operationName);
                if (EndpointHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return;
                }

                var response = GetResponse(httpReq, httpRes, request);
                if (EndpointHost.ApplyResponseFilters(httpReq, httpRes, response))
                {
                    return;
                }

                if (doJsonp && !(response is CompressedResult))
                {
                    httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(), ")".ToUtf8Bytes());
                }
                else
                {
                    httpRes.WriteToResponse(httpReq, response);
                }
            }
            catch (Exception ex)
            {
                if (!EndpointHost.Config.WriteErrorsToResponse)
                {
                    throw;
                }
                HandleException(httpReq, httpRes, operationName, ex);
            }
        }
Ejemplo n.º 3
0
        /// <summary>Initialises this object.</summary>
        ///
        /// <exception cref="InvalidDataException">Thrown when an Invalid Data error condition occurs.</exception>
        public void Init()
        {
            if (Instance != null)
            {
                throw new InvalidDataException("AppHostBase.Instance has already been set");
            }

            Instance = this;

            var serviceManager = EndpointHost.Config.ServiceManager;

            if (serviceManager != null)
            {
                serviceManager.Init();

                Configure(EndpointHost.Config.ServiceManager.Container);
            }
            else
            {
                Configure(null);
            }

            EndpointHost.AfterInit();
        }
Ejemplo n.º 4
0
        /// <summary>Process the request.</summary>
        ///
        /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
        /// <exception cref="Exception">            Thrown when an exception error condition occurs.</exception>
        ///
        /// <param name="httpReq">      The HTTP request.</param>
        /// <param name="httpRes">      The HTTP resource.</param>
        /// <param name="operationName">Name of the operation.</param>
        public override void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {
            try
            {
                if (EndpointHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return;
                }

                var restPath = GetRestPath(httpReq.HttpMethod, httpReq.PathInfo);
                if (restPath == null)
                {
                    throw new NotSupportedException("No RestPath found for: " + httpReq.HttpMethod + " " + httpReq.PathInfo);
                }

                operationName = restPath.RequestType.Name;

                var callback = httpReq.GetJsonpCallback();
                var doJsonp  = EndpointHost.Config.AllowJsonpRequests &&
                               !string.IsNullOrEmpty(callback);

                if (ResponseContentType != null)
                {
                    httpReq.ResponseContentType = ResponseContentType;
                }

                var responseContentType = httpReq.ResponseContentType;
                EndpointHost.Config.AssertContentType(responseContentType);

                var request = GetRequest(httpReq, restPath);
                if (EndpointHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return;
                }

                var response = GetResponse(httpReq, httpRes, request);
                if (EndpointHost.ApplyResponseFilters(httpReq, httpRes, response))
                {
                    return;
                }

                if (responseContentType.Contains("jsv") && !string.IsNullOrEmpty(httpReq.QueryString["debug"]))
                {
                    JsvSyncReplyHandler.WriteDebugResponse(httpRes, response);
                    return;
                }

                if (doJsonp && !(response is CompressedResult))
                {
                    httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(), ")".ToUtf8Bytes());
                }
                else
                {
                    httpRes.WriteToResponse(httpReq, response);
                }
            }
            catch (Exception ex)
            {
                if (!EndpointHost.Config.WriteErrorsToResponse)
                {
                    throw;
                }
                HandleException(httpReq, httpRes, operationName, ex);
            }
        }
Ejemplo n.º 5
0
 /// <summary>Initializes a new instance of the NServiceKit.WebHost.Endpoints.AppHostBase class.</summary>
 ///
 /// <param name="serviceName">           Name of the service.</param>
 /// <param name="assembliesWithServices">A variable-length parameters list containing assemblies with services.</param>
 protected AppHostBase(string serviceName, params Assembly[] assembliesWithServices)
 {
     EndpointHost.ConfigureHost(this, serviceName, CreateServiceManager(assembliesWithServices));
 }