2012-01-06 72 views
4

我已經創建了WCF服務並嘗試託管在託管Windows服務中(後面跟着article)。該服務已啓動並正在服務中運行。由於目標機器主動拒絕它,因此無法建立連接127.0.0.1:8000

當嘗試添加URL在客戶端應用程序(的net.tcp://本地主機:8000/UserManagement)我得到的錯誤:

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8000/UserManagement'. Could not connect to net.tcp://localhost:8000/UserManagement. The connection attempt lasted for a time span of 00:00:00.9531433. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8000. No connection could be made because the target machine actively refused it 127.0.0.1:8000 If the service is defined in the current solution, try building the solution and adding the service reference again.

Service.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 
using System.ServiceModel; 
using System.ServiceProcess; 
using System.Configuration; 
using System.Configuration.Install; 

namespace AddUser 
{ 
public class UserManagement : IUserManagement 
{ 

    public bool AddUser(string strName, DateTime dtDOB, string strGender, string strRole) 
    { 

     return true; 
    } 
} 

[ServiceContract] 
public interface IUserManagement 
{ 
    [OperationContract] 
    bool AddUser(string strLname,string strFName, string strUname, string strPswd, DateTime dtDOB, string strGender, string strRole, string strHobbies); 

} 

public class UserManagementService : ServiceBase 
{ 
    public ServiceHost serviceHost = null; 
    public UserManagementService() 
    { 
     ServiceName = "WCFUserManagementService"; 
    } 

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

    protected override void OnStart(string[] args) 
    { 
     if (serviceHost != null) 
     { 
      serviceHost.Close(); 
     }       
     serviceHost = new ServiceHost(typeof(UserManagementService)); 
     serviceHost.Open(); 
    } 

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

[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 = "WCFUserManagementService"; 
     Installers.Add(process); 
     Installers.Add(service); 
    } 
} 

} 

的app.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<system.serviceModel> 
    <services> 
     <service behaviorConfiguration="AddUser.UserManagementServiceBehavior" name="AddUser.UserManagement"> 
     <endpoint address="" binding="netTcpBinding" contract="AddUser.IUserManagement"/> 
     <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> 
     <host> 
     <baseAddresses> 
     <add baseAddress="net.tcp://localhost:8000/UserManagement" /> 
     </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="AddUser.UserManagementServiceBehavior"> 
       <serviceMetadata httpGetEnabled="false"/> 
       <serviceDebug includeExceptionDetailInFaults="False"/> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
</system.serviceModel> 
</configuration> 
+0

不應該baseAddress是例如 – Petesh 2012-01-06 10:15:31

+0

示例http:// localhost:8000 /我甚至嘗試使用localhost:8000仍然無法正常工作 – BABA 2012-01-06 11:08:45

回答

1

您需要使用地址

net.tcp://localhost:8000/UserManagement/mex 

當您配置您的服務參考。

或者您的元數據終結應該使用mexHttpBinding,你應該設置你的httpGetEnabled爲true在服務行爲

<serviceMetadata httpGetEnabled="true"/> 
1

我也面臨着同樣的問題,下面你所提供的MSDN鏈接後。該代碼中存在錯誤。

在您OnStart方法,

serviceHost = new ServiceHost(typeof(UserManagementService)); 

,而不是爲UserManagementService創建ServiceHost的,在這裏用你的實際WCF服務類名。這行代碼將創建一個Windows服務的實例,而不是WCF服務。我能夠使用它修復我的問題。

相關問題