2012-03-27 47 views
3

試圖在我的Win 7系統上運行自託管應用程序,但收效甚微。該應用程序啓動,但我無法從WCF測試客戶端訪問它或通過在VS中添加引用。我已經閱讀了1000篇關於類似問題的文章,但沒有一篇似乎適合。無法訪問服務在非域上的Windows 7自託管WCF應用程序

我這樣做:

netsh http add urlacl url=http://+:9090/hello user=LocalPC\UserName 

然後將此:

netsh http add iplisten ipaddress=0.0.0.0:9090 

下面的代碼做

void MainWindow_Loaded(object sender, RoutedEventArgs e) 
{ 
     Uri baseAddress = new Uri("http://localhost:9090/hello"); 

     // Create the ServiceHost. 
     using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress)) 
     { 
      // Enable metadata publishing. 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpGetEnabled = true; 
      smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
      host.Description.Behaviors.Add(smb); 

      // Add MEX endpoint 
      host.AddServiceEndpoint(
       ServiceMetadataBehavior.MexContractName, 
       MetadataExchangeBindings.CreateMexHttpBinding(), 
       "mex"); 

      // Add application endpoint 
      host.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");     

      // Open the ServiceHost to start listening for messages. Since 
      // no endpoints are explicitly configured, the runtime will create 
      // one endpoint per base address for each service contract implemented 
      // by the service. 
      try 
      { 
       host.Open(); 
      } 
      catch (Exception excep) 
      { 
       string s = excep.Message; 
      } 
     } 
    } 

當我嘗試從WCF測試客戶端我可以訪問:

Error: Cannot obtain Metadata from http://localhost:9090/hello If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455 .
WS-Metadata Exchange Error URI: http://localhost:9090/hello
Metadata contains a reference that cannot be resolved: 'http://localhost:9090/hello'.
There was no endpoint listening at http://localhost:9090/hello that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:9090
HTTP GET Error URI: http://localhost:9090/hello There was an error downloading 'http://localhost:9090/hello'. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:9090

當我嘗試添加服務引用我得到:

There was an error downloading 'http://localhost:9090/hello'.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it
127.0.0.1:9090
Metadata contains a reference that cannot be resolved: 'http://localhost:9090/hello'.
There was no endpoint listening at http://localhost:9090/hello that could accept the
message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it 127.0.0.1:9090
If the service is defined in the current solution, try building the solution and adding the service reference again.

+0

您是否嘗試添加MEX行爲? http://stackoverflow.com/questions/6056329/error-cannot-obtain-metadata-from-wcf-service或http://stackoverflow.com/questions/6156461/error-cannot-obtain-metadata-from-http- 172-16-70-1258080-when-using-wcf-cli。你也可能想修復你的.NET軟件包[http://stackoverflow.com/questions/189436/wcf-errors-using-wcftestclient-to-test-a-simple-wcf-web-service] – Nayan 2012-03-27 02:24:09

回答

2

的問題是,你是讓ServiceHost的立即走出去的範圍。

使用語句是作爲一種方便的方式來清理時,代碼塊超出了範圍,但你沒有任何東西來阻止。因此,實質上你打開了連接,但是它幾乎是立即被處理的......這關閉了連接。

只要你沒有遇到任何權限問題,這種方法應該適合你。這就是說,這只是演示軟件。實際上,你可能不希望你的WCF服務直接綁定到你的表單,而是在應用程序級定義。

public partial class WcfHost : Form 
{ 
    private ServiceHost _svcHost; 
    private Uri _svcAddress = new Uri("http://localhost:9001/hello"); 

    public WcfHost() 
    { 
     _svcHost = new ServiceHost(typeof(HelloWorldService), _svcAddress); 

     ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
     smb.HttpGetEnabled = true; 
     smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
     _svcHost.Description.Behaviors.Add(smb); 

     InitializeComponent(); 

     FormClosing += WcfHost_FormClosing; 
    } 

    private void WcfHost_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      _svcHost.Open(TimeSpan.FromSeconds(10)); 
      lblStatus.Text = _svcHost.State.ToString(); 
     } 
     catch(Exception ex) 
     { 
      lblStatus.Text = ex.Message; 
     }    
    } 

    void WcfHost_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     _svcHost.Close(); 

     lblStatus.Text = _svcHost.State.ToString(); 
    } 
} 

[ServiceContract] 
public interface IHelloWorldService 
{ 
    [OperationContract] 
    string SayHello(string name); 
} 

public class HelloWorldService : IHelloWorldService 
{ 
    public string SayHello(string name) 
    { 
     return string.Format("Hello, {0}", name); 
    } 
} 
+0

這是正確的一點。 +1 – Nayan 2012-03-27 02:26:48

+0

這是完全正確的..我拿着一個控制檯應用程序的例子,並把它放到我的表單應用程序。該示例必須保持主機打開,直到輸入被按下爲止 謝謝 – 2012-03-27 09:51:23

相關問題