与WCF客户端在同一程序集中的EndpointNotFoundException
本文关键字:程序集 集中 EndpointNotFoundException 程序 WCF 客户端 | 更新日期: 2023-09-27 18:06:11
我在我的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
端点。这个在我的浏览器中可以访问,所以服务已经启动并运行。
经过几个小时的调试,我自己解决了(非常简单的)问题:
结果,服务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>