1

所以我有一个托管在 IIS8(Windows Server 2012)中的 WCF 服务。这是配置文件的相关部分:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="MovieCorner.DAL.Service.MovieCornerDalService">
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.221.101/MovieCorner/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <endpoint address="" binding="basicHttpBinding" 
           contract="MovieCorner.Commons.Services.IMovieCornerDalService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint kind="udpDiscoveryEndpoint" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="True" />
          <serviceDiscovery />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

具有简单绑定和发现端点的简单服务。该服务已启动并运行,一切正常。几乎...

这是我在客户端使用的代码(它只是一个“单元”测试):

var client = new DiscoveryClient(new UdpDiscoveryEndpoint());
var response = client.Find(new FindCriteria(typeof(IMovieCornerDalService)));

Assert.IsNotNull(response);
Assert.IsNotNull(response.Endpoints);
Assert.IsTrue(response.Endpoints.Count > 0);

foreach (var endpoint in response.Endpoints)
{
    Console.WriteLine("Address: {0}; Contract: {1}", endpoint.Address, endpoint.ContractTypeNames[0]);
}

代码成功找到了唯一正在运行的服务。输出如下:

地址:http://ws12-iis8/MovieCorner/MovieCornerDalService.svc;合同:http ://tempuri.org/:IMovieCornerDalService

该地址与托管服务的机器名称一起返回。发现后我想使用这样的服务:

var endpoint = response.Endpoints[0];
var clientProxy = ChannelFactory<IMovieCornerDalService>.CreateChannel(new BasicHttpBinding(), endpoint.Address);
var user = clientProxy.RegisterUser("1234"); // problem

实际方法调用抛出异常,内部异常如下:System.Net.WebException: The remote name could not be resolve: 'ws12-iis8'

“单元”测试在我的 PC 中运行,服务托管在 VM 中。我可以通过http://192.168.221.101/MovieCorner/MovieCornerDalService.svc地址访问该服务。但不能用机器名地址。

我错过了什么?我有哪些选择?如何检索服务托管 VM 的实际(私有)IP?我搜索了不同的元数据选项,但我不是网络世界的专业人士,所以我不知道我在寻找什么。

如果您需要更多信息,请告诉我。谢谢你的时间!

4

0 回答 0