我可以在本地调用自托管WCF服务中的方法吗?

DJI*_*ave 10 .net c# wcf servicehost

我有一个WCF服务合同,基本上是发布订阅者模式.

WCF服务托管在我要发布的Windows服务中.客户端订阅消息,当Windows服务执行某些操作时,它会发布给所有客户端.

为了托管服务,我已经声明了一个ServiceHost类,而Contract Class有一个方法,该方法没有在接口中标记,而是在要发布的类中实现.

我希望能够在本地调用此方法(不通过WCF),然后通过Callbacks发布消息.

我似乎无法从ServiceHost获得Contract Class的实例.

这有可能吗?如果可以的话怎么样?我知道解决方法是将一个客户端内置到服务中,但创建一个连接到自身的客户端似乎有点奇怪.

提前致谢

DJIDave

的app.config

<system.serviceModel>
    <services>
      <service behaviorConfiguration="Processor.Wcf.ServiceBehavior"
        name="Processor.Wcf.ProcessorService">
        <endpoint address="net.tcp://localhost:9000/processor/service"
              binding="netTcpBinding" name="procService"
              bindingConfiguration="netTcpBindingConfig"
              contract="Processor.Wcf.IProcessorService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8732/Design_Time_Addresses/Processor.Wcf/Service1/" />
            </baseAddresses>
          </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Processor.Wcf.ServiceBehavior">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindingConfig"
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00"
                 receiveTimeout="00:10:00"
                 sendTimeout="00:01:00"
                 transactionFlow="false"
                 transferMode="Buffered"
                 transactionProtocol="OleTransactions"
                 hostNameComparisonMode="StrongWildcard"
                 listenBacklog="10"
                 maxBufferPoolSize="524288"
                 maxBufferSize="65536"
                 maxConnections="10"
                 maxReceivedMessageSize="65536">
          <readerQuotas maxDepth="32"
                        maxStringContentLength="8192"
                        maxArrayLength="16384"
                        maxBytesPerRead="4096"
                        maxNameTableCharCount="16384" />
          <reliableSession ordered="true"
                           inactivityTimeout="00:10:00"
                           enabled="false" />
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

Six*_*aez 8

除非您将ServiceHost的服务实例引用作为构造函数参数提供,否则无法让ServiceHost为您提供服务实例引用.如果您确实提供了该实例引用,那么您正在创建一个通常不是一个好主意的单例服务.

要保持服务的配置,您必须通过客户端进行调用.这实际上比你想象的要容易.由于您的主机代码可以访问服务合同,因此您可以将它与ChannelFactory类一起使用以获取服务的代理.除服务合同外,您必须提供的只是端点名称,ChannelFactory将完成剩下的工作.以下是如何执行此操作的示例:

private IMyServiceContract GetLocalClient(string serviceEndpointName)
{
    var factory = new ChannelFactory<IMyServiceContract>(serviceEndpointName);
    return factory.CreateChannel();
}
Run Code Online (Sandbox Code Playgroud)

更新:除了这种方法,您还应考虑让服务公开NetNamedPipeBinding端点以提高性能.这种绑定几乎完成了内存中的所有操作,并且是同一机器服务调用的最快绑定.