Move Azure Search and CosmosDB keys to appsettings.json

This commit is contained in:
Robin-Manuel Thiel 2018-02-13 15:21:00 +01:00
Родитель c1061b46cb
Коммит cddfd12a3e
11 изменённых файлов: 85 добавлений и 81 удалений

Просмотреть файл

@ -19,7 +19,6 @@
<Folder Include="wwwroot\" />
<Folder Include="Services\" />
<Folder Include="Models\" />
<Folder Include="Helpers\" />
<Folder Include="Services\Blob Storage\" />
<Folder Include="DummyData\" />
<Folder Include="Services\Storage Queue\" />

Просмотреть файл

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using ContosoMaintenance.WebAPI.Services;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
using Microsoft.Extensions.Configuration;
namespace ContosoMaintenance.WebAPI.Controllers
{
@ -10,9 +11,9 @@ namespace ContosoMaintenance.WebAPI.Controllers
{
public DocumentDBRepositoryBase<T> DBRepository = new DocumentDBRepositoryBase<T>();
public BaseController()
public BaseController(IConfiguration configuration)
{
DBRepository.Initialize();
DBRepository.Initialize(configuration["AzureCosmosDb:CosmosEndpoint"], configuration["AzureCosmosDb:CosmosKey"], configuration["AzureCosmosDb:CosmosDatabaseId"]);
}
void CreateDummyDataIfNeeded()

Просмотреть файл

@ -1,11 +1,15 @@
using System;
using ContosoMaintenance.WebAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace ContosoMaintenance.WebAPI.Controllers
{
[Route("/api/customer")]
public class CustomerController : BaseController<Customer>
{
public CustomerController(IConfiguration configuration) : base(configuration)
{
}
}
}

Просмотреть файл

@ -1,11 +1,15 @@
using System;
using ContosoMaintenance.WebAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace ContosoMaintenance.WebAPI.Controllers
{
[Route("/api/employee")]
public class EmployeeController : BaseController<Employee>
{
public EmployeeController(IConfiguration configuration) : base(configuration)
{
}
}
}

Просмотреть файл

@ -1,10 +1,14 @@
using ContosoMaintenance.WebAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace ContosoMaintenance.WebAPI.Controllers
{
[Route("/api/job")]
public class JobController : BaseController<Job>
{
public JobController(IConfiguration configuration) : base(configuration)
{
}
}
}

Просмотреть файл

@ -1,10 +1,14 @@
using ContosoMaintenance.WebAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace ContosoMaintenance.WebAPI.Controllers
{
[Route("/api/part")]
public class PartController : BaseController<Part>
{
public PartController(IConfiguration configuration) : base(configuration)
{
}
}
}

Просмотреть файл

@ -7,16 +7,23 @@ using ContosoMaintenance.WebAPI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Extensions.Configuration;
namespace ContosoMaintenance.WebAPI.Controllers
{
public class SearchController : Controller
public class SearchController : Controller
{
SearchServiceClient serviceClient;
public SearchController(IConfiguration configuration)
{
serviceClient = new SearchServiceClient(configuration["AzureSearch:AzureSearchServiceName"], new SearchCredentials(configuration["AzureSearch:AzureSearchApiKey"]));
}
[Route("/api/search/jobs")]
public async Task<List<Job>> Get(string keyword)
{
var sp = new SearchParameters();
var serviceClient = new SearchServiceClient(Helpers.Keys.AzureSearchServiceName, new SearchCredentials(Helpers.Keys.AzureSearchApiKey));
var indexClient = serviceClient.Indexes.GetClient("job-index");
var response = await indexClient.Documents.SearchAsync<Job>(keyword, sp);

Просмотреть файл

@ -1,27 +0,0 @@
using System;
namespace ContosoMaintenance.WebAPI.Helpers
{
public static class Keys
{
#region CosmosDB Keys
public static string CosmosEndpoint = "https://contosomaintenance.documents.azure.com:443/";
public static string CosmosKey = "uWKYOhZTjTW8T1WvixwTpgUsLD3V5aW7Z7OcWjUHvw7THBZEBi6I4HZaSjyaj567YKU5esqMS270p7vfNj993A==";
public static string CosmosDatabaseId = "contosomaintenance";
#endregion
#region Azure Search Keys
public static string AzureSearchServiceName = "contosomaintenance";
public static string AzureSearchApiKey = "3B1E2B38DC3C03F5357F7B409F814BF8";
#endregion
}
}

Просмотреть файл

@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
@ -8,28 +8,26 @@ using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
namespace ContosoMaintenance.WebAPI.Services
{
{
public class DocumentDBRepositoryBase<T> where T : class
{
string Endpoint = Helpers.Keys.CosmosEndpoint;
string Key = Helpers.Keys.CosmosKey;
string DatabaseId = Helpers.Keys.CosmosDatabaseId;
string CollectionId;
string databaseId;
DocumentClient client;
public void Initialize()
{
client = new DocumentClient(new Uri(Endpoint), Key, new ConnectionPolicy { EnableEndpointDiscovery = false });
CreateDatabaseIfNotExistsAsync().Wait();
CreateCollectionIfNotExistsAsync().Wait();
}
public void Initialize(string endpoint, string key, string databaseId)
{
this.databaseId = databaseId;
client = new DocumentClient(new Uri(endpoint), key, new ConnectionPolicy { EnableEndpointDiscovery = false });
CreateDatabaseIfNotExistsAsync().Wait();
CreateCollectionIfNotExistsAsync().Wait();
}
public async Task<T> GetItemAsync(string id)
public async Task<T> GetItemAsync(string id)
{
try
{
Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));
Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, CollectionId, id));
return (T)(dynamic)document;
}
catch (DocumentClientException e)
@ -46,7 +44,7 @@ namespace ContosoMaintenance.WebAPI.Services
{
try
{
var document = client.CreateDocumentCollectionQuery(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),"SELECT c.id FROM c");
var document = client.CreateDocumentCollectionQuery(UriFactory.CreateDocumentCollectionUri(databaseId, CollectionId), "SELECT c.id FROM c");
return (int)(dynamic)document.Count();
}
catch (DocumentClientException e)
@ -58,13 +56,13 @@ namespace ContosoMaintenance.WebAPI.Services
throw;
}
}
public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)
{
CollectionId = GetCollectionName();
IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(databaseId, CollectionId),
new FeedOptions { MaxItemCount = -1 })
.Where(predicate)
.AsDocumentQuery();
@ -80,33 +78,33 @@ namespace ContosoMaintenance.WebAPI.Services
public async Task<Document> CreateItemAsync(T item)
{
CollectionId = GetCollectionName();
return await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), item);
CollectionId = GetCollectionName();
return await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseId, CollectionId), item);
}
public async Task<Document> UpdateItemAsync(string id, T item)
{
CollectionId = GetCollectionName();
return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), item);
CollectionId = GetCollectionName();
return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, CollectionId, id), item);
}
public async Task DeleteItemAsync(string id)
{
CollectionId = GetCollectionName();
await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));
CollectionId = GetCollectionName();
await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(databaseId, CollectionId, id));
}
async Task CreateDatabaseIfNotExistsAsync()
{
try
{
await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId));
await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
}
catch (DocumentClientException e)
{
if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await client.CreateDatabaseAsync(new Database { Id = DatabaseId });
await client.CreateDatabaseAsync(new Database { Id = databaseId });
}
else
{
@ -117,18 +115,18 @@ namespace ContosoMaintenance.WebAPI.Services
async Task CreateCollectionIfNotExistsAsync()
{
CollectionId = GetCollectionName();
CollectionId = GetCollectionName();
try
try
{
await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));
await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, CollectionId));
}
catch (DocumentClientException e)
{
if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(DatabaseId),
UriFactory.CreateDatabaseUri(databaseId),
new DocumentCollection { Id = CollectionId },
new RequestOptions { OfferThroughput = 400 });
}

