【问题标题】:How to make TCP enabled on lan如何在局域网上启用 TCP
【发布时间】:2011-10-16 09:27:01
【问题描述】:

我已经为我的 WPF 应用程序编写了使用 WCF 的代码,wcf 作为 Windows 服务运行,我希望程序 wpf 在客户端计算机上运行。我在 PC 上安装了 WPF 应用程序以及服务和 sql 服务器,它在那里工作正常。但是,当我在另一台 PC 上安装 wpf 客户端时,客户端 WPF 无法连接到服务器。我的意思是 WCF windows 服务不支持局域网使用,但是我设计它只是为了这个目的。请检查我的代码并告诉我哪里出错了。

WPF 应用代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WorkMateWPF.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IWCFBatchImport" 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" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
                <binding name="NetTcpBinding_IWCFCourses" 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" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
                <binding name="NetTcpBinding_IWCFFaculty" 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" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
                <binding name="NetTcpBinding_IWCFLogin" 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" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
                <binding name="NetTcpBinding_IWCFStudent" 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" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:8732/WCFBatchImports"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IWCFBatchImport"
                contract="ServiceBatchImports.IWCFBatchImport" name="NetTcpBinding_IWCFBatchImport">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://localhost:8732/WCFCourses" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IWCFCourses" contract="ServiceCourses.IWCFCourses"
                name="NetTcpBinding_IWCFCourses">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://localhost:8732/WCFFaculty" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IWCFFaculty" contract="ServiceFaculty.IWCFFaculty"
                name="NetTcpBinding_IWCFFaculty">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://localhost:8732/WCFLogin" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IWCFLogin" contract="ServiceLogins.IWCFLogin"
                name="NetTcpBinding_IWCFLogin">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://localhost:8732/WCFStudents" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IWCFStudent" contract="ServiceStudents.IWCFStudent"
                name="NetTcpBinding_IWCFStudent">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
    <userSettings>
        <WorkMateWPF.MySettings>
            <setting name="Test" serializeAs="String">
                <value />
            </setting>
        </WorkMateWPF.MySettings>
    </userSettings>
</configuration>

以下是服务应用代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
  </configSections>
  <connectionStrings>
    <add name="WorkMateWCF.My.MySettings.workmateConnectionString"
      connectionString="Data Source=PAVAN-PC;Initial Catalog=WorkMate;Persist Security Info=True;User ID=WorkMateUser;Password=workmateuser"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WorkMateWCF.WCFLogin">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IWCFLogin">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/WCFLogin" />
          </baseAddresses>
        </host>
      </service>
      <service name="WorkMateWCF.WCFCourses">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IWCFCourses">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/WCFCourses" />
          </baseAddresses>
        </host>
      </service>
      <service name="WorkMateWCF.WCFStudent">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IWCFStudent">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/WCFStudents" />
          </baseAddresses>
        </host>
      </service>
      <service name="WorkMateWCF.WCFBatchImport">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IWCFBatchImport">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/WCFBatchImports" />
          </baseAddresses>
        </host>
      </service>
      <service name="WorkMateWCF.WCFFaculty">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IWCFFaculty">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/WCFFaculty" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

期待有需要的人。谢谢。

【问题讨论】:

    标签: wpf wcf service


    【解决方案1】:

    在服务和客户端配置文件中,您的所有服务地址似乎都设置为 localhost。本地主机总是指运行程序的机器。

    您需要为服务使用正确的机器名称并在客户端中引用它。

    例如,假设您的服务托管在网络上具有以下地址的计算机上 - myservicebox:8732。服务的配置文件如下所示:

    <service name="WorkMateWCF.WCFLogin">
      <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
                contract="WorkMateWCF.IWCFLogin">
        <identity>
          <dns value="myservicebox" />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
                contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://myservicbox:8732/WCFLogin" />
          </baseAddresses>
        </host>
      </service>
    

    然后,您可以在希望使用该服务的客户中引用 net.tcp://myservicebox:8732/WCFLogin

    另外,请注意,您的服务(基于您发布的配置文件)将使用指定绑定的默认值,如果您需要不同的值(如更大的消息大小等),这可能会给您带来问题。

    当您通过bindingConfiguration 属性将它们分配给客户端端点时,您的客户端配置将使用您设置的绑定。

    在您的情况下不一定是问题(看起来您无论如何都在使用客户端上的默认值,一目了然),但需要注意一些事情。

    【讨论】:

    • 感谢您的信息。我是否需要以任何方式主持此活动?我已经使用 Windows 服务项目安装了它。等待你的答复。谢谢
    • 在 Windows 服务中托管您的 WCF 服务很好 - 这完全取决于您的需要和要求(您也可以在 IIS/WAS 下自行托管和托管)。我通常使用 IIS,但我有一项工作在 Windows 服务中托管。
    • 我尝试将 localhost 的名称更改为 pc 名称,但是,现在您的应用程序也无法在自己的 pc 上运行。简单的更换有什么问题吗。我的程序仅适用于 10-15 台的小型网络,不适用于互联网。期待。
    • 嗯...您可能想尝试使用机器的 IP 地址。不要忘记端口 - 例如,net.tcp://192.168.1.1:8732/WCFLogin。只需将 192.168.1.1 替换为服务所在机器的 IP 地址即可。
    • 不确定那个 - 我必须检查一下。出于测试目的,我只需删除 DNS 值标签,看看它是否有效。
    【解决方案2】:

    我后来检查了事件日志,问题在于绑定时间,我将其更改为 00:10:00,它似乎可以工作,但这次我遇到了一个新错误,它与此无关问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2015-06-07
      • 2021-03-07
      • 2023-03-11
      • 2015-05-28
      • 1970-01-01
      相关资源
      最近更新 更多