2012-04-26 72 views
0

我正在學習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

回答

1

有以下兩種方式:

  • 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