I'm a total noob in developing windows services and I found a tutorial about implementing a standard windows service. This is the code that I found:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace ReviewsSvc
{
[RunInstaller(true)]
public class ReviewsSvcInstaller
{
private ServiceProcessInstaller processInstaller;
private ServiceInstaller serviceInstaller;
public ReviewsSvcInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Reviews Updater";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
I've added the necessary references but I still get an error about 'Installers' not found. What am I missing?
You forgot to specify the base class
namespace ReviewsSvc
{
[RunInstaller(true)]
public class ReviewsSvcInstaller : Installer
{
....
Make sure the class ReviewsSvcInstaller in the main executable (EXE). The installer look for this class in the main entry assembly. I hope it helps.
Related
I have an issue related to running CodedUITests from a Windows Form. (it works in the CodedUITestProject with Test-Explorer).
This is my CodedUITest's structure :
[CodedUITest]
public class DBTest
{
#region Static Fields
public static CSVReader csvReader;
#endregion
#region Fields
public string logFileName;
public string timer = String.Empty;
public TestRead testRead;
public UploadResults uploadResults;
private DateTime testStart;
#endregion
[ClassInitialize]
public static void MyTestInitialize(TestContext test)
{
csvReader = new CSVReader();
csvReader.LoadTestValues("steam.csv");
}
[TestInitialize]
public void testInit()
{
testStart = DateTime.Now;
}
[TestMethod(), TestCategory("Reflection"), TestCategory("DataDriven"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", #"|DataDirectory|\CSV's\steam.csv", "steam#csv", DataAccessMethod.Sequential), DeploymentItem(#"..\..\CSV's\steam.csv")]
public void steamAccess()
{
testRead = new TestRead();
SteamMap a = new SteamMap();
testRead.Read(a, TestContext);
}
[TestCleanup]
public void TestCleanup()
{
uploadResults = new UploadResults();
//timer for each test ( in seconds )
double diffSecs = (DateTime.Now - testStart).TotalSeconds;
uploadResults.TestUpload(testRead.TestResults, csvReader.DataTable, diffSecs,TestContext);
}
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
private TestContext testContextInstance;
}
This runs perfectly from VS's Test Explorer, testContextInitialize variable gets initialized.
In the same Solution I've added a second project, a simple Windows Form application, added a reference to my DLL ( from References) which runs the following code :
Playback.Initialize();
DBTest a = new DBTest();
a.steamAccess();
Playback.Cleanup();
NullReferenceException occurs, my testContex is null when I run my test from outside it's assembly.
I need some help in this matter, Thanks
Edit 1:
Test Class :
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using System.Linq;
using System.Reflection;
using SimarpiDB.GLobal;
using SimarpiDB.UITestMaps.SteamMapClasses;
using SimarpiDB.Global;
using System.Data;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("TestLauncher")]
namespace SimarpiDB
{
[CodedUITest]
public class.....
Edit 2:
Temporary workaround until I find the cause of this error :
From my Windows Form I launch a MSTest with few required parameters and most of the what-methods-to-run logic will come from my database. I already did this few months ago but I scraped it because it's an additional performance overhead to use a tool such as MsTest within a launcher such as mine.
For anyone interested, there's a file located within VS's installation directory, it's called VsDevCmd.bat, I load this .bat within a hidden-in-background cmd with few additional commands ( mstest, testcontainer, test). This works but as I said I have no other plausible ideas.
There may also be a lack of referenced libraries within my Form ? Maybe something, a .dll that initialized the testenvironment and the testContext variable.
I wrote this because there may be others seeking the same result.
To clarify on my comment: internal is default in C#, so declaring something as abstract is like declaring it as "internal abstract".
For InternalsVisibleTo, you must make the library project visible to the test project, not the other way around:
[assembly:InternalsVisibleTo("MyTestProject")]
I am new to WCF and I am using "Learning WCF: A Hands-on Guide" book for now. The book has used VS2008 for the examples, and I am not sure what Visual Studio IDE to use for the examples. I tried using VS Express for Web and it gives the following error:
"HelloIndigo.exe does not contain static Main method suitable at entry point'.
I can understand the cause of the issue, but I am not sure where to add the main method. So I used VS Express for Desktop and it worked fine, but as I kept going in the first chapter I could not proceed as there are no WCF service templates in the VS Express for Desktop version. VS2012 is available only in trial version for free, and it expires in 90 days. So what IDE should I should be using? If the answer is VS Express for Web, then how to fix the error for the example in first chapter?
The example provided in the book is
Host:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService),new Uri("http://localhost:8000/HelloIndigo")))
{
host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new BasicHttpBinding(), "HelloIndigoService");
host.Open();
Console.WriteLine("Please <ENTER> to terminate the service host");
Console.ReadLine();
}
}
}
}
HelloIndigo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace HelloIndigo
{
public class HelloIndigoService : IHelloIndigoService
{
public string HelloIndigo()
{
return "Hello Indigo";
}
}
[ServiceContract(Namespace="http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
}
Client:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
EndpointAddress ep = new EndpointAddress("http://localhost:8000/HelloIndigo/HelloIndigoService");
IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new BasicHttpBinding(), ep);
string s = proxy.HelloIndigo();
Console.WriteLine(s);
Console.WriteLine("Please <ENTER> to terminate client");
Console.ReadLine();
}
}
}
ServiceProxy.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Client
{
class ServiceProxy
{
}
[ServiceContract(Namespace = "http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
}
HelloIndigo should be compiled as a library (DLL) and not an executable. So there should be no Main method - it doesn't have one as a class library.
The point of the Host is that it will host the service library HelloIndigo and start listening for calls on an endpoint for that particular service.
Change HelloIndigo to compile as a class library and add a reference to HelloIndigo in Host. Then start up the Host process.
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 new in programming. I've followed the steps in the book "Pro asp.net 4 in c#2010" to create 2 classes.
Now I try to use this class on a webpage. In the .cs file I added using "DBComponent;" but Visual Studio says "The type or namespace name 'DBComponent' could not be found (are you missing a using directive or an assembly reference?)"
What did I forget?
If i try to add the compiled dll under 'references' i get the error:
The type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs' conflicts with the imported type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\bin\Debug\DBComponent.dll'. Using the type defined in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs'.
This is what I've done:
Create a new empty website 'OSWeb' in VS
On top of this solution I clicked right and choosed: Add > new Project > Visual C# > Windows > Class library: name: DBComponent and I stored in D:_Web\OSWeb\DBcomponent
List item
I created 2 cs files (EventDB.cs and EventDetails.cs)
my events.cs file
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDB
{
private string connStr;
public EventDB()
{...}
public EventDB(string connectionString)
{...}
public int InsertEvent(EventDetails evd)
{...}
}
}
This is my eventdetails.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDetails
{
private int eventId;
private string eventName1;
public int EventId { get { return eventId; } set { eventId = value; } }
public string EventName1 { get { return eventName1; } set { eventName1 = value; } }
public EventDetails(int eventId, string eventName1)
{
this.eventId = eventId;
this.eventName1 = eventName1;
}
public EventDetails() { }
}
}
Now I create a new webpage (this is the .cs file of this webpage) (events.aspx.cs)
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBComponent; => here is the error
public partial class events : System.Web.UI.Page
{
private EventDB db = new EventDB(); => here is the same error
protected void Page_Load(object sender, EventArgs e)
{
}
}
try the following:
right click on your web project and click in properties, then click en references. remove the reference of your class, close this windows. Verify in the Bin folder from you proyect, yours references (should not be the reference has been removed.). Next, you open again the project properties and add your class library.
(before adding the reference should be sure that the class library compiles correctly.)
sorry for my english
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?