示例#1
0
        public static ISunnyApplicationBuilder UseService(this ISunnyApplicationBuilder app, Type serviceType)
        {
            if (!typeof(ISunnyService).GetTypeInfo().IsAssignableFrom(serviceType.GetTypeInfo()))
            {
                throw new InvalidOperationException(Resources.FormatException_UseServiceServiceTypeNotInheritedISunnyService(serviceType));
            }
            return(app.Use(next =>
            {
                return async context =>
                {
                    var serviceFactory = context.RequestServices.GetService <ISunnyServiceFactory>();
                    if (serviceFactory == null)
                    {
                        // No service factory
                        throw new InvalidOperationException(Resources.FormatException_UseServiceNoSuunyServiceFactory(typeof(ISunnyServiceFactory)));
                    }

                    var service = serviceFactory.Create(serviceType);
                    if (service == null)
                    {
                        // The factory returned null, it's a broken implementation
                        throw new InvalidOperationException(Resources.FormatException_UseServiceUnableToCreateSunnyService(serviceFactory.GetType(), serviceType));
                    }

                    try
                    {
                        await service.InvokeAsync(context, next);
                    }
                    finally
                    {
                        serviceFactory.Release(service);
                    }
                };
            }));
        }
示例#2
0
文件: Startup.cs 项目: lfzm/Sunny
        public void Configure(ISunnyApplicationBuilder app)
        {
            app.UseClientServices();
            Func <RequestDelegate, RequestDelegate> middleware = next =>
            {
                return(context =>
                {
                    return next(context);
                });
            };

            app.Use(middleware);
        }