I am creating a class library that needs to communicate with a Sqlite Database, using .NET Core and EF Core.
I'm executing my code from a Test Project in the same solution
The issue is GetConnectionString() always returns null.
My appsettings.json is
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"TestingDatabase": "Server=c:\\myDb.db;Version=3;"
}
}
And my Entities file is
public Entities()
{
_configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", true,true)
.Build();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = _configuration.GetConnectionString("TestingDatabase"); // always returns null
optionsBuilder.UseSqlite(connectionString);
}
I don't understand why.
Related
I am using Java API like this
public void convertJsonToJavaClass(URL inputJsonUrl, File outputJavaClassDirectory, String packageName, String javaClassName) throws IOException {
com.sun.codemodel.JCodeModel jcodeModel = new com.sun.codemodel.JCodeModel();
GenerationConfig config = new DefaultGenerationConfig() {
#Override
public boolean isGenerateBuilders() {
return true;
}
#Override
public SourceType getSourceType() {
return SourceType.JSON;
}
};
SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
mapper.generate(jcodeModel, javaClassName, packageName, inputJsonUrl);
jcodeModel.build(outputJavaClassDirectory);
}
The input Json schema file has properties that look like this:
{
"type": "object",
"properties": {
"Market": {
"type": "string"
},
"Dealer": {
"type": "integer"
},
"Side": {
"type": "string",
"enum": ["Buy", "Sell", "Pay", "Receive", "Undisclosed"]
},
"Package": {
"type": "string",
"enum": ["Y", "N"]
}
},
"required": ["Market", "Side"]
}
The default output is 5 POJO classes, 1 for each Json element with a Properties.java to collect all the POJO. Whereas I would like 1 POJO with the Json elements converted to Java primitives. What to do please? Is this Json schema layout and Java API code the best practise for jsonschema2pojo? Thanks
The problem was using return SourceType.JSON; for a JsonSchema.
When I changed to return SourceType.JSONSCHEMA; it created the 1 POJO class.
I have an Azure Function in .net5 (a.k.a. dotnet-isolated) and I've added Entity Framework like this
services.AddDbContext<EfDbContext>(options =>
{
options.UseSqlServer(context.Configuration[...], builder =>
{
builder.EnableRetryOnFailure();
});
});
When I run the function I see the DB queries from EF in the console (info level judging by the color).
How can I disable them? I tried obvious options in my host.json:
"logging": {
"logLevel": {
"Microsoft.EntityFrameworkCore": "Warning",
}
}
and
"logging": {
"logLevel": {
"default": "Information",
"Microsoft.EntityFrameworkCore": "Warning",
"Microsoft.EntityFrameworkCore.Database": "Warning",
"Microsoft.EntityFrameworkCore.Query": "Warning"
}
}
or even
"logging": {
"logLevel": {
"Microsoft": "Warning",
}
}
but it didn't help. The only option which worked was
"logging": {
"logLevel": {
"default": "Warning",
}
}
Am I missing something?
Found a way. That's a bit strange though and I still wanna understand how it works and why it's done this way. Anyways, from AppInsights logs I found out that EF logs are written under the Function.<YOUR_FUNCTION_NAME>.User category. (turns out that a standard EF category is somehow overwritten by the functions runtime or so?).
That means that I can impact on the overall log level of a specific function by
"logging": {
"logLevel": {
"Function.MyFunc1.User": "Debug",
"Function.MyFunc2.User": "Warning"
}
}
in the host.json. It can be helpful but it doesn't solve my problem.
If however I now add a filter in the Program.cs like this:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
...
.ConfigureLogging(builder =>
{
builder.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.Warning);
})
...
the EF info logs are gone.
This worked for me:
builder.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.None);
I am trying to host my Asp.Net Core application on a Raspberry Pi 4 with a LocalDB, but I get the following error message:
An unhandled exception occurred while processing the request.
PlatformNotSupportedException: LocalDB is not supported on this platform.
Microsoft.Data.SqlClient.SNI.LocalDB.GetLocalDBConnectionString(string localDbInstance)
This is how my appsettings.json looks like:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MyString": "data source=(localdb)\\MSSQLLocalDB;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
},
}
That's how I configure the Database in my Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DatenbankKontext>(item =>
item.UseSqlServer(Configuration.GetConnectionString("MyString")));
}
It worked fine on Windows but I don't know how to properly configure it for Windows. How can I solve this?
Helo guys, sorry for my English =(
I am building a consumer of the jira api and I had the following error "Can not deserialize instance of Jira out of START_ARRAY token"
My JSON looks like this:
[
{
"expand": "description,lead,url,projectKeys",
"self": "http://",
"id": "10802",
"key": "TE",
"name": "TEST TEST",
"avatarUrls": {
"48x48": "http://",
"24x24": "http://",
"16x16": "http://",
"32x32": "http://"
},
"projectCategory": {
"self": "http://",
"id": "10200",
"name": "TTTTTT",
"description": "TTTTTTTT"
},
"projectTypeKey": "software"
},
{
"expand": "description,lead,url,projectKeys",
"self": "http://",
"id": "10801",
"key": "TT",
"name": "TREINAMENTO TESTE",
"avatarUrls": {
"48x48": "http://",
"24x24": "http://",
"16x16": "http://",
"32x32": "http://"
},
"projectTypeKey": "business"
}
]
Here's my code.
public class Project {
private String expand;
private String self;
private int ID;
private String key;
private String name;
private Avatar avatarUrls;
private ProjectCategory projectCategory;
private String projectTypeKey;
//get and setter
}
public class Jira {
private ArrayList<Project> projects;
public Jira() {
}
public ArrayList<Project> getProjects() {
return projects;
}
public void setProjects(ArrayList<Project> projects) {
this.projects = projects;
}
}
public class Application {
public static void main(String args[]) throws IOException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = new HttpHeaders();
//AUTHORIZATION CIT
header.set("Authorization", "Basic XXXXXXXXX");
header.set("app_token", "XXXXXXXXX");
HttpEntity entity = new HttpEntity(header);
ResponseEntity <Jira> result = restTemplate.exchange("URL",HttpMethod.GET, entity, Jira.class);
System.out.println(result.getBody().toString());
}
}
Any ideas how to solve this?
Thank you.
I have a solution for you. This error appears because Jira API returns a list of objects.
Here is what you should do:
ResponseEntity <Jira[]> result = restTemplate.exchange("URL",HttpMethod.GET, entity, Jira[].class);
List<Jira>=Arrays.asList(result.getBody());
I´m using Spring Boot and HATEOAS to build a REST API and I am struggling with the curie creation. The Spring HATEOAS guide says that in order to automatically insert a curie in the responses, you should do the following:
#Configuration
#EnableWebMvc
#EnableHypermediaSupport(type= {HypermediaType.HAL})
public class Config {
#Bean
public CurieProvider curieProvider() {
return new DefaultCurieProvider("ex", new UriTemplate("http://www.example.com{#rel}"));
}
}
My config class is like this:
#SpringBootApplication
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
#Bean
public CurieProvider curieProvider() {
return new DefaultCurieProvider("xpto", new UriTemplate("http://www.xpto.com{#rel}"));
}
}
I tried to add the #EnableWebMvc to my config class but it changes the rendering of the response (hal) and the curie doesn´t appear. I have to do something on the controller to create the curie?
Update:
I updated the Spring Hateoas (to 0.17.0.RELEASE) and now my collection name is rendered with the curie but the curie does not appear in the _links section:
{
"_links": {
"self": {
"href": "http://localhost:8080/technologies"
}
},
"_embedded": {
"mycurie:technology": [
{
"id": 1,
"description": "A",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/1"
}
}
},
{
"id": 2,
"description": "B",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/2"
}
}
}
]
}
}
If I add one link to the _links section then the curie link appears:
{
"_links": {
"self": {
"href": "http://localhost:8080/technologies"
},
"mycurie:xpto": {
"href": "http://localhost:8080/xpto"
},
"curies": [
{
"href": "http://localhost:8080/rels/{rel}",
"name": "mycurie",
"templated": true
}
]
},
"_embedded": {
"mycurie:technology": [
{
"id": 1,
"description": "A",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/1"
}
}
},
{
"id": 2,
"description": "B",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/2"
}
}
}
]
}
}
This is my controller:
#RestController
#ExposesResourceFor(Technology.class)
#RequestMapping(value = "/technologies")
public class TechnologyRestController {
...
#RequestMapping(method = RequestMethod.GET, produces = "application/vnd.xpto-technologies.text+json")
public Resources<TechnologyResource> getAllTechnologies() {
List<Technology> technologies = technologyGateway.getAllTechnologies();
Resources<TechnologyResource> technologiesResources = new Resources<TechnologyResource>(technologyResourceAssembler.toResources(technologies));
technologiesResources.add(linkTo(methodOn(TechnologyRestController.class).getAllTechnologies()).withSelfRel());
return technologiesResources;
}
}
Your config class looks correct to me, however Spring Boot's HypermediaAutoConfiguration requires org.springframework.plugin:spring-plugin-core, an optional dependency of Spring HATEOAS, to be on the classpath for it to be enabled. I'd guess that you're missing this dependency. Try adding a dependency on org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE.