簡體   English   中英

無法建立連接,因為目標計算機主動拒絕了127.0.0.1:8000

[英]No connection could be made because the target machine actively refused it 127.0.0.1:8000

我已經創建了WCF服務,並嘗試在Managed Windows Service中托管(接下去的文章 )。 該服務已啟動並正在服務中運行。

嘗試在客戶端應用程序中添加URL(net.tcp:// localhost:8000 / UserManagement)時,出現錯誤:

元數據包含無法解析的引用:“ 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>

跟隨您提供的MSDN鏈接后,我也面臨同樣的問題。 該代碼有錯誤。

在您的OnStart方法中,

serviceHost = new ServiceHost(typeof(UserManagementService));

而不是為UserManagementService創建ServiceHost,請在此處使用實際的WCF服務類名稱。 這行代碼將創建Windows服務而不是WCF服務的實例。 我能夠使用它修復我的。

您需要使用地址

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

在配置服務參考時。

或者,您的元數據終結點應使用mexHttpBinding並且應在服務行為httpGetEnabled設置為true

<serviceMetadata httpGetEnabled="true"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM