0

在我的书中,它希望我使用两个绑定公开两个端点: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。

4

1 回答 1

1

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

Host = new ServiceHost(typeof(SServiceClient), BaseAddress). 通过这里typeof(SService.SService)而不是typeof(SServiceClient). 像这样改变:

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

<service behaviorConfiguration="WsHttpBehaviour". 我想这应该是您定义的“行为”。由于您在配置中启用了元数据,您可以删除将 ServiceMetadataBehavior 添加到服务主机的代码行。

这是一个示例,您可以参考:

<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 基地址自动可用。

于 2013-02-21T04:28:30.193 回答