";无法下载元数据“;当使用Two Bindings时

本文关键字:Two Bindings 元数据 quot 下载 | 更新日期: 2023-09-27 18:00:18

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

我在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。

";无法下载元数据“;当使用Two Bindings时

我可以在您的代码中找到一些可能导致此问题的问题:

CCD_ 1。此处传递typeof(SService.SService)而不是typeof(SServiceClient)。更改如下:

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

<service behaviorConfiguration="WsHttpBehaviour"。我想这应该是你定义的"行为"。由于您在配置中启用了元数据,因此可以删除将ServiceMetadataBehavior添加到servicehost的代码行。

这里有一个你可以参考的例子:

<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";
        }
    }
}

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