简体   繁体   English

客户端无法在Windows Service中找到WCF的终结点元素

[英]Client can't find endpoint element of WCF Inside Windows Service

I'm trying to put a WCF service inside a Windows Service. 我正在尝试将WCF服务放在Windows服务中。 I used svcutil to use the CalculatorService.cs and output.config. 我使用svcutil来使用CalculatorService.cs和output.config。 I put the contents of the output.config into the App.config and write a client, including the CalculatorService.cs into it. 我将output.config的内容放入App.config中,并编写一个包括CalculatorService.cs的客户端。 When it runs, I get the error, "Could not find default endpoint element that references contract 'IAnswerResult' in the ServiceModel client configuration section". 运行时,出现错误“在ServiceModel客户端配置部分中找不到引用合同'IAnswerResult'的默认端点元素”。 In my client, I've just copied what the wizard said to use: 在我的客户端中,我刚刚复制了向导说的用法:

protected void Page_Load(object sender, EventArgs e)
{
AnswerResultClient client = new AnswerResultClient(); // error here
client.QuestionAnswered("1", "2", "3");
}

In App.config of the client (generated) I have the following but it tells me that the contract "IAnswerResult" is invalid. 在客户端(已生成)的App.config中,我具有以下内容,但它告诉我合同“ IAnswerResult”无效。 I don't know why... it's what was generated by svcutil. 我不知道为什么...这是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>

Code in the Windows Service/WCF Service: Windows Service / 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();
        }

App.config in the Windows Service/WCF Service: Windows Service / 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>

Any direction greatly appreciated. 任何方向都非常感谢。 This is my first attempt and I'm lost! 这是我的第一次尝试,我迷路了!

EDIT: I changed the Windows service ServiceContract namespace to: 编辑:我将Windows服务ServiceContract命名空间更改为:

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

In the client CalculatorService class: 在客户端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);
}

In the client's app.config: 在客户端的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">

But it's still flagging "CADEEternaService.IAnswerResult" as an error and I'm also getting the original error, "Could not find default endpoint element that references contract 'IAnswerResult'"... however, I find it odd that it's barking at IAnswerResult and not CADEEternaService.IAnswerResult in the error message. 但是它仍然将“ CADEEternaService.IAnswerResult”标记为错误,并且我也得到了原始错误,“找不到引用合同'IAnswerResult'的默认端点元素” ...但是,我发现奇怪的是它在IAnswerResult吠叫而不是错误消息中的CADEEternaService.IAnswerResult。

In your client's config, the contract needs to be a fully qualified name, not just the interface name. 在客户端的配置中,合同需要是完全限定名称,而不仅仅是接口名称。

<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">

In other words, EternaServie.IAnswerResult , not just IAnswerResult . 换句话说,是EternaServie.IAnswerResult ,而不仅仅是IAnswerResult

You need to add the namespace of your Service Reference generated, before the contract .... contract="xxxxxxx.IAnswerResult 您需要在合同之前添加生成的服务引用的名称空间...。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM