【问题标题】:Hosting WCF service through Console通过控制台托管 WCF 服务
【发布时间】:2011-01-06 16:05:09
【问题描述】:

我创建了一个 WCF 服务并通过控制台托管它。但是当我创建另一个 Web 应用程序并尝试添加它的服务引用时,它给出了错误

元数据包含一个引用 无法解决: 'net.tcp://192.0.0.0:9100/ConsoleApplication3/Communicator.svc/mextcp'。 无法连接到 net.tcp://192.0.0.0:9100/ConsoleApplication3/Communicator.svc/mextcp。 连接尝试持续了 时间跨度为 00:00:00.9843750。 TCP 错误代码 10061:无法连接 因为目标机器而被制造 积极拒绝它 192.0.0.0:9100。 无法建立连接,因为 目标机器主动拒绝 192.0.0.0:9100 如果当前解决方案中定义了服务,请尝试 构建解决方案并添加 再次引用服务。

代码如下:

namespace ConsoleApplication3
{
class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (ServiceHost host = new ServiceHost(typeof(Communicator)))
            {
                host.Open();
                Console.WriteLine("Press <Enter> to terminate the Host application.");
                Console.ReadLine();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }
 }

[ServiceContract]
public interface ICommunicator
{
    [OperationContract]
    string SayHello();
}

public class Communicator : ICommunicator
{
    public string SayHello()
    {
        return "I am here";
    }
}

这是配置:

    <configuration>
  <system.serviceModel>
    <services>
      <service name="ConsoleApplication3.Communicator" behaviorConfiguration="CommunicatorBehavior">
        <!-- Service Endpoints -->
        <endpoint address="ConsoleApplication3" binding="netTcpBinding"
            contract="ConsoleApplication3.ICommunicator"/>
        <!-- This Endpoint is used for genertaing the proxy for the client -->
        <!-- To avoid disclosing metadata information, set the value below to false and
       remove the metadata endpoint above before deployment -->
        <endpoint address="mex" contract="IMetadataExchange" binding="mexTcpBinding" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:9100/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CommunicatorBehavior">
          <serviceMetadata httpGetEnabled="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

【问题讨论】:

    标签: .net wcf


    【解决方案1】:

    我认为您尝试获取服务的地址有误。

    您自己在控制台应用程序中使用netTcpBinding 托管了一项服务,并且您定义了net.tcp://localhost:9100/ 的基地址,MEx 端点位于net.tcp://localhost:9100/mex

    所以你需要使用基地址

    net.tcp://localhost:9100/
    

    或 MEX 地址

    net.tcp://localhost:9100/mex
    

    尝试连接到服务时。

    我不知道您是如何想出这个您似乎尝试连接的地址 (net.tcp://192.0.0.0:9100/ConsoleApplication3/Communicator.svc/mextcp) - 但这个地址无效有效。首先 - 自托管 netTcpBinding 网络服务时没有要使用的 *.svc 文件,而且我不知道你从哪里得到这个 /mextcp 地址.....

    更新:我使用了你的代码,创建了一个带有接口和服务实现以及你的服务配置的新控制台应用程序,它在我的机器上运行良好:

    当我尝试从 Visual Studio 中启动控制台应用程序时,我确实收到了来自 Windows 防火墙的警告,关于允许访问 - 我确实允许了。

    【讨论】:

    • 那么我试过的地址应该是什么 'net.tcp://localhost:9100/mex' 但仍然是同样的错误
    • @BreakHead:或者这个,这似乎是你最终要去的那个:net.tcp://192.168.168.145:9100/
    • @Breakhead:在这种情况下,一定是其他原因导致了问题……某种 TCP/IP 设置——也许是防火墙?你能关掉所有防火墙之类的东西来做个测试吗?
    • 我关闭了防火墙还是同样的错误,你能帮个忙,可以试试你最后的代码吗??
    • @BreakHead:你的代码和配置很好——在我的机器上就像一个魅力。这里一定有别的东西妨碍你.....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多