2011-07-25 123 views
0

我試圖創建一個Windows服務以及WCF。我們不想使用IIS來處理服務的代理。我已經註冊並開始了服務。我創建了一個簡單的控制檯應用程序來調用服務,但超時。WCF/Windows服務不工作

我有一個.NET DLL包含函數調用本地運行的應用程序來創建一個警報記錄。我創建了一個使用AlarmLib.dll的表單應用程序,它可以進行呼叫並插入警報記錄。

它似乎是一個權限問題。我收到以下例外情況:

> Unhandled Exception: System.ServiceModel.EndpointNotFoundException: 
> Could not co nnect to http://localhost:8000/ServiceModel/service. TCP 
> error code 10061: No co nnection could be made because the target 
> machine actively refused it 127.0.0.1: 
> 8000. ---> System.Net.WebException: Unable to connect to the remote 
> server ---> System.Net.Sockets.SocketException: No connection could 
> be made because the tar get machine actively refused it 
> 127.0.0.1:8000 
> at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, 
> SocketAddre ss socketAddress) at 
> System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at 
> System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, 
> Sock et s4, Socket s6, Socket& socket, IPAddress& address, 
> ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, 
> Exception& exception) --- End of inner exception stack trace 
> at System.Net.HttpWebRequest.GetRequestStream(TransportContext& 
> context) at System.Net.HttpWebRequest.GetRequestStream() at 
> System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStre 
> am() --- End of inner exception stack trace --- 

我以管理員身份運行VS2010。我可以修改的服務的任何其他設置?

感謝, 約翰

Windows服務代碼:

using System.ServiceProcess; 
using System.Text; 
using System.ServiceModel; 
using System.Configuration; 
using System.Configuration.Install; 
using AlarmLib; 

namespace Test.ServiceModel 

.WindowsServices { 



     // 

Define a service contract. 
    [ServiceContract(Namespace = "http://ServiceModel.WindowsServices")] 
    public interface IAlarmLib { 
     [OperationContract] 
     bool CreateNoDataAlarm(string well, string run, string record, string description, string selectedVariable); 
    } 

    // Implement the IAlarmLib service contract in a service class. 
    public class AlarmLibService : IAlarmLib { 
     // Implement the IAlarmLib methods. 


    public bool CreateNoDataAlarm(string well, string run, string record, string description, string sel 

ectedVariable) { 
      AlarmUserConfiguration newalarm = new AlarmUserConfiguration(); 

      // The Machine name should be the machine which is running the client application 
      // and which calls the webservice. This should not be the name of machine hosting 

     // webservice. 
     newalarm.MachineName = System.Environment.MachineName; 
     newalarm.AlarmType = AlarmTypes.NoData; 
     newalarm.TimeInSeconds = 30; 
     DateTime CreationTime = DateTime.Now; 
     newalarm.Name = "NoDataAlarm " + CreationTime.ToString(); 
     PrimaryKey key = new PrimaryKey(); 
     key.Well = "Well ID 1"; 
     key.Run = "1600"; 
     key.Record = "DGR"; 
     key.Desc = "Realtime"; 
     key.SelectedVariable = "Gamma Ray A"; 
     newalarm.PrimaryKeys = key; 

     // Add any of the following activities. 
     /*"All" 
     "Trip Out" 
     "Trip In" 
     "Circulating" 
     "Drilling On Bottom" 
     "Drilling Off Bottom"*/ 

     newalarm.TDActivities.Add("Drilling On Bottom"); 

     bool bStatus = AlarmUtilities.AddNewAlarm(newalarm, "-Local-"); 
     return bStatus; 
    } 
} 

public class AlarmLibWindowsService : ServiceBase { 
    public ServiceHost serviceHost = null; 
    public AlarmLibWindowsService() { 
     // Name the Windows Service 
     ServiceName = "AlarmLibWS"; 
    } 

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

    // Start the Windows service. 
    protected override void OnStart(string[] args) { 
     if (serviceHost != null) { 
      serviceHost.Close(); 
     } 

     // Create a ServiceHost for the AlarmLibService type and 
     // provide the base address. 
     serviceHost = new ServiceHost(typeof(AlarmLibService)); 

     // Open the ServiceHostBase to create listeners and start 
     // listening for messages. 
     serviceHost.Open(); 
    } 

    protected override void OnStop() { 
     if (serviceHost != null) { 
      serviceHost.Close(); 
      serviceHost = null; 
     } 
    } 
} 

// Provide the ProjectInstaller class which allows 
// the service to be installed by the Installutil.exe tool 
[RunInstaller(true)] 
public class ProjectInstaller : Installer { 
    private ServiceProcessInstaller process; 
    private ServiceInstaller service; 

    public ProjectInstaller() { 
     process = new ServiceProcessInstaller(); 


      process.Account = ServiceAccount.LocalSystem; 
      service = new ServiceInstaller(); 
      service.ServiceName = "AlarmLibWS"; 
      Installers.Add(process); 
      Installers.Add(service); 
     } 
    } 
} 

