问题
I've been banging my head against a brick-wall the past 2 days trying to successfully use this example on MSDN:
How to: Host WCF in a Windows Service Using TCP
If you follow the example through and place the Consumer application within the same solution then it runs successfully - even if the service is turned off !
If the service is switched on and the consumer application is published to say a different network drive then it will not run because the WCF is not listening.
The following piece of code in the walk-through is the culprit:
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(Service1));
myServiceHost.Open();
}
My friend noticed that if we change new ServiceHost(typeof(Service1));
to new ServiceHost(typeof(WcfServiceLibrary1.Service1));
then the WCF will actually start to listen!
Easy to see it's confusion as there is a class called Service1 within the windows service project aswell as the WCF project so the full qualification is required.
Did anyone else encounter this?
回答1:
I had the same issue (as you know from the original post in stackoverflow).
Alternatively, you can rename the class Service1 in the namespace WcfServiceLibrary1 to avoid conflicts.
回答2:
you have to put using WcfServiceLibrary1;
at the top
回答3:
I also found that the the article references the wrong Service Name. When I tried it I had to use:
net.tcp://localhost:8526/Service1/mex
in step 8 instead of what was listed.
net.tcp://localhost:8526/Service1
来源:https://stackoverflow.com/questions/12761107/an-error-in-the-msdn-walk-through-how-to-host-wcf-in-a-windows-service-using