web.config in ASP.NET Core 2 - web-config

I just started learning ASP.NET Core 2 MVC application.
After taking a new project, I don't see any web.config file in solution explorer.
Any help?
Sorry if this a foolish question.

Not just Asp.Net Core 2.x no longer uses web.config, even the Asp.Net Core no longer uses it.
The configuration now is part of the application startup procedure, in Startup.cs. And there is an application setting file called appsettings.json where you can put all your configuration values there.
You can read setting values like this:
appsettings.json
{
"Recaptcha": {
"SiteKey": "xxxx-xxxx",
"SecretKey": "xxxx-xxxx"
}
}
Startup.cs
public class Startup
{
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...
// Configure Google Recaptcha
// reading values from appsettings.json
services.AddRecaptcha(new RecaptchaOptions
{
SiteKey = this.Configuration.GetValue<string>("Recaptcha:SiteKey"),
SecretKey = this.Configuration.GetValue<string>("Recaptcha:SecretKey")
});
}
}
Also in .Net Core projects, there is .csproj file. You can right click a project and click Edit <project>.csproj.
web.config will be generated after you publish your web projects.

Related

How to share a kafka connector and its configuration between multiple services in Quarkus?

I've got the following project structure:
project
- serviceA
- serviceB
- serviceC
- serviceD
And I would like to have a common kafka connector in all the project services. Something like this:
public class CommonProcessor {
private final CommonService commonService;
public CommonProcessor(final CommonService commonService) {
this.commonService= commonService;
}
#Incoming("channel-name")
public CompletionStage<Void> process(final Message<CommonMessage> message) {
final CommonMessage commonMessage = message.getPayload();
commonService.commonMethod(commonMessage.getAttribute());
return message.ack();
}
}
With its corresponding configuration:
mp.messaging.incoming.channel-name.connector=smallrye-kafka
mp.messaging.incoming.channel-name.topic=channel-name
mp.messaging.incoming.channel-name.value.deserializer=foo.bar.CommonDeserializer
How could I share this CommonProcessor (and its configuration) between all services?
Can I put it in a common jar and add it as a dependency in all services? I've tried this option but it does not work because of the configuration. When starting the service with the dependency it says Impossible to bind mediators, some media
tors are not connected: [foo.bar.CommonProcessor#process]
Can it be done through an extension? In this case, I have the same question, how can I share the configuration in application.properties?

After Deploying, ASP.NET application showing Internal server error

I deployed my ASP.NET application to a remote server with a hosting company, and when i try to send data from Postman, i get the internal server error with no definite error message. I have set custom error mode to off in the web config file. please can anyone help me? I have checked for several solutions but nothing.
PS: i am new to ASP.NET deployment with other companies apart from Azure
In this case, you should log error to file to see what issues in deployment mode.
This way i implemented global error log.
public class ExceptionHandlingAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
//Log Critical errors
// You can use log4net library and configure log folder
}
}
In WebApiConfig.cs file you register it.
public static void Register(HttpConfiguration config)
{
// .....
config.Filters.Add(new ExceptionHandlingAttribute());
}

How to use code first migrations with Azure App Service

When I run locally I run the commands below manually and then package and publish the app onto my IIS server.
Add-Migration Initial
Update-Database
When I want to publish to an azure appservice will these commands run automatically? If so how does it know to use a different ConnectionString when I publish it to azure?
I added the connectionString for azure in appsettings.json but I don't understand how I can tell my controllers etc to use that when I publish to azure AppServices
"ConnectionStrings": {
"AzureTestConnection": "Data Source=tcp:xxxxxx-test.database.windows.net,1433;Initial Catalog=xxxxx;User Id=xxx#yyyy.database.windows.net;Password=xxxxxx",
"NWMposBackendContext": "Server=(localdb)\\mssqllocaldb;Database=NWMposBackendContext-573f6261-6657-4916-b5dc-1ebd06f7401b;Trusted_Connection=True;MultipleActiveResultSets=true"
}
I am trying to have three profiles with different connection strings
Local
Published to AzureApp-Test
Published to AzureApp-Prod
When I want to publish to an azure appservice will these commands run automatically?
EF does not support Automatic migrations, you may need to manually execute Add-Migration or dotnet ef migrations add for adding migration files. You could explicitly execute the command to apply the migrations, also you could apply migrations in your code.
And you could add the following code in the Configure method of Startup.cs file:
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
}
I am trying to have three profiles with different connection strings
You would dynamically choose a connection string based on Environment, so here is main steps, you could refer to it.
Set the ASPNETCORE_ENVIRONMENT value to azure in webapp>property>debug.
2.Follow ASP.NET Core MVC with Entity Framework Core to get started.
3.Set the appsetting.json with your two connection string.
{
"ConnectionStrings": {
"DefaultConnection": "connectiondefault",
"azure": "connectionazure"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
Note:You could also set the connectionstring in database on portal to here, then you could test it in local and could use debug to troubleshooting.
Also, you could try to test with one connectionstring to ensure you have no problem with connecting to database.
4.Enable Developer exception page by using app.UseDeveloperExceptionPage(); and the app.UseExceptionHandler methods in your startup class which would display the errors.
public Startup(IHostingEnvironment env)
{
Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
HostingEnvironment = env;
}
public IConfigurationRoot Configuration { get; }
public IHostingEnvironment HostingEnvironment { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
if (HostingEnvironment.IsDevelopment())
{
services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
else
{
services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("azure")));
}
services.AddMvc();
}
For more details, you could refer to this thread.

How to automate the verify, repair and upgrade activity in ALM QC upgrade

I am currently taking care of ALM QC upgrade activity from 11 to 12.20.
During this i need to verify, repair and upgrade each project.
Is there any way to automate this?
I am open on using UFT or selenium.
Let me know if there is any other way to do this.
I'd recommend using SaApi. You can read more about it in
ALM help-> API references -> HP ALM Site Administration API Reference
Basically it's a dll that is a COM object, so you can register it by regsvr32 and use it in your .NET application or vbs application.
To get this dll just login to site admin once - and you'll have it along with other site admin libraries in a path like this ->
C:\Users\YOUR_USERNAME\AppData\Local\HP\ALM-Client\YOUR_ALM_NAME
run cmd.exe as Administrator
register SAClient.dll in cmd
-> C:\Windows\system32>regsvr32 C:\Users\YOUR_USERNAME\AppData\Local\HP\ALM-Client\YOUR_ALM_NAME\SAClient.dll
Now you can add SAClient to your project references.
Create a simple client.
public class ALMSiteAdminClient
{
private SAapi sconnection = new SAapi();
public void connect(string url, string user, string password)
{
sconnection.Login(url, user, password);
}
public void disconnect()
{
sconnection.Logout();
}
public void verifyProject(string domain, string project)
{
sconnection.Verify(domain, project, "basic");
}
public void repairProject(string domain, string project)
{
sconnection.Repair(domain, project, "");
}
public void upgradeProject(string domain, string project)
{
sconnection.Upgrade(domain, project, "");
}}
Use the client ;)
static void Main(string[] args)
{
//get list of a projects from anywhere you want
//this is just a mock example
Dictionary<string, string> projects = new Dictionary<string, string>();
projects.Add("domain1", "project1");
projects.Add("domain1", "project2");
//create site admin client and login
ALMSiteAdminClient SACLIENT = new ALMSiteAdminClient();
SACLIENT.connect("http://myd-vm15059.hpeswlab.net:8081/qcbin", "sa","");
//do whatever you need with each project
foreach (KeyValuePair<string, string> project in projects)
{
SACLIENT.verifyProject(project.Key, project.Value);
SACLIENT.repairProject(project.Key, project.Value);
SACLIENT.upgradeProject(project.Key, project.Value);
}
//logout
SACLIENT.disconnect();
}}
I ended up using HP ALM Robot for auto upgrading QC projects. At the time there wasnt much documentation apart from: https://community.softwaregrp.com/dcvta86296/attachments/dcvta86296/itrc-895/91467/1/Robot_User_Guide_0.pdf
Now there are videos to help out the process too: https://www.youtube.com/watch?v=l-McyxeW0aI

