客户端在 Windows 服务内找不到 WCF 的端点元素

Posted

技术标签:

【中文标题】客户端在 Windows 服务内找不到 WCF 的端点元素【英文标题】:Client can't find endpoint element of WCF Inside Windows Service 【发布时间】:2018-07-09 20:39:09 【问题描述】:

我正在尝试将 WCF 服务放入 Windows 服务中。我使用 svcutil 来使用 CalculatorService.cs 和 output.config。我将 output.config 的内容放入 App.config 并编写一个客户端,包括 CalculatorService.cs 到其中。当它运行时,我收到错误消息“在 ServiceModel 客户端配置部分中找不到引用合同 'IAswerResult' 的默认端点元素”。在我的客户中,我刚刚复制了向导说要使用的内容:

protected void Page_Load(object sender, EventArgs e)

AnswerResultClient client = new AnswerResultClient(); // error here
client.QuestionAnswered("1", "2", "3");

在客户端(生成)的 App.config 中,我有以下内容,但它告诉我合同“IAswerResult”无效。不知道为什么……是svcutil生成的。

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IAnswerResult" />
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="NetTcpBinding_IAnswerResult" />
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_IAnswerResult" contract="IAnswerResult"
          name="BasicHttpBinding_IAnswerResult" />
      <endpoint address="net.tcp://localhost:6256/CalculatorService"
          binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IAnswerResult"
          contract="IAnswerResult" name="NetTcpBinding_IAnswerResult">
        <identity>
          <servicePrincipalName value="host/Server2012" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

Windows 服务/WCF 服务中的代码:

namespace EternaService

    [ServiceContract(Namespace = "http://EternaService")]

    public interface IAnswerResult
    
        [OperationContract]
        string QuestionAnswered(string person_int, string course_int, string lesson_int);
    


    public class CalculatorService : IAnswerResult
    
        public string QuestionAnswered(string person_int, string course_int, string lesson_int)
        
            string result = "";
            // Have input, now do something with it.
            return result;
        

    

    public partial class EternaService : ServiceBase
    
        public ServiceHost serviceHost = null;

        public EternaService()
        
            this.CanStop = true;
            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;

            InitializeComponent();
        

        protected override void OnStart(string[] args)
        

            if (serviceHost != null)
            
                serviceHost.Close();
            
            serviceHost = new ServiceHost(typeof(CalculatorService));
            serviceHost.Open();
        

Windows 服务/WCF 服务中的 App.config:

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <system.serviceModel>
    <services>
      <service name="EternaService.CalculatorService" behaviorConfiguration="myServiceBehave">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6255/CalculatorService"/>
            <add baseAddress="net.tcp://localhost:6256/CalculatorService"/>
          </baseAddresses>
        </host>
        <endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding" contract="EternaService.IAnswerResult" />
        <endpoint address="net.tcp://localhost:6256/CalculatorService" binding="netTcpBinding" contract="EternaService.IAnswerResult" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myServiceBehave">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

非常感谢任何方向。这是我的第一次尝试,我迷路了!

编辑: 我将 Windows 服务 ServiceContract 命名空间更改为:

[ServiceContract(Namespace = "http://CADEEternaService")]

在客户端 CalculatorService 类中:

[System.ServiceModel.ServiceContractAttribute(Namespace= "http://CADEEternaService", ConfigurationName="IAnswerResult")]
public interface IAnswerResult


    [System.ServiceModel.OperationContractAttribute(Action= "http://CADEEternaService/IAnswerResult/QuestionAnswered", ReplyAction= "http://CADEEternaService/IAnswerResult/QuestionAnsweredResponse")]
    string QuestionAnswered(string person_int, string course_int, string lesson_int);

    [System.ServiceModel.OperationContractAttribute(Action= "http://CADEEternaService/IAnswerResult/QuestionAnswered", ReplyAction= "http://CADEEternaService/IAnswerResult/QuestionAnsweredResponse")]
    System.Threading.Tasks.Task<string> QuestionAnsweredAsync(string person_int, string course_int, string lesson_int);

