如何在lan上启用TCP

时间:2011-10-16 09:27:02

标签: wpf wcf service

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

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>

期待有需要。谢谢。

2 个答案:

答案 0 :(得分:1)

在服务和客户端配置文件中,似乎所有服务地址都设置为localhost。 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属性将其分配给客户端端点时设置的绑定。

在您的情况下不一定是个问题(看起来您无论如何都在使用客户端上的默认值,快速浏览一下),但需要注意的事项。

答案 1 :(得分:0)

我后来检查了事件日志,问题是关于绑定的时间,我把它更改为00:10:00它似乎工作,但这次我登陆了一个新的错误,它与此无关问题