2012-03-27 18 views
4

私は別のビジュアルスタジオランタイムで新しいプロジェクトを開き、何も見つけられないウェブサービスを追加しようとすると、このサービスをうまく動作させようとしていますか?指定されたアドレスやローカルマシン上のものではありませんか?以下のコードは、同じソリューションで実行すると機能するようです。Webサービスが検出されませんでしたか?

namespace Students 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create the address for the service 
      Uri address = new Uri("http://localhost:8001"); 
      // Create the binding for the service 
      WSHttpBinding binding = new WSHttpBinding(); 
      // Create the service object 
      StudentService service = new StudentService(); 
      // Create the host for the service 
      ServiceHost host = new ServiceHost(service, address); 
      // Add the endpoint for the service using the contract, binding and name 
      host.AddServiceEndpoint(typeof(IStudentService), 
            binding, 
            "students"); 

      // Open the host 
      host.Open(); 
      Console.WriteLine("Student service started"); 
      Console.WriteLine("Press return to exit"); 
      Console.ReadLine(); 
      // Close the host 
      host.Close(); 
     } 
    } 


} 

私は(現在のソリューションへの別々の)新しいプロジェクトからそれを追加しようとすると、私は取得エラーは、このです:

私はこのケーススタディ(スタータープロジェクト)をダウンロードし
The HTML document does not contain Web service discovery information. 
Metadata contains a reference that cannot be resolved: 'http://localhost:8001/'. 
There was no endpoint listening at http://localhost:8001/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. 
The remote server returned an error: (404) Not Found. 
If the service is defined in the current solution, try building the solution and adding the service reference again. 

それはコンソールアプリケーションからホストされている場所には、ウェブやアプリの設定ファイルはありませんでした。

また、Webサービスを追加しようとするとサービスが実行されています。

+0

は、だからあなたは新しいVSを開いてhttp 'にサービス参照を追加しようとしている:// localhostを:8001'をし、それはあなたにエラーを与えますか?エラーは何ですか? – CodingGorilla

+0

私の知る限り、VSは単に任意のローカルサービスを自動的に検出するのではなく、ソリューションの一部のみを自動的に検出します。サービス参照テキストボックスにURLを入力しましたか?それは動作しましたか? –

+2

他のプロジェクトからサービス参照を追加しようとしているときにサービスホスト(コンソールアプリケーション)を実行していますか? – daryal

答えて

3

があなたのServiceHostにメタデータ交換の動作を追加します。

namespace Students 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create the address for the service 
      Uri address = new Uri("http://localhost:8001"); 
      // Create the binding for the service 
      WSHttpBinding binding = new WSHttpBinding(); 
      // Create the service object 
      StudentService service = new StudentService(); 
      // Create the host for the service 
      ServiceHost host = new ServiceHost(service, address); 
      // Add the endpoint for the service using the contract, binding and name 
      host.AddServiceEndpoint(typeof(IStudentService), 
            binding, 
            "students"); 

      // Add metadata exchange 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpGetEnabled = true; 
      host.Description.Behaviors.Add(smb); 

      // Open the host 
      host.Open(); 
      Console.WriteLine("Student service started"); 
      Console.WriteLine("Press return to exit"); 
      Console.ReadLine(); 
      // Close the host 
      host.Close(); 
     } 
    } 
} 

http://wcftutorial.net/WCF-Self-Hosting.aspx

関連する問題