【问题标题】:EndpointNotFoundException with WCF client in same AssemblyEndpointNotFoundException 与 WCF 客户端在同一程序集中
【发布时间】:2016-09-08 19:30:38
【问题描述】:

我在我的 WPF 应用程序中托管 WCF 服务,并希望在同一个程序集中使用它(但在另一个实例中)。为了依赖注入,我使用已创建的服务实现实例调用ServiceHost.Open

合同和服务实施

[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface IService
{
    [OperationContract]
    PartnerInfo GetPartnerInfo();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
    private IpService _ipService;

    public Service(IpService ipService)
    {
        _ipService = ipService;
    }

    public PartnerInfo GetPartnerInfo()
    {
        var info = new PartnerInfo();
        info.Address = _ipService.GetLocalIpAddress();

        return info;
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Project.WebService.Service" behaviorConfiguration="ServiceBehavior">
        <endpoint address="endpoint0"
                  binding="netTcpBinding"
                  bindingConfiguration="NetTcpBinding_IService"
                  contract="Project.WebService.IService">
          <identity>
            <servicePrincipalName value="Project.WebService.Service"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:61000/Project/Service.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IService"
                 transferMode="Buffered"
                 maxBufferPoolSize="2147483647"
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647"
                 sendTimeout="00:30:00"
                 receiveTimeout="infinite">
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          </security>
          <reliableSession inactivityTimeout="infinite" enabled="true" />
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint
        binding="netTcpBinding"
        bindingConfiguration="NetTcpBinding_IService"
        contract="Project.WebService.IService"
        name="NetTcpBinding_IService">
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

ServiceHost 创建

_serviceHost = new ServiceHost(_Service);
_serviceHost.Open();

打开ServiceHost 后,它会侦听端口 61000(通过 netstat 检查)。虽然我不需要它(因为我自己创建了一个实现 IService 的客户端),但我测试了创建一个服务引用并且它有效。

但是,我的服务实现如下所示:

public class ServiceClient : ClientBase<IService>, IService
{
    public ServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public PartnerInfo GetPartnerInfo()
    {
        return Channel.GetPartnerInfo();
    }
}

当我这样调用它时,在同一台机器上使用另一个服务托管实例,会抛出一个EndpointNotFoundException

Uri baseUri = new Uri("net.tcp://<IPAdress>:61000");
Uri uri = new Uri(baseUri, "Project/Service.svc");

// Spn identity is only here for testing purposes, doesn't work either way
return new ServiceClient("NetTcpBinding_IService", new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity("NetTcpBinding_IService")));

我在实现 ServiceClient 时是否遗漏了某些内容,因为我在同一个程序集中同时拥有服务和客户端,因此不想在 Visual Studio 中创建服务引用?

更新

我测试了创建服务引用的整个过程。这也不起作用。我还仔细检查了我的防火墙规则。这是异常消息:

There was no endpoint listening at net.tcp://192.168.200.29:61000/Project/Service.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

更新 2

出于测试目的,我创建了第二个wsHttpBinding 端点。这个可以在我的浏览器中访问,因此该服务已启动并正在运行。

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    经过数小时的调试,我自己解决了(非常简单的)问题:

    事实证明,服务 URL 不是net.tcp://localhost:61000/Project/Service.svc,而是net.tcp://localhost:61000/Project/Service.svc/endpoint0

    从我的端点配置中删除“endpoint0”后,连接顺利:

    <endpoint address=""
              binding="netTcpBinding"
              bindingConfiguration="NetTcpBinding_IService"
              contract="Project.WebService.IService">
      <!-- -->
    </endpoint>
    

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2015-08-09
      • 1970-01-01
      相关资源
      最近更新 更多