与同一程序集

时间:2016-09-08 19:30:38

标签: c# .net wcf

我正在我的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端点。这个可以在我的浏览器中访问,因此服务已启动并运行。

1 个答案:

答案 0 :(得分:0)

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

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

删除&#34; endpoint0&#34;从我的端点配置来看,连接进展顺利:

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