2012-05-04 2 views
1

私はwinformsアプリケーションでセルフホストされているWCFサービスを持っています。私は、以下のリンクを使用: サービスの追加に失敗しました: テストクライアントはWCFに接続できません。winformsアプリケーションでセルフホストされているサービス

私はWCFテストクライアントを使用して、私は次のエラーを取得するサービスを追加しよう

。サービスメタデータにアクセスできない可能性があります。サービスが実行中で、メタデータが公開されていることを確認してください。

エラーの詳細:

Error: Cannot obtain Metadata from http://localhost:8001/HelloWorld 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:8001/HelloWorld

Metadata contains a reference that cannot be resolved: 'http://localhost:8001/HelloWorld'.

There was no endpoint listening at http://localhost:8001/HelloWorld 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:8001HTTP GET Error URI: http://localhost:8001/HelloWorld

There was an error downloading 'http://localhost:8001/HelloWorld'.

Unable to connect to the remote server

No connection could be made because the target machine actively refused it 127.0.0.1:8001

ここに私のコードです:

public Server() 
{ 
    InitializeComponent(); 

    using (host = new ServiceHost(typeof(HelloWorldService), new Uri("http://localhost:8001/HelloWorld"))) 
    { 
     // Check to see if the service host already has a ServiceMetadataBehavior 
     ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); 
     // If not, add one 
     if (smb == null) 
      smb = new ServiceMetadataBehavior(); 
     smb.HttpGetEnabled = true; 
     smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
     host.Description.Behaviors.Add(smb); 

     //You need to add a metadata exchange (mex) endpoint to your service to get metadata. 
     host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); 

     //http 
     host.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), ""); 

     host.Open(); 
    } 
} 

[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

に動作します。このお試しください ためのソリューションであるブロックされたポート8001ですか? –

+0

@ jskiles1 Windowsのファイアウォールを無効にしました。まだ動かない。 – Mausimo

答えて

1

私は理由はわかりませんが、using(){}をtry {} .. catch {}に切り替えると、このコードは正しく機能します。 WCFテストクライアントが正常にサービスを追加することができますし、私は経由で実行中のサービスを参照することができますhttp://localhost:8001/HelloWorldここ

+2

それはあなたのサービスを処分することになるからです。 'Console.ReadLIne()'や、 'host.Open()'の後に実行を一時停止するのに似たものを追加すると、 'using'でも動作します。 –

+0

@WiktorZychlaありがとう、それは今意味があります。 – Mausimo

0

根本的な原因は、一番下にダウンしているようだ:

Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8001

私はあなたのイベントログを確認しますhost.Open()呼び出しが(おそらくファイアウォールなどの理由で)失敗しているかどうかを確認したり、デバッガ実際に8001でリッスンしているかどうかを確認するためにtelnetを使います。

+0

私はプログラムを実行し、telnetを使って試しました。 "telnet 127.0.0.1 8001"。 「接続先127.0.0.1 ...ポート8001のホストへの接続を開けませんでした:接続に失敗しました」というメッセージが表示されます。私はこれをテストしている間にWindowsのファイアウォールを回したデバッガでコードをステップ実行すると、host.open()は例外をスローしません。 – Mausimo

+0

Windowsイベントビューアを意味しますか? – Mausimo

0

それは

Uri baseAddress = new Uri("http://localhost:8001/HelloWorld"); 

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

     // 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. 
     serviceHost.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), ""); 
     serviceHost.Open(); 

    } 
関連する問題