【问题标题】:Cannot start a tcp based WCF service无法启动基于 tcp 的 WCF 服务
【发布时间】:2014-11-18 10:05:54
【问题描述】:

我正在开发一个基于 tcp 的 WCF 服务,并且在尝试在主机应用程序中启动该服务时遇到了这个臭名昭著的 WCF 异常:

System.ServiceModel.dll 中出现“System.InvalidOperationException”类型的未处理异常

附加信息:服务“WcfServiceWas.MathService”的应用程序(非基础设施)端点为零。这可能是因为找不到您的应用程序的配置文件,或者因为在配置文件中找不到与服务名称匹配的服务元素,或者因为在服务元素中没有定义端点。

这个问题之前已经描述过很多次了,建议的解决方案是完全按照类库中定义的方式命名服务,问题是这个解决方案对我不起作用。

托管服务拒绝启动Service的这个配置有什么问题?

服务接口(IMathService.cs):

namespace WcfServiceWas
{
   [ServiceContract]
   public interface IMathService
   {
      [OperationContract]
      OperationResult Add(int x, int y);

      [OperationContract]
      int Subtract(int x, int y);
    }
}

服务实现(MathService.cs):

namespace WcfServiceWas
{
    public class MathService : IMathService
    {
        public OperationResult Add(int x, int y)
        {
            return new OperationResult { Result = new MyInteger { Value = x + y } };
        }

        public int Subtract(int x, int y)
        {
            return x - y;
        }
    }
}

Svc 文件(MathService.svc):

<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceWas.MathService" CodeBehind="MathService.cs" %>

Web.Config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
  </system.web>
  
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceWas.MathServiceBehavior" >
            <serviceMetadata httpGetEnabled="false" />
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <services>
        <service name="WcfServiceWas.MathService" behaviorConfiguration="WcfServiceWas.MathServiceBehavior" >
            <endpoint contract="WcfServiceWas.IMathService" binding="netTcpBinding" address="" />
            <endpoint contract="IMetadataExchange" binding="mexTcpBinding" address="mex" />
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:999/MathServiceX/"/>
                </baseAddresses>
            </host>
        </service>
      </services>
  </system.serviceModel>
</configuration>

服务主机:

class Program
{
    static void Main(string[] args)
    {
        using (var myServiceHost = new ServiceHost(typeof(MathService)))
        {
            myServiceHost.Open();
        }
    }
}

更新:宿主应用程序的 app.config 也必须包含 wcf 配置。

【问题讨论】:

    标签: c# .net web-services wcf tcp


    【解决方案1】:

    您需要定义绑定部分,在&lt;system.serviceModel&gt; 下添加以下&lt;bindings&gt; 配置:

    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindingConfiguration" sendTimeout="00:01:00">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    

    并编辑&lt;endpoint contract="WcfServiceWas.IMathService"行,在其中添加绑定配置,如下:

    <endpoint contract="WcfServiceWas.IMathService"
              binding="netTcpBinding" 
              bindingConfiguration="netTcpBindingConfiguration" 
              address="" />
    

    保存这些配置更改并重新启动服务,它现在应该可以正常工作了。

    更新:

    因此看来,对于自托管应用程序,WCF 的配置必须在主机本身的 app.config 中,并且该部分不是必须出现在 System.ServiceModel 中,因为它将采用默认绑定,msdn 文档更能说明这一点

    http://msdn.microsoft.com/en-us/library/ee530014%28v=vs.110%29.aspx

    【讨论】:

    • 感谢您的回答,我之前尝试过配置绑定(实际上您不必根据文档进行配置)。不幸的是,我尝试了您的解决方案,但没有为我做任何事情。
    • 看,我创建了一个新的 wcf 服务,起初我得到了你的确切错误,然后我将配置部分 从 WCF 服务的 web.config 移到了 app.config 中。服务主机的配置,它工作。试试这个,让我知道。
    • 更改主机应用程序的 app.config 有效 - 所以配置没问题,只是没有考虑到。如果你改变你的答案,我会接受。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多