gpt4 book ai didi

c# - 我可以在本地调用自承载 WCF 服务中的方法吗?

转载 作者:可可西里 更新时间:2023-11-01 09:05:41 27 4
gpt4 key购买 nike

我有一个 WCF 服务契约(Contract),它基本上是发布订阅者模式。

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

为了托管服务,我声明了一个 ServiceHost 类,并且 Contract 类有一个方法,该方法未在接口(interface)中标记但在要发布的类中实现。

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

我似乎无法从 ServiceHost 获取 Contract 类的实例。

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

提前致谢

大疆通

应用程序配置

<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 为您提供服务实例引用。如果您确实提供了该实例引用,那么您将创建一个单例服务,这通常不是一个好主意。

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

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

更新:除了这种方法,您应该考虑让您的服务公开一个 NetNamedPipeBinding endpoint以提高性能。此绑定(bind)几乎在内存中执行所有操作,并且是同一机器服务调用的最快绑定(bind)。

关于c# - 我可以在本地调用自承载 WCF 服务中的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6070078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com