的App.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <services> 
     <!-- This section is optional with the new configuration model 
      introduced in .NET Framework 4. --> 
     <service name="ServiceModel.WindowsServices.AlarmLibService" behaviorConfiguration="AlarmLibServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8000/ServiceModel/service"/> 
      </baseAddresses> 
     </host> 
     <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModel/service --> 
     <endpoint address="" 
        binding="wsHttpBinding" 
        contract="ServiceModel.WindowsServices.IAlarmLib" /> 
     <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModel/service/mex --> 
     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="AlarmLibServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

控制檯應用程序:

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

namespace TestAlarmLibConsoleApp { 
    class Program { 
     static void Main(string[] args) { 
      ServiceReference1.AlarmLibClient client = new ServiceReference1.AlarmLibClient(); 
      bool result = client.CreateNoDataAlarm("Well ID 1", "1100", "DGR", "Realtime", "Gamma Ray A"); 
      Console.WriteLine("result = " + bool.TrueString); 
     } 
    } 
} 

這裏是形式的應用程序,工作代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 
using AlarmLib; 

namespace TestCallingAlarmLib { 
    static class Program { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 

      bool result = CreateNoDataAlarm(); 

      Application.Run(new Form1()); 
     } 

     static bool CreateNoDataAlarm() { 
      AlarmUserConfiguration newalarm = new AlarmUserConfiguration(); 

      // The Machine name should be the machine which is running the client application 
      // and which calls the webservice. This should not be the name of machine hosting 
      // webservice. 
      newalarm.MachineName = System.Environment.MachineName; 
      newalarm.AlarmType = AlarmTypes.NoData; 
      newalarm.TimeInSeconds = 30; 
      DateTime CreationTime = DateTime.Now; 
      newalarm.Name = "NoDataAlarm " + CreationTime.ToString(); 
      PrimaryKey key = new PrimaryKey(); 
      key.Well = "Well ID 1"; 
      key.Run = "1100"; 
      key.Record = "DGR"; 
      key.Desc = "Realtime"; 
      key.SelectedVariable = "Gamma Ray A"; 
      newalarm.PrimaryKeys = key; 

      // Add any of the following activities. 
      /*"All" 
      "Trip Out" 
      "Trip In" 
      "Circulating" 
      "Drilling On Bottom" 
      "Drilling Off Bottom"*/ 

      newalarm.TDActivities.Add("Drilling On Bottom"); 

      bool bStatus = AlarmUtilities.AddNewAlarm(newalarm, "-Local-"); 
      return bStatus; 
     } 
    } 
} 
+0

你可以在Windows服務中顯示代碼的基礎知識嗎? –

+0

您是否嘗試過調試您的代碼?如果你在超時狀態下闖入代碼會發生什麼? –

+0

如何將不同的項目添加到我的解決方案中,以便我可以調試代碼並查看它失敗的位置?我需要包含AlarmLib.dll項目嗎? – John

回答

1

檢查您的客戶端訪問策略,請確保您有端口的服務器上打開了,仔細檢查你的防火牆。每次我得到這個錯誤都與其中的一個有關。