Просмотреть файл

@ -21,7 +21,9 @@ namespace ContosoMaintenance.WebAPI
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Inject Configuration
services.AddSingleton<IConfiguration>(Configuration);
services.AddScoped<IAzureBlobStorage>(factory =>
{
return new AzureBlobStorage(new AzureBlobSettings(

Просмотреть файл

@ -1,22 +1,30 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
"AzureStorage": {
"Blob_StorageAccount": "contosomaintenance",
"Blob_StorageKey": "fo4azz3N7jCRE/ZIDeB9Bon4LX9qlmxZWmpwAYUYBNFnsG/UX8ml+3tO+29YYLTOhcYtReN9RuAKC+CLGmqLXg==",
"Blob_ContainerName": "photos",
"Queue_Name": "processphotos"
},
"AzureCosmosDb": {
"CosmosEndpoint": "https://contosomaintenance.documents.azure.com:443/",
"CosmosKey": "uWKYOhZTjTW8T1WvixwTpgUsLD3V5aW7Z7OcWjUHvw7THBZEBi6I4HZaSjyaj567YKU5esqMS270p7vfNj993A==",
"CosmosDatabaseId": "contosomaintenance"
},
"AzureSearch": {
"AzureSearchServiceName": "contosomaintenance",
"AzureSearchApiKey": "3B1E2B38DC3C03F5357F7B409F814BF8"
}
},
"AzureStorage": {
"Blob_StorageAccount": "contosomaintenance",
"Blob_StorageKey": "fo4azz3N7jCRE/ZIDeB9Bon4LX9qlmxZWmpwAYUYBNFnsG/UX8ml+3tO+29YYLTOhcYtReN9RuAKC+CLGmqLXg==",
"Blob_ContainerName": "photos",
"Queue_Name": "processphotos"
}
}