在客户端的 app.config 中:

  <endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IAnswerResult" contract="CADEEternaService.IAnswerResult"
      name="BasicHttpBinding_IAnswerResult" />
  <endpoint address="net.tcp://localhost:6256/CalculatorService"
      binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IAnswerResult"
      contract="CADEEternaService.IAnswerResult" name="NetTcpBinding_IAnswerResult">

但它仍然将“CADEEternaService.IAswerResult”标记为错误,并且我也收到原始错误,“找不到引用合同 'IAswerResult' 的默认端点元素”......但是,我觉得奇怪的是在错误消息中对 IAnswerResult 而不是 CADEEternaService.IAnswerResult 咆哮。

【问题讨论】:

我将两个合同值都更改为“EternaService.IAswerResult”,但它给出了相同的错误,“在 ServiceModel 客户端配置部分中找不到引用合同 'IAswerResult' 的默认端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。”我尝试使用 Inelllisense,但没有发现任何我认识的东西。 请注意,您在ServiceContract 上设置的命名空间与合约的命名空间无关。服务契约命名空间是 SOAP 的一部分。 【参考方案1】:

您需要在合同之前添加您生成的服务参考的命名空间...... contract="xxxxxxx.IAnswerResult

 <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IAnswerResult" />
  </basicHttpBinding>
  <netTcpBinding>
    <binding name="NetTcpBinding_IAnswerResult" />
  </netTcpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IAnswerResult" contract="**xxxxx.IAnswerResult**"
      name="BasicHttpBinding_IAnswerResult" />
  <endpoint address="net.tcp://localhost:6256/CalculatorService"
      binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IAnswerResult"
      contract="IAnswerResult" name="NetTcpBinding_IAnswerResult">
    <identity>
      <servicePrincipalName value="host/Server2012" />
    </identity>
  </endpoint>
</client>

【讨论】:

正如我上面所说,完全限定的名称仍然会出错。在查看 app.config 时,如果我将鼠标悬停在合同值上,它会显示“合同值根据其数据类型 'clientContractType' 无效 - 枚举约束失败” 尝试将命名空间更改为不在您的项目命名空间中的其他命名空间 EternaService [ServiceContract(Namespace = "EternaService")] 您的命名空间与您的服务名称相同。 我以为你在做某事……除非我搞砸了更改。我使用 CADEEternaService 作为合同命名空间编辑了原始帖子。 在您的客户端应用程序中,您需要从您的 Windows 服务中添加对 WCF 服务库项目的引用,并将以下 using 语句添加到您的 Windows 服务项目中的 **.cs 文件中。尝试遵循本教程msdn.microsoft.com/en-us/library/… 关键是客户端中对 WCF 服务的引用。奇怪的是,我创建了 ServiceReference 好,但无法通过“使用”显示它。所以我完全限定它:CalculatorServiceReference.AnswerResultClient client = new CalculatorServiceReference.AnswerResultClient("NetTcpBinding_IAswerResult");【参考方案2】:

在您的客户端配置中,合约必须是完全限定名称,而不仅仅是接口名称。

<endpoint address="http://localhost:6255/CalculatorService" 
          binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_IAnswerResult" 
          contract="EternaService.IAnswerResult"
          name="BasicHttpBinding_IAnswerResult" />
  <endpoint address="net.tcp://localhost:6256/CalculatorService"
            binding="netTcpBinding" 
            bindingConfiguration="NetTcpBinding_IAnswerResult"
            contract="EternaService.IAnswerResult"
            name="NetTcpBinding_IAnswerResult">

换句话说,EternaServie.IAnswerResult,而不仅仅是IAnswerResult

【讨论】:

以上是关于客户端在 Windows 服务内找不到 WCF 的端点元素的主要内容,如果未能解决你的问题,请参考以下文章

当 WCF 应用程序在 Windows 7 上作为 Windows 服务托管时,找不到证书

SwiftUI NavigationLink 在范围内找不到“json”

无法在类库应用程序中使用 WCF 服务 [重复]

如何在 ajax 客户端中使用作为 Windows 服务运行的 wcf 服务

Windows 服务与托管 WCF 服务

找不到端点异常 - WCF Web 服务