简体   繁体   English

与同一程序集中的WCF客户端的EndpointNotFoundException

[英]EndpointNotFoundException with WCF client in same Assembly

I'm hosting a WCF Service in my WPF app and want to consume it within the same assembly (in another instance, though). 我在我的WPF应用程序中托管WCF服务,并希望在同一个程序集中使用它(在另一个实例中)。 For the sake of dependency injection I'm calling ServiceHost.Open with an already created instance of my service implementation. 为了依赖注入,我使用已经创建的服务实现实例调用ServiceHost.Open

Contract and Service implementation 合同和服务实施

[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 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 creation ServiceHost创建

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

After opening the ServiceHost , it listens on Port 61000 (checked via netstat). 打开ServiceHost ,它将侦听端口61000(通过netstat检查)。 Altough I don't need it (because I created a client implementing IService myself), I tested creating a service reference and it worked. 尽管我不需要它(因为我自己创建了一个实现IService的客户端),我测试了创建服务引用并且它有效。

However, my service implementation looks like this: 但是,我的服务实现如下所示:

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

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

And when I'm calling it this way, with another service hosting instance on the same machine, an EndpointNotFoundException is thrown: 当我以这种方式调用它时,在同一台机器上使用另一个服务托管实例时,会抛出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")));

Am I missing something on my implementation of the ServiceClient as I have both service and client in the same assembly and therefore don't want to create a service reference in Visual Studio? 我在ServiceClient实现上遗漏了什么,因为我在同一个程序集中同时拥有服务和客户端,因此不想在Visual Studio中创建服务引用?

Update 更新

I tested the whole process creating a Service Reference. 我测试了整个过程,创建了一个服务参考。 This doesn't work either. 这也不起作用。 I also double-checked my firewall rules. 我还仔细检查了我的防火墙规则。 Here is the exception message: 这是异常消息:

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.

Update 2 更新2

For testing purposes, I created a second wsHttpBinding Endpoint. 出于测试目的,我创建了第二个wsHttpBinding端点。 This one is reachable in my browser, so the service is up and running. 这个可在我的浏览器中访问,因此该服务已启动并正在运行。

After hours of debugging, I solved the (quite simple) issue myself: 经过几个小时的调试,我自己解决了(非常简单)的问题:

As it turned out, the service URL wasn't net.tcp://localhost:61000/Project/Service.svc but net.tcp://localhost:61000/Project/Service.svc/endpoint0 . 事实证明,服务URL不是net.tcp://localhost:61000/Project/Service.svc而是net.tcp://localhost:61000/Project/Service.svc/endpoint0

After removing the "endpoint0" from my endpoint configuration, the connection went well: 从我的端点配置中删除“endpoint0”后,连接进行得很顺利:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM