WCF自托管在VS2013中有效,但在生成的exe文件中无效

时间:2014-06-12 09:40:18

标签: c# wcf ipc self-hosting

我已经在C#中阅读了很多关于WCF的不同内容,但是仍然没有找到满意的答案来解决我的noopy问题。 (我是C#和.NET的新手)

当我在VS中运行自我托管的WCF服务时,一切正常。在VS运行时运行我的客户端应用程序的生成的exe文件( clientdir \ bin \ debug \ consoleApp.exe )时,它也可以正常工作。但是,当我运行生成的exe文件时( clientdir \ bin \ debug \ consoleApp.exe hostdir \ bin \ debug \ consoleApp.exe )当VS 正在运行时,我得到 EndpointNotFoundException

这是我的客户端应用程序的App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ImyService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1592/myService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ImyService"
                contract="refmy.ImyService" name="BasicHttpBinding_ImyService" />
        </client>
    </system.serviceModel>
</configuration>

这是我的主机应用程序的App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MetadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDiscovery />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MetadataBehavior" name="my.myService">
        <endpoint binding="basicHttpBinding" bindingConfiguration="" name="httpEndpoint" contract="my.ImyService" />
        <endpoint binding="netTcpBinding" bindingConfiguration="" name="netTcpEndpoint" contract="my.ImyService" />
        <endpoint address="MEX" binding="mexTcpBinding" bindingConfiguration="" name="mexEndpoint" contract="IMetadataExchange" />
        <endpoint name="udpDiscovery" kind="udpDiscoveryEndpoint" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9400/myService" />
            <add baseAddress="net.tcp://localhost:9500/myService" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

这是我的主机应用程序的C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using my;

namespace myHost
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(myService));
            host.Open();
            Console.ReadLine();

        }
    }
}

2 个答案:

答案 0 :(得分:1)

客户端配置中的端点地址无效。它应该是:

address="http://localhost:9400/myService"

答案 1 :(得分:0)

您的客户地址与服务地址不匹配:

<client>
        <endpoint address="http://localhost:1592/myService.svc"

服务:

<add baseAddress="http://localhost:9400/myService" />

尝试使用:

<client>
    <endpoint address="http://localhost:9400/myService".....