客户端无法找到WCF Inside Windows Service的端点元素

时间:2018-01-30 20:48:34

标签: c# wcf windows-services wcf-binding

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

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

在客户端的App.config中(生成)我有以下内容,但它告诉我合同" IAnswerResult"是无效的。我不知道为什么......它是由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">

但它仍在萎靡不振&#34; CADEEternaService.IAnswerResult&#34;作为一个错误,我也得到了原始错误,&#34;找不到引用合同的默认端点元素&#39; IAnswerResult&#39;&#34; ...但是,我觉得很奇怪它在IAnswerResult上吠叫,而不是在错误信息中的CADEEternaService.IAnswerResult。

2 个答案:

答案 0 :(得分:0)

在客户端的配置中,合同需要是一个完全限定的名称,而不仅仅是接口名称。

<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

答案 1 :(得分:0)

您需要在合同之前添加生成的服务参考的命名空间.... 的合同=“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>