首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF服务抛出InvalidOperationException

WCF服务抛出InvalidOperationException
EN

Stack Overflow用户
提问于 2018-01-31 20:36:14
回答 1查看 1.1K关注 0票数 0

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

这是我的WCF项目代码。

ISampleService.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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托管在控制台appl - Program.cs上

代码语言:javascript
复制
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附加信息中:服务'MultipleSeviceContractAppl.SampleService‘有零个应用程序(非基础结构)端点。这可能是因为没有为您的应用程序找到配置文件,或者因为在配置文件中找不到匹配服务名称的服务元素,或者因为在服务元素中没有定义端点。帮我找出我的错误。谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-01-31 23:51:51

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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48551018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档