WCF服务抛出InvalidOperationException

时间:2018-01-31 20:36:15

标签: c# .net wcf

我正在尝试创建基本的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>

在控制台appl上进行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(); 行的控制台应用程序中,抛出以下异常。

  

未处理的类型&#39; System.InvalidOperationException&#39;   发生在System.ServiceModel.dll中的附加信息:服务   &#39; MultipleSeviceContractAppl.SampleService&#39;没有申请   (非基础设施)端点。这可能是因为没有配置   找到了您的应用程序的文件,或者因为没有服务元素   匹配服务名称可以在配置文件中找到,或者   因为在service元素中没有定义端点。帮我   搞清楚我的错误。感谢

1 个答案:

答案 0 :(得分:0)

您需要在控制台应用程序中复制配置,并将对服务模型程序集的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>
相关问题