The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by johnchaf]

So far I've got the SvcPokService installed and running as a Windows service.
The problem is with the client application. The code for the poker client is
provided on page 154 of "Microsoft .NET for Programmers" (by Fergal Grimes).
I'm using Visual Studio .NET, Visual Studio "Build" menu commands, and of
course the SvcPokService compiles and then installs as an SvcPokService.EXE
service. The SvcPok code has lines like "SvcPokService service" -- thus the
SvcPok code will not build because it needs a reference to a non-existent
SvcPokService.DLL (the Add Reference process will not reference an .EXE). In
general, I don't see how a client can reference a Windows Service that is, by
it's very nature, an .EXE. And therefore I don't understand how to code a
client so it will compile. Thanks very much in advance for solving this
problem.
import-bot (20211) [Avatar] Offline
#2
Re: SvcPok won't compile without .dll reference
[Originally posted by johnchaf]

Still not working -- but you need to realize that I don't really understand
what I'm doing: only explicit instructions are helpful.

Nevertheless, I created a separate dll named "Services" which is referenced by
both SvcPokService and SvcPok. And everything does compile. But when I try to
run SvcPok, I get "Trying to create a proxy to an unbound type" in SvcPok at
the instruction:

Services service = (Services)Activator.GetObject
(
//typeof(Poker.SvcPokService),
//typeof(Poker.Services),
//typeof(MarshalByRefObject),
typeof(Services),
url
);

As you can see, I've tried other combinations, but am clueless: I just don't
understand the "typeof" requirement.

******************************************
Currently, my SvcPok code looks like this:
******************************************

namespace Poker
{
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Proxies;

class SvcPok
{
//private SvcPokService service = null;
private Services service = null;

public static void Main(string[] args)
{
Console.WriteLine("Starting HTTP SvcPok client...");
ChannelServices.RegisterChannel(new HttpChannel());
string url = "http://localhost:6789/SvcPokService"; //http localhost
channel name
//SvcPokService service = (SvcPokService)Activator.GetObject
Services service = (Services)Activator.GetObject
(
//typeof(Poker.SvcPokService), //CANNOT USE SvcPokService IS AN .EXE
//typeof(Poker.Services),//WRONG trying to create proxy to an unbound type
//typeof(MarshalByRefObject), //WRONG specified cast is not valid
typeof(Services), //WRONG trying to create proxy to an unbound type
url
);

new SvcPok(service); // start game

// CustomProxy prx = new CustomeProxy(typeof(BaseRemoteObject),
"http://localhost:6789/SvcPokService");
// BaseRemoteObject brObj = (BaseRemoteObject) prx.GetTransparentProxy();

}

public SvcPok(Services service)
{
this.service = service;
Console.WriteLine("A Windows service-based poker game...");
Console.WriteLine("Hit Ctrl-c at any time to abort.
");
while (true) nextGame(); // play
}

private void nextGame()
{

string dealHand = service.Deal(); // deal hand
Console.WriteLine(dealHand); // display it
Console.Write("Enter card numbers (1 to 5) to hold: ");
string holdCards = Console.ReadLine();

GameResult res = service.Draw(dealHand, holdCards);
Console.WriteLine(res.Hand);
Console.WriteLine(res.Title);
Console.WriteLine("Score = {0}
", res.Score);
}
}
}

*********************************************
Currently, SvcPokService code looks like this
*********************************************

namespace Poker
{
using System;
using System.ServiceProcess;
using System.Runtime.Remoting;
using System.Configuration.Install;
using System.ComponentModel;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

public class SvcPokService : MarshalByRefObject
{
public class PokerService : ServiceBase
{
public PokerService()
{
CanPauseAndContinue = false;
CanStop = true;
ServiceName = "PokerService";
}

protected override void OnStart(string[] args)
{
ChannelServices.RegisterChannel(new HttpChannel(6789));
RemotingConfiguration.RegisterWellKnownServiceType(
new SvcPokService().GetType(), "SvcPokService",
WellKnownObjectMode.Singleton); //Activation mode
}

public static void Main()
{
ServiceBase.Run(new PokerService());
}

}//end public class PokerService

}//end public class SvcPokService

[RunInstallerAttribute(true)]
public class PokerServiceInstaller : Installer
{
public PokerServiceInstaller()
{
ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
ServiceInstaller serviceInstaller = new ServiceInstaller();
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "PokerService";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}

}//end namespace Poker

************************************************
Currently, the new "Services" dll has this code:
************************************************

namespace Poker
{
using System;

[Serializable]
public struct GameResult
{
public string Hand;
public int Score;
public string Title;
}

public class Services
{
public Services()
{
//
// TODO: Add constructor logic here
//
}

public String Deal()
{
String hand = new SimpleMachine().Deal().Text;
Console.WriteLine("Dealing : {0}", hand);
return hand;
}

public GameResult Draw(string oldHand, string holdCards)
{
GameResult g = new GameResult();
Hand h = new SimpleMachine().Draw(oldHand, holdCards);
g.Hand = h.Text;
g.Score = h.Score;
g.Title = h.Title;
Console.WriteLine("Drawing : {0} ({1}", g.Hand, g.Title);
return g;
}

}//end public class Services

}//end namespace Poker


*****************************************************
And finally, the PokerLib has the following routines:
*****************************************************

Card.cs
Hand.cs
SimpleMachine.cs
import-bot (20211) [Avatar] Offline
#3
Re: SvcPok won't compile without .dll reference
[Originally posted by fgrimes]

Hi John,

We haven't had a problem with this.

>> I'm using Visual Studio .NET, Visual Studio "Build" menu commands

Compile instead from the command line as shown on page 154:

csc /r:SvcPokService.exe;poker.dll SvcPok.cs

That should do it.

Regards,
Fergal