代码后servicehost.open方法不执行

问题描述:

我正在学习wcf和工作的示例。在此,我为wcf服务和2个Windows应用程序托管服务和另一个客户端appn构建了一个dll。在托管appn中,我无法在servicehost.open方法后执行代码。我只是想知道发生了什么事情。请帮忙。代码后servicehost.open方法不执行

在托管APPN的代码如下:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.ServiceModel; 
using System.ServiceModel.Description; 
using WCFService; 
namespace WCFServiceHost 
{ 
    public partial class Form1 : Form 
    { 
     ServiceHost sh = null; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      Uri tcpa = new Uri("net.tcp://localhost:8000/TcpBinding"); 

      sh = new ServiceHost(typeof(ServiceClass), tcpa); 
      NetTcpBinding tcpb = new NetTcpBinding(); 
      ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior(); 
      sh.Description.Behaviors.Add(mBehave); 
      sh.AddServiceEndpoint(typeof(IMetadataExchange), 
      MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); 
      sh.AddServiceEndpoint(typeof(IServiceClass), tcpb, tcpa); 
      sh.Open(); 
**//This line is not executed 
      label1.Text = "Service Running"; 
//This line is not executed** 
     } 
     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      sh.Close(); 
     } 
    } 
} 

一个更多的帮助:我们如何才能增加在Visual Studio服务引用2005年

+0

为什么你不能执行该行?打开服务时你会得到例外吗?它是什么? – 2012-04-26 12:52:52

+0

您好先生Maciej我已经复制了excepiton日志。检查下面的评论。 – chaitanyat 2012-04-27 09:18:18

+0

你确定你没有听到8000端口上的东西吗? – BugFinder 2012-04-27 10:19:19

有以下两种方式:

  • Form1_Load未被调用
  • sh.Open()或前面的一行是抛出异常

围绕您的代码尝试一下。在catch块中,将文本设置为例外的文本。

编辑

这是配股。当你做sh.Open()时,程序应该开始监听一个端口。听港口需要许可。

如果以“以管理员身份运行”启动程序,它应该可以工作。

+0

嗨mr shiraz,这是例外日志。我在多个评论框中评论这一点,因为整体超出了框的限制。 System.ServiceModel.CommunicationException:在IP Endpoint = 0.0.0.0:8000上侦听时发生TCP错误(10013:试图以其访问权限禁止的方式访问套接字)。 ---> System.Net.Sockets.SocketException:尝试以System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress) 在System上的访问权限 – chaitanyat 2012-04-27 09:10:44

+0

禁止的方式访问套接字。 Net.Sockets.Socket.Bind(EndPoint localEP) at System.ServiceModel.Channels.SocketConnectionListener.Listen() ---内部异常堆栈跟踪的末端--- 位于System.ServiceModel.Channels.SocketConnectionListener.Listen() 在System.ServiceModel.Channels.BufferedConnectionListener.Listen() 在System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen() 在System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener) – chaitanyat 2012-04-27 09:16:38

+0

异常发生在sh.open甲基OD。在 – chaitanyat 2012-04-27 09:29:35