我可以在本地WCF自服务中调用方法吗? [英] Can I call a method in a Self-Hosted WCF Service locally?

查看:207
本文介绍了我可以在本地WCF自服务中调用方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WCF Service contract which is basically the Publish Subscriber pattern.

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

The WCF Service is hosted inside the Windows Service that I want to publish from. The Clients subscribe to messages and when the Windows Service does something it publishes to all clients.

要托管服务,我已声明ServiceHost类,并且Contract Class具有一种方法,该方法未在Interface中标记,但在要发布的Class中实现.

To host the service I have declared a ServiceHost class and the Contract Class has a method which is not flagged in the Interface but is implemented in the Class to publish.

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

I want to be able to call this method locally (not going through WCF) which then publishes the message via Callbacks.

我似乎无法从ServiceHost转到Contract Class的实例.

I can't seem to get from ServiceHost to the instance of the Contract Class.

这有可能吗?我知道解决方法是也要在服务中内置一个客户端,但是创建一个客户端来连接自身似乎有点奇怪.

Is this possible and if so how? I know the work around is to have a client built into the service as well, but it seems a bit strange creating a client to connect to itself.

预先感谢

DJIDave

app.config

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>

推荐答案

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

Unless you provide the service instance reference to the ServiceHost as a constructor parameter, there isn't a way to have the ServiceHost provide you an service instance reference. If you do provide that instance reference then you are creating a singleton service which is generally not a good idea.

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

To keep the service as it is configured, you will have to call it through a client. This is actually easier than you might think. Since your host code has access to the service contract, you can use it with the ChannelFactory class to get a proxy for the service. Besides the service contract, all you have to provide is the endpoint name and ChannelFactory will do the rest. Below is an example of how to do this:

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

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

UPDATE: Along with this approach, you should consider having you service expose a NetNamedPipeBinding endpoint to improve performance. This binding pretty much does everything in memory and is the fastest binding for same machine service invocation.

这篇关于我可以在本地WCF自服务中调用方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