【问题标题】:No connection could be made because the target machine actively refused it 127.0.0.1:8000无法建立连接,因为目标机器主动拒绝它 127.0.0.1:8000
【发布时间】:2012-02-04 01:55:38
【问题描述】:

我已创建 WCF 服务并尝试在托管 Windows 服务中托管(关注 article)。该服务已启动并在服务中运行。

尝试在客户端应用程序 (net.tcp://localhost:8000/UserManagement) 中添加 URL 时出现错误:

元数据包含无法解析的引用: 'net.tcp://localhost:8000/UserManagement'。无法连接到 net.tcp://localhost:8000/UserManagement。连接尝试持续 时间跨度为 00:00:00.9531433。 TCP 错误代码 10061:否 可以建立连接,因为目标机器主动拒绝 它是 127.0.0.1:8000。无法建立连接,因为目标 机器主动拒绝它 127.0.0.1:8000 如果服务被定义 在当前解决方案中,尝试构建解决方案并添加 再次引用服务。

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>

【问题讨论】:

  • 不应像示例中那样 baseAddress 为 localhost:8000
  • 我试过即使 localhost:8000 仍然无法正常工作

标签: c# wcf


【解决方案1】:

你需要使用地址

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

当您配置服务参考时。

或者,您的元数据端点应该使用 mexHttpBinding,并且您应该在服务行为中将 httpGetEnabled 设置为 true

<serviceMetadata httpGetEnabled="true"/>

【讨论】:

    【解决方案2】:

    按照您提供的 MSDN 链接后,我也遇到了同样的问题。该代码中有错误。

    在你的 OnStart 方法中,

    serviceHost = new ServiceHost(typeof(UserManagementService));
    

    不要为 UserManagementService 创建 ServiceHost,而是在此处使用您的实际 WCF 服务类名称。这行代码将创建一个 Windows 服务实例,而不是 WCF 服务。我能够使用它来修复我的问题。

    【讨论】:

      猜你喜欢
      • 2015-04-05
      • 1970-01-01
      • 2012-03-30
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 2018-07-12
      相关资源
      最近更新 更多