2012-05-04 12 views
0

私は、クライアントがエンターキーを押すのを待ってから、ハードコードされた場所から別の場所にファイルをコピーするコンソールアプリケーションを持っていました。コンソールアプリケーションは、これはしかし、素晴らしい作品として、私は、Windowsサービスにそれを変換したときに、クライアントがEnterキーを押すと、私は次のエラーコードを取得する:Windowsサービスとして実行中のコンソールアプリケーション

"Could not connect to http://localhost:8080/myService. TCP error code 10061: 
No connection could be made because the target machine actively refused it 127.0.0.1:8080." 

私はポートが開いていることを確認するためにチェックし、ファイアウォールのことをしていますそれをブロックしていません。すべてが明確に表示されるので、私は何が起こっているのか混乱しています。以下で私はクライアントとホストコードを投稿して、それを見て、私が逃したものがあるかどうかを知らせて、私がこれを進めることができるようにしてください。

これは、これはクライアント

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

namespace myServiceClient 
{ 
    class Program 
    { 
     static void Main(string[] args) 

     Console.WriteLine("***** Prepairing to transfer files ***** \n"); 

     using (FileXferClient myMethod = new FileXferClient()) 
     { 
      Console.WriteLine("Press any key to begin transfer..."); 
      Console.ReadLine(); 
      string fileName = "test.txt"; 
      string sourcePath = @"C:\TestFromC"; 
      string targetPath = @"C:\TestFromC\Test Folder"; 

      myMethod.FileXfer(fileName, sourcePath, targetPath); 

      Console.WriteLine("File transfer is complete!"); 
     } 
      Console.ReadLine(); 
     } 
    } 
} 

myServiceLib(このロジックを持つ図書館で)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

namespace myServiceLib 
{ 
    public class myService : IFileXfer 
    { 
     public myService() 
     { 
      Console.WriteLine("Awaiting Files..."); 
     } 
     public void FileXfer(string fileName, string sourcePath, string targetPath) 
     { 
      string passFileName = fileName; 
      string passSourcePath = sourcePath; 
      string passTargetPath = targetPath; 

      string sourceFile = System.IO.Path.Combine(sourcePath, fileName); 
      string destFile = System.IO.Path.Combine(targetPath, fileName); 

      if (!System.IO.Directory.Exists(targetPath)) 
      { 
       System.IO.Directory.CreateDirectory(targetPath); 
      } 

      System.IO.File.Copy(sourcePath, destFile, true); 

      if (System.IO.Directory.Exists(sourcePath)) 
      { 
       string[] files = System.IO.Directory.GetFiles(sourcePath); 

       foreach (string s in files) 
       { 
        fileName = System.IO.Path.GetFileName(s); 
        destFile = System.IO.Path.Combine(targetPath, fileName); 
        System.IO.File.Copy(s, destFile, true); 
       } 
      } 
      else 
      { 
       Console.WriteLine("Source path does not exist!"); 
      } 

      Console.WriteLine("Press any key to exit."); 
      Console.ReadKey(); 
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

namespace myServiceLib 
{ 
    [ServiceContract(Namespace = "http://Intertech.com")] 
    public interface IFileXfer 
    { 
     //Transfer files from one the target machine to the destination location 
     [OperationContract] 
     void FileXfer(string fileName, string sourcePath, string targetPath); 
    } 
} 
+1

"' FileXfer'":音節省-良さを?いいえ、無意味です。 –

+0

クライアントが起動する前にサーバーが実行されていますか? – Leri

+0

はいサーバーは実際に動作しています。私はservices.mscを開き、クライアントが起動する前にそのプロセスがそこにあって実行されていることを確認しました。 – Jimmy

答えて

2

あなたは直後ServiceHostを閉じているあるホスト

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceProcess; 
using System.ServiceModel; 
using myServiceLib; 

namespace myServiceHost 
{ 
    class Program : ServiceBase 
    { 
     static void Main(string[] args) 
     { 
      ServiceBase.Run(new Program()); 
     } 

     public Program() 
     { 
      this.ServiceName = "myService"; 
     } 

     protected override void OnStart(string[] args) 
     { 
      base.OnStart(args); 

      using (ServiceHost serviceHost = new ServiceHost(typeof(myService))) 
      { 
       serviceHost.Open(); 
      } 
     } 
    } 
} 

です開始:

protected override void OnStart(string[] args) 
    { 
     base.OnStart(args); 

     using (ServiceHost serviceHost = new ServiceHost(typeof(myService))) 
     { 
      serviceHost.Open(); 
     } 
    } 

ステートメントの内部にあるため、OnStartメソッドが返される前にそれが破棄されます(Closeを呼び出すのと同じです)。

代わりに、ProgramクラスでserviceHostフィールドを作り、その後、持っている:

protected override void OnStart(string[] args) 
    { 
     base.OnStart(args); 

     serviceHost = new ServiceHost(typeof(myService)); 
     serviceHost.Open(); 
    } 

そして

protected override void OnStop() 
    { 
     serviceHost.Close(); 

     base.OnStop(); 
    } 
+0

それはそれだった...私はそれが見えないほど馬鹿だと感じる。非常に義務づけられた、あなたは私に多くの欲求不満と頭痛を救った。 – Jimmy

関連する問題