2016-05-19 25 views
1

私は見てきましたが、いくつか似たような質問はありませんでした。私はWCFを手に入れようとしています。理由はWindowsフォームアプリケーションで接続できるWindowsサービスを作成したいからです。私はいくつかのチュートリアルに従ってきたし、インストールして、私のWindows 7デバイス上で実行されている次のWindowsサービスを作成しました:C# - WCF:エンドポイントのリッスンがありませんでした

using System; 
using System.ServiceModel; 
using System.ServiceProcess; 

[ServiceContract] 
public interface HelloWorld 
{ 
    [OperationContract] 
    string SayHello(string value); 
} 

public class Hello : HelloWorld 
{ 
    public string SayHello(string value) 
    { 
     string retVal = ""; 
     if (value == "Hello") 
     { 
      retVal = "Hello World!"; 
     } 
     else 
     { 
      retVal = "Say hello to me..."; 
     } 
     return (retVal); 
    } 
} 

class PluginService : ServiceBase 
{ 
    public PluginService() 
    { 
     this.ServiceName = "Test Plugin Service"; 
     this.CanStop = true; 
     this.CanShutdown = true; 
    } 

    static void Main() 
    { 
     ServiceBase.Run(new PluginService()); 
    } 

    protected override void OnStart(string[] args) 
    { 
     using (ServiceHost host = new ServiceHost(typeof(Hello), 
      new Uri[]{ 
      new Uri("http://localhost:8998"), 
      new Uri("net.pipe://localhost") 
     })) 
     { 
      host.AddServiceEndpoint(typeof(HelloWorld), new BasicHttpBinding(), "Hello"); 
      host.AddServiceEndpoint(typeof(HelloWorld), new NetNamedPipeBinding(), "PipeHello"); 
      host.Open(); 
     } 
    } 

    protected override void OnStop() 
    { 

    } 

    protected override void OnShutdown() 
    { 

    } 

} 

は、私はまた、サービスに接続する必要があり、非常に基本的なコンソールアプリケーションを作成しました。アイデアは、あなたがこのコンソールユーティリティを使用して「こんにちは」と言っており、サービスリターンの「Hello World」:

using System; 
using System.ServiceModel; 


namespace PluginApplication 
{ 

    [ServiceContract] 
    public interface HelloWorld 
    { 
     [OperationContract] 
     string SayHello(string value); 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      ChannelFactory<HelloWorld> httpFactory = 
       new ChannelFactory<HelloWorld>(
       new BasicHttpBinding(), 
       new EndpointAddress(
        "http://localhost:8998/Hello")); 

      ChannelFactory<HelloWorld> pipeFactory = 
       new ChannelFactory<HelloWorld>(
       new NetNamedPipeBinding(), 
       new EndpointAddress(
        "net.pipe://localhost/PipeHello")); 

      HelloWorld httpProxy = httpFactory.CreateChannel(); 
      HelloWorld pipeProxy = pipeFactory.CreateChannel(); 

      while (true) 
      { 
       try 
       { 
        Console.WriteLine("Say something..."); 
        string str = Console.ReadLine(); 
        Console.WriteLine("http: " + 
         httpProxy.SayHello(str)); 
        Console.WriteLine("pipe: " + 
         pipeProxy.SayHello(str)); 
       } 
       catch (Exception Ex) 
       { 
        Console.WriteLine(Ex); 
       } 
      } 
     } 
    } 
} 

私は、コンソールと入力し、「こんにちは」を実行すると、私は数秒後に次のエラーを取得します:

System.ServiceModel.EndpointNotFoundException:メッセージを受け入れることができるhttp://localhost:8998/Helloで何のエンドポイントlisteniongはありませんでした。これは、しばしば不正なアドレスまたはSOAPアクションによって引き起こされます。詳細については、InnerException(存在する場合)を参照してください。ターゲットマシンが積極的にSystem.Net.Sockets.Socket.DoConnect(エンドポイントendPointSnapshot、のSocketAddressのSocketAddress)で127.0.0.1:8998

それを拒否したファイアウォールは完全に私のデバイスで無効になっているので、何も接続は、行われないことができターゲットマシンが接続を積極的に拒否した理由がわかりません。誰かがこれでいくつかの光を当てることができますか?

答えて

1

OnStart()メソッドでusing構成を使用します。つまり、そのメソッドが完了すると、作成したServiceHostオブジェクトは使用できなくなり、定義したエンドポイントも使用できなくなります。代わりに、ServiceHost変数を静的にWindowsサービスに格納します。

+0

ありがとうございます。私は今、私のコードを調整することができたし、それは動作します。 :) – Rawns

+0

喜んで:) –

関連する問題