Has anyone successfully deployed a GWT app on Heroku?

Heroku recently began supporting Java apps. Looking through the docs, it seems to resemble the Java Servlet Standard. Does anyone know of an instance where a GWT app has been successfully deployed on Heroku? If so, are there any limitations?
Yes, I've got a successful deployment using the getting started with Java instructions here:
http://devcenter.heroku.com/articles/java
I use the Maven project with appassembler plugin approach but added gwt-maven-plugin to compile a GWT app during the build.
When you push to heroku you see the GWT compile process running, on one thread only so quite slow but it works fine.
The embedded Jetty instance is configured to serve up static resources at /static from src/main/resources/static and I copy the compiled GWT app to this location during the build and then reference the .nocache.js as normal.
What else do you want to know?
You've got a choice, either build the Javascript representation of your GWT app locally into your Maven project, commit it and the read it from your app, or to generate it inside Heroku via the gwt-maven-plugin as I mentioned.
The code to serve up files from a static location inside your jar via embedded Jetty is something like this inside a Guice ServletModule:
(See my other answer below for a simpler and less Guice-driven way to do this.)
protected void configureServlets() {
bind(DefaultServlet.class).in(Singleton.class);
Map<String, String> initParams = new HashMap<String, String>();
initParams.put("pathInfoOnly", "true");
initParams.put("resourceBase", staticResourceBase());
serve("/static/*").with(DefaultServlet.class, initParams);
}
private String staticResourceBase() {
try {
return WebServletModule.class.getResource("/static").toURI().toString();
}
catch (URISyntaxException e) {
e.printStackTrace();
return "couldn't resolve real path to static/";
}
}
There's a few other tricks to getting embedded Jetty working with guice-servlet, let me know if this isn't enough.
My first answer to this turned out to have problems when GWT tried to read its serialization policy. In the end I went for a simpler approach that was less Guice-based. I had to step through the Jetty code to understand why setBaseResource() was the way to go - it's not immediately obvious from the Javadoc.
Here's my server class - the one with the main() method that you point Heroku at via your app-assembler plugin as per the Heroku docs.
public class MyServer {
public static void main(String[] args) throws Exception {
if (args.length > 0) {
new MyServer().start(Integer.valueOf(args[0]));
}
else {
new MyServer().start(Integer.valueOf(System.getenv("PORT")));
}
}
public void start(int port) throws Exception {
Server server = new Server(port);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setBaseResource(createResourceForStatics());
context.setContextPath("/");
context.addEventListener(new AppConfig());
context.addFilter(GuiceFilter.class, "/*", null);
context.addServlet(DefaultServlet.class, "/");
server.setHandler(context);
server.start();
server.join();
}
private Resource createResourceForStatics() throws MalformedURLException, IOException {
String staticDir = getClass().getClassLoader().getResource("static/").toExternalForm();
Resource staticResource = Resource.newResource(staticDir);
return staticResource;
}
}
AppConfig.java is a GuiceServletContextListener.
You then put your static resources under src/main/resources/static/.
In theory, one should be able to run GWT using the embedded versions of Jetty or Tomcat, and bootstrap the server in main as described in the Heroku Java docs.