2012-03-27 21 views
3

Win 7システムで自己ホストアプリケーションを実行しようとしましたが、ほとんど成功しませんでした。アプリケーションが起動しますが、WCF Test Clientからアクセスすることはできません。また、VSに参照を追加することもできません。私は同様の問題について1000件の投稿があるようだが、解決策のどれもが適合していないようだ。サービスにアクセスできないドメインでないWindows 7の自己ホストWCFアプリ

私はこれでした:ここで私は、WCFテストクライアントからアクセスしようとすると、私は取得

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; 
      } 
     } 
    } 

を行うためのコードだ

netsh http add iplisten ipaddress=0.0.0.0:9090 

:その後、

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

そして、これを:

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

012私は、サービス参照を追加しようとすると

は私が取得:

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-使用時wcf-cli。またあなたの.NETパッケージを修復したいかもしれません[http://stackoverflow.com/questions/189436/wcf-errors-using-wcftestclient-to-test-a-simple-wcf-web-service] – Nayan

答えて

2

問題は、あなたがのServiceHostはすぐにスコープの外に出すことです。

usingステートメントは、そのコードブロックが範囲外になったときにクリーンアップを行うのが便利であるが、そのことを防ぐために何も用意されていません。したがって本質的には接続を開いていますが、接続はすぐに終了します。

権限の問題に遭遇していない限り、この方法が有効です。つまり、これは単なるデモウェアです。現実には、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

+0

これはまさに正しかった..私はコンソールアプリケーションのための例を取っ​​て、私のフォームアプリに入れた。その例は、入力が押されるまでホストを開いたままにしなければならなかった ありがとう –

関連する問題