예제 #1
0
        public static void RegisterComponents()
        {
            var builder = new ContainerBuilder();

            // Get your HttpConfiguration.
            var config = GlobalConfiguration.Configuration;

            var bizAssembly  = Assembly.Load(BizAssembly);
            var dataAssembly = Assembly.Load(DataAssembly);

            string connection = KeyValutHelper.GetSecret(CommonConfig.DBConnectionSecretUri);

            builder.RegisterType <UnitOfWork>().As <IUnitOfWork>().InstancePerLifetimeScope().WithParameter("connection", connection);

            builder.RegisterAssemblyTypes(bizAssembly)
            .Where(x => x.Name.EndsWith(ServiceClass))
            .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(dataAssembly)
            .Where(x => x.Name.EndsWith(RepositoryClass))
            .AsImplementedInterfaces();

            // Register your Web API controllers.
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            // Set the dependency resolver to be Autofac.
            var container = builder.Build();

            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
        public async Task <EmbedToken> GetToken(string groupId, string type, string id)
        {
            try
            {
                string   secretValue = KeyValutHelper.GetSecret(CommonConfig.PbiSecretUri);
                string[] splitValue  = secretValue.Split(new String[] { Constants.PbiSecretSplit }, StringSplitOptions.None);
                string   userName    = splitValue[0];
                string   password    = splitValue[1];

                string token = await GetTokenAsync(userName, password);

                var tokenCredentials = new TokenCredentials(token, Constants.PbiTokenType);

                // Create a Power BI Client object. It will be used to call Power BI APIs.
                using (var client = new PowerBIClient(new Uri(CommonConfig.PbiApiUrl), tokenCredentials))
                {
                    // Generate Embed Token.
                    var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: Constants.PbiDefaultAccessLevel);

                    EmbedToken tokenResponse = null;

                    if (Constants.PbiTypeReport.Equals(type, StringComparison.OrdinalIgnoreCase))
                    {
                        tokenResponse = await client.Reports.GenerateTokenInGroupAsync(groupId, id, generateTokenRequestParameters);
                    }
                    else if (Constants.PbiTypeDashboard.Equals(type, StringComparison.OrdinalIgnoreCase))
                    {
                        tokenResponse = await client.Dashboards.GenerateTokenInGroupAsync(groupId, id, generateTokenRequestParameters);
                    }

                    return(tokenResponse);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleServiceException(ex);
                return(null);
            }
        }