C# &引用;“无法下载元数据”;当使用两个绑定时

C# &引用;“无法下载元数据”;当使用两个绑定时,c#,wcf,web-config,metadata,wcf-binding,C#,Wcf,Web Config,Metadata,Wcf Binding,在我的书中,它希望我使用两个绑定公开两个端点:WsHttpBinding和NetTCPBinding,并将服务托管在主机应用程序中 我使用C#中的以下代码尝试连接到我的服务: Uri BaseAddress = new Uri("http://localhost:5640/SService.svc"); Host = new ServiceHost(typeof(SServiceClient), BaseAddress); ServiceMetadataBehavior Behaviour =

在我的书中,它希望我使用两个绑定公开两个端点:WsHttpBinding和NetTCPBinding,并将服务托管在主机应用程序中

我使用C#中的以下代码尝试连接到我的服务:

Uri BaseAddress = new Uri("http://localhost:5640/SService.svc");

Host = new ServiceHost(typeof(SServiceClient), BaseAddress);
ServiceMetadataBehavior Behaviour = new ServiceMetadataBehavior();
Behaviour.HttpGetEnabled = true;
Behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
Host.Description.Behaviors.Add(Behaviour);
Host.Open();
在服务方面,我有:

[ServiceContract]
public interface IService...

public class SService : IService....
然后在我的配置文件中,我有:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>

    <behaviors>
      <serviceBehaviors>
        <behavior name="Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="WsHttpBehaviour" name="SService.SService">

        <endpoint
          address=""
          binding="wsHttpBinding"
          bindingConfiguration="WsHttpBindingConfig"
          contract="SService.IService" />

        <endpoint
          address=""
          binding="netTcpBinding"
          bindingConfiguration="NetTCPBindingConfig"
          contract="SService.IService" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:5640/SService.svc" />
            <add baseAddress="net.tcp://localhost:5641/SService.svc" />
          </baseAddresses>
        </host>

      </service>
    </services>

  </system.serviceModel>
</configuration>

但是,当我尝试向主机应用程序添加服务引用时,它表示无法从该地址下载元数据。我不明白怎么了,老师从来没有教过这个。。我决定继续向前看,提前学习。我使用编辑器创建了如上所示的WebConfig


谁能给我指出正确的方向吗?我通过编辑器添加了元数据行为,并将HttpGetEnabled设置为true。

我发现您的代码中存在一些可能导致此问题的问题:

Host=newServiceHost(typeof(SServiceClient),BaseAddress)
。在此处传递
typeof(SService.SService)
而不是
typeof(SServiceClient)
。像这样改变:

Host = new ServiceHost(typeof(SService.SService))

命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
ServiceHost主机=新的ServiceHost(类型(服务));
host.Open();
Console.ReadLine();
}
}
[服务合同]
公共接口设备
{
[经营合同]
串销();
}
公共课服务:IService
{
公共字符串DoWork()
{
返回“你好世界”;
}
}
}

元数据将在配置中定义的http基地址处自动可用。

I get:“无法找到与绑定NetTcpBinding的端点的scheme net.tcp匹配的基地址。注册的基地址方案为[http]”。当我进行更改时,我可以看到您在配置中定义了net.tcp基地址。因此无需在ServiceHost()中传递baseAddress。你试过删除它吗?我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。
<system.serviceModel>
    <services>
      <service name="ConsoleApplication1.Service">
        <endpoint address="" binding="wsHttpBinding" contract="ConsoleApplication1.IService" />
        <endpoint address="" binding="netTcpBinding" contract="ConsoleApplication1.IService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://<machinename>:5640/SService.svc" />
            <add baseAddress="net.tcp://<machinename>:5641/SService.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service));
            host.Open();

            Console.ReadLine();
        }
    }

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string DoWork();
    }

    public class Service : IService
    {
        public string DoWork()
        {
            return "Hello world";
        }
    }
}