无法建立连接,因为目标计算机已主动拒绝127.0.0.1:8000

本文关键字:拒绝 8000 建立 连接 目标计算机 因为 | 更新日期: 2023-09-27 18:19:32

我已经创建了WCF服务,并尝试在托管Windows服务中托管(后面的文章)。该服务已启动并在服务中运行。

当尝试在客户端应用程序(net。tcp://localhost:8000/UserManagement)我得到错误:

元数据包含无法解析的引用:’net。tcp://localhost:8000/UserManagement"。无法连接到网tcp://localhost:8000/UserManagement.连接尝试持续时间跨度为00:00:00.95 31433。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>

无法建立连接,因为目标计算机已主动拒绝127.0.0.1:8000

您需要使用地址

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

当您配置服务引用时。

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

<serviceMetadata httpGetEnabled="true"/>

在使用您提供的MSDN链接后,我也遇到了同样的问题。那个代码中有一个错误。

在您的OnStart方法中,

serviceHost = new ServiceHost(typeof(UserManagementService));

不要为UserManagementService创建ServiceHost,而是在此处使用实际的WCF服务类名。这行代码将创建windows服务的实例,而不是WCF服务。我可以用它修复我的。