I have created a web application, below is the entry point of that application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
using Microsoft.Graph.Communications.Common.Telemetry;
namespace ProductsApp3
{
public class WebApiApplication : System.Web.HttpApplication
{
private readonly IGraphLogger logger;
public WebApiApplication()
{
this.logger =
new
GraphLogger(typeof(WebApiApplication).Assembly.GetName().Name,
redirectToTrace: true);
}
protected void Application_Start()
{
this.logger.Info("WorkerRole has been started");
Console.WriteLine("Sai");
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}
But Nowhere I can see the below statements when I run the application in Visual Studio
this.logger.Info("WorkerRole has been started");
Console.WriteLine("Sai");"
can someone help me please where I can see this log inormation locally and also in AKS if I deploy this app there?.
Related
I did an ASP.NET Core MVC app working with the Web. This app read from and write to a database.
Now, I want to create a console app that uses the same database (server, connection string,...), also in .NET Core.
How can I reuse the code of the database context to access the same database than the first app?
For now, my Program.cs is the basic. I would like to insert here the code to prepare the database connection...
EDIT
My data context model is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MVFxml;
using MVFxml.Models;
using MVFxml.Models.ViewModels;
//using MVFxml.Controllers;
using MVFxml.Infrastructure;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;
namespace MVFxml.Models
{
public class fmDataContext : DbContext
{
private DbContextOptions<fmDataContext> opt { get; set; }
public fmDataContext(DbContextOptions<fmDataContext> options)
: base(options)
{
opt = options;
}
public MyStructure RetrieveFromDb(long id = -1)
{
… code here …
}
...other fuctions here...
}
To solve this problem, I wrote the Main routine of my console app as following:
static void Main(string[] args)
{
…
var optionsBuilder = new DbContextOptionsBuilder<fmDataContext>();
optionsBuilder
.UseSqlServer(connectionString, providerOptions => providerOptions.CommandTimeout(60));
using (fmDataContext context = new fmDataContext(optionsBuilder.Options))
{
MyStructure data = context.RetrieveFromDb();
… // here I do whatever I want with the data
}
And I didn't have to change my fmDataContext class shown above...
I am doing user authentication using owin middleware. But for IppBuilder it is giving error "IAppBuilder could not found. Are you missing Reference?" I have already installed nuggets packages related to owin as shown while searching on google.
Any help would be appreciated. Here is my code:
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using OpenConfigure.Core;
[assembly: OwinStartup(typeof(CURDApis.API.Startup))]
namespace CURDApis.API
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
}
In the below code, it gives me an compilation error for the line "public class MvcApplication : System.Web.HttpApplication"
Please, help me to solve this problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace Simple_Calendar
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
I think System.Web.HttpApplication dll has been corrupted just add dll in your reference
I've been watching some tuts on how to create a C# Windows Service; all good but no one says how to make te service run, at the end of installation, a specific file from installation folder(in my case hidden.vbs)(my app has 2 project: the service itself and the setup).
After the install of the setup, the service starts PROJECT_NAME.exe and PROJECT_NAME.svhost.exe
Tell me please if you need any other code in order to help me...
Here is my Program.cs
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
namespace PROJECT_NAME
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
new ServiceController(serviceInstaller1.ServiceName).Start();
}
}
}
Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace PROJECT_NAME
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
}
protected override void OnStop()
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
}
}
}
Also, here is a pic of my Solution Explorer http://i.imgur.com/wbqUGOc.png ; please tell me how or where should I import the files I need the service to run.
It's my first time in C#, I'm not willing to understand it now, but to make this service because I will need it in my work..
Your script can do one of several things to start a service:
Issue the net start console command to launch a service (e.g. net start "My Service Name")
OR
Programmaticaly, call the StartService API.
I’m pretty new to programming, so bear with me if my question isn’t specific enough. Right now I’m trying to make a simple Client Logon to my server. So the server App knows which users are connected. When a client connects I want an event to fire on the server that update the userlist. But it doesn’t and I can’t figure out why. Hope you can help.
In the codes I have removed how the users should be displayed in the serverApp. Right now I just need the event to work.
In my Service Library:
INetworkService contract:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace NetworkLib
{
[ServiceContract]
public interface INetworkService
{
[OperationContract]
void Logon(UserInfo userInfo);
[OperationContract]
void Logout();
}
}
NetworkService Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace NetworkLib
{
public class NetworkService : INetworkService
{
public event EventHandler UserListChanged;
public void Logon(UserInfo userInfo)
{
OnUserListChanged();
}
public void Logout()
{
OnUserListChanged();
}
private void OnUserListChanged()
{
var handler = UserListChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
}
UserInfo Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace NetworkLib
{
[DataContract]
public class UserInfo
{
[DataMember]
public string Name;
}
}
In my ServerApp (WPF):
using System.ServiceModel;
using NetworkLib;
namespace ServerApp
{
public partial class MainWindow : Window
{
NetworkService networkService;
public MainWindow()
{
InitializeComponent();
ServiceHost host = new ServiceHost(typeof(NetworkService));
host.Open();
networkService = new NetworkService();
networkService.UserListChanged += networkService_UserListChanged;
}
private void networkService_UserListChanged(object sender, EventArgs e)
{
MessageBox.Show("It Works!");
}
}
}
In my ClientApp (WPF): (Have made a Service Reference to the Server)
namespace ClientApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ServiceReference.NetworkServiceClient proxy = new ServiceReference.NetworkServiceClient();
ServiceReference.UserInfo userInfo = new ServiceReference.UserInfo();
userInfo.Name = "Test";
proxy.Logon(userInfo);
}
}
}
You subscribe to event of other NetworkService instance than ServiceHost instantiates. In your case every time you make request to server, new NetworkService instance is created. Place the following attribute above NetworkService class:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
Then subscribe to event:
var serviceInstance = (NetworkService)host.SingletonInstance;
serviceInstance.UserListChanged += networkService_UserListChanged;
When creating your ServiceHost, you should provide NetworkService instance instead of typeof(NetworkService)
ServiceHost host = new ServiceHost(networkService);
You need to initialize it first, of course.
I didnt look in great detail but overall your code looks ok. It is sometimes useful in this situation to run 2 instances of VisualStudio - one for server in debug and one for client in debug. If you put a break point in client button1_Click code in VS thats debugging client, and a break point in NetworkServices.Logout in VS thats debugging server you will be able to step from client to server code and see easily whats going wrong where.
Why do you need an event model here? Why not just handle your "event" directly in NetworkService.Logout(). Does pushing this off to an event and then having to wire that event (which as ilya.dofofeev correctly points out is not on the same object) provide any real benefit?