【问题标题】:WCF service throws InvalidOperationExceptionWCF 服务抛出 InvalidOperationException
【发布时间】:2018-07-11 02:03:20
【问题描述】:

我试图创建基本的 WCF 服务并将其托管在控制台应用程序上。

这是我的 WCF 项目代码。

ISampleService.cs

using System.ServiceModel;  

namespace MultipleSeviceContractAppl  
{  
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISampleService" in both code and config file together.  
    [ServiceContract]  
    public interface ISampleService  
    {  
        [OperationContract]  
        string DoWork();  
    }  
}

SampleService.cs

namespace MultipleSeviceContractAppl  
{  
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SampleService" in both code and config file together.  
    public class SampleService : ISampleService  
    {  
        public string DoWork()  
        {  
            return "Message from WCFservice";  
        }  
    }  
}

App.config

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
  <system.serviceModel>  

    <behaviors>  
      <serviceBehaviors>  
        <behavior name="mexBehaviour">  
          <serviceMetadata httpGetEnabled="false"/>  
          <serviceDebug includeExceptionDetailInFaults="false" />  
        </behavior>  
      </serviceBehaviors>  
    </behaviors>

    <services>  
      <service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">  
        <endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">  
        </endpoint>  
        <host>  
          <baseAddresses>  
            <add baseAddress="http://localhost:8733/"/>  <!--For metadata exchange-->
            <add baseAddress="net.tcp://localhost:8737/" />  <!--Endpoint, netTCP binding, For data exchange-->
          </baseAddresses>  
        </host>  
      </service>  
    </services>

  </system.serviceModel>  
</configuration>

控制台应用程序上的 WCF 托管 - Program.cs

using System;  
using System.ServiceModel;  
namespace ConsumeWCFApplicationAppl  
{  
    class Program  
    {  
        static void Main()  
        {  
            using (ServiceHost host = new ServiceHost(typeof(MultipleSeviceContractAppl.SampleService)))  
            {  
                host.Open();  
                Console.WriteLine("Host started @" + DateTime.Now.ToString());  
                Console.ReadKey();  
            }  
        }  
    }  
}

host.Open(); 行的控制台应用程序中,引发了以下异常。

“System.InvalidOperationException”类型的未处理异常 发生在 System.ServiceModel.dll 附加信息:服务 'MultipleServiceContractAppl.SampleService' 的应用程序为零 (非基础设施)端点。这可能是因为没有配置 为您的应用程序找到了文件,或者因为没有服务元素 可以在配置文件中找到匹配的服务名称,或者 因为在服务元素中没有定义端点。帮我 找出我的错误。谢谢

【问题讨论】:

  • 尝试在 ServiceHost 构造函数中指定基地址:ServiceHost host = new ServiceHost(typeof(MultipleServiceContractApp1.SapmleService), new Uri[] { "http://localhost:8733/", "net.tcp://localhost:8737/" }); 此外,您可能需要使用提升的权限运行才能注册 net.tcp 地址。
  • 添加上面的行会引发新的错误严重性无法将类型'string'隐式转换为'System.Uri'
  • 您的应用程序的问题是,它无法找到您的 app.config 文件。删除并重新添加它或创建一个新的解决方案,检测 app.config 有问题。跨度>
  • @LearnAvid - 试试ServiceHost host = new ServiceHost(typeof(MultipleServiceContractApp1.SapmleService), new Uri[] { new Uri("http://localhost:8733/"), new Uri("net.tcp://localhost:8737/") });

标签: c# .net wcf


【解决方案1】:

您需要在控制台应用程序中复制配置并将对服务模型程序集的 DLL 的引用添加到此项目...

<behaviors>  
  <serviceBehaviors>  
    <behavior name="mexBehaviour">  
      <serviceMetadata httpGetEnabled="false"/>  
      <serviceDebug includeExceptionDetailInFaults="false" />  
    </behavior>  
  </serviceBehaviors>  
</behaviors>

<services>  
  <service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">  
    <endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">  
    </endpoint>  
    <host>  
      <baseAddresses>  
        <add baseAddress="http://localhost:8733/"/>  <!--For metadata exchange-->
        <add baseAddress="net.tcp://localhost:8737/" />  <!--Endpoint, netTCP binding, For data exchange-->
      </baseAddresses>  
    </host>  
  </service>  
</services>

【讨论】:

  • 我认为我们不应该在任何宿主应用程序中添加 app.config。
  • 为什么不将 app.config 添加到宿主应用程序? WCF 运行配置(除非您以编程方式配置它)。
  • 我的朋友,正如我所说,您必须将系统模型配置复制到控制台主机应用程序,除非您使用 c# 代码以编程方式进行。你试过看我是对的吗??
  • 感谢@Jailson Evora,Tim 的回复。我使用上述配置更新了控制台应用程序的(主机应用程序)app.config。它有效!
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多