宿主程序代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Console Based WCF Host *****");
try
{
using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService)))
{
// Open the host and start listening for incoming messages.
serviceHost.Open();
//DisplayHostInfo(serviceHost);
// Keep the service running until Enter key is pressed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press the Enter key to terminate service.");
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
#region Show all the ABCs exposed from the host.
static void DisplayHostInfo(ServiceHost host)
{
Console.WriteLine();
Console.WriteLine("***** Host Info *****");
foreach (System.ServiceModel.Description.ServiceEndpoint se in host.Description.Endpoints)
{
Console.WriteLine("Address: {0}", se.Address);
Console.WriteLine("Binding: {0}", se.Binding.Name);
Console.WriteLine("Contract: {0}", se.Contract.Name);
Console.WriteLine();
}
Console.WriteLine("**********************");
}
#endregion
}
客户端程序代码
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Ask the Magic 8 Ball *****\n");
try
{
using (EightBallClient ball = new EightBallClient())
{
Console.Write("Your question: ");
string question = Console.ReadLine();
string answer =
ball.ObtainAnswerToQuestion(question);
Console.WriteLine("8-Ball says: {0}", answer);
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
}
}
宿主程序运行错误如下:
客户端程序运行错误如下:
客户端InnerException详细信息如下:
{System.Net.Sockets.SocketException (0x80004005): 由于目
标计算机积极拒绝,无法连接。 127.0.0.1:8080 在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
在 System.Net.ServicePoint.ConnectSocketInternal
(Boolean connectFailure, Socket s4, Socket s6, Socket&
socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)}
大概在百度与google上找了找解决方案如下链接未解决:
http://social.msdn.microsoft.com/Forums/zh-CN/wcfzhchs/thread/e7b3fa10-abcf-4986-86eb-33825526e1da 解决方案为程序少了Console.Read(),对我不可行
http://social.msdn.microsoft.com/Forums/en-US/wcfzhchs/thread/393fa2b6-554b-42bf-a88e-24fe6402c39f 同样的书基本同样的问题,但是看不懂楼主所说的文件夹少了
http://zhidao.baidu.com/question/194073402.html 写了一些原因,但是不太看懂
http://78138971.blog.163.com/blog/static/291606620081116236519/ 8080端口被占用
以下是现在目测较有价值且未仔细研究的链接
http://blog.csdn.net/EricBai/article/details/3202204 占用8080端口之后的解决方法
http://blog.sina.com.cn/s/blog_53e5d4330100sv32.html 一个搬英文博客的中文博客,有英文错误,之后可以用这个错误google更多方案,可以stackoverflow
http://www.cnblogs.com/zengen/archive/2011/07/14/2106347.html 防火墙
http://www.flmuch.com/archives/411 8080端口与win7权限问题
有学习价值的wcf第一个程序博客
http://www.cnblogs.com/artech/archive/2007/02/26/656901.html
留稿备今后继续解决