加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:22:52 所属栏目:百科 来源:网络整理
导读:我创建了WCF服务并尝试在托管 Windows服务中托管(遵循 article).该服务已在服务中启动并运行. 当尝试在客户端应用程序中添加URL(net.tcp:// localhost:8000 / UserManagement)时,我收到错误: Metadata contains a reference that cannot be resolved: ‘n
我创建了WCF服务并尝试在托管 Windows服务中托管(遵循 article).该服务已在服务中启动并运行.

当尝试在客户端应用程序中添加URL(net.tcp:// localhost: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,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>

解决方法

您需要使用该地址

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

在配置服务引用时.

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

<serviceMetadata httpGetEnabled="true"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读