WCF调用本地Windows服务而不是服务器

时间:2016-02-16 17:30:39

标签: c# vb.net wcf

我在Windows服务中托管了WCF服务。我想要做的是调用位于客户端PC上的Windows服务(WS)和WS在同一客户端计算机上调用客户端DLL。 我已经在每台客户端PC和服务器上安装了WS。当导航到服务器上的ASP.NET页面并调用该方法时,它工作正常,但是客户端PC不是从本地WS调用它们的DLL,而是调用服务器上调用的WS - 这不是期望的行为。此环境将安装在LAN上。

我的一个示例方法是获取PC名称,这反过来我得到了服务器的名称。

这是我的app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <!-- This section is optional with the new configuration model
           introduced in .NET Framework 4. -->
      <service name="Service.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
          </baseAddresses>
        </host>
        <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service  -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Service.ICalculator" />
        <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我的服务类:

Public Interface ICalculator
    <OperationContract()> _
    Function GetPCname() As String
      Sub OpenPort()
    <OperationContract()> _
End Interface

Public Class CalculatorService
    Implements ICalculator

   <DllImport("C:\App_32bit\test.dll", CharSet:=CharSet.Ansi)> _
    Private Shared Function DrvOpen(ByVal cha As String, ByRef ope As T_open, ByRef out As T_open_out) As Short
    End Function

    Public Function GetPCname() As String Implements ICalculator.GetPCname
        Return Environment.MachineName
    End Function

    Public Sub OpenPort() Implements ICalculator.OpenPort
     'some code.
    End Function
End Class

如何设法调用我的LOCAL服务而不是服务器的Windows服务?

1 个答案:

答案 0 :(得分:0)

您应该在安装和配置了Windows服务的每台网络PC上调用方法,在您的服务器上调用方法意味着它只会在您的服务器上执行一次。您需要在所有计算机上调用所有方法,例如:

192.168.1.1\MyWindowsService\Method1
192.168.1.2\MyWindowsService\Method1
...
etc.

我建议创建不同的Windows服务,它只能在服务器上运行并调用局域网机器上的方法。

相关问题