【问题标题】:WCF service works with localhost, but not with an IP addressWCF 服务适用于 localhost,但不适用于 IP 地址
【发布时间】:2020-10-26 18:25:20
【问题描述】:

我查看了其他类似的问题,但无法解决我自己的问题。

我有一个 WCF 服务,如果我通过以下方式连接它就可以使用:

http://localhost:35001/SchoolLightService.svc

但如果我使用计算机的 IP 地址(来自 LAN 内)则不是:

http://192.168.1.4:35001/SchoolLightService.svc

错误信息是(必须将 ip 号(上面那个)更改为“我的 ip”才能取悦 Stackoverflow):

错误:无法从 http://"my ip":35001/SchoolLightService.svc 如果这是 Windows (R) 您有权访问的通信基础服务,请 检查您是否已在指定位置启用元数据发布 地址。有关启用元数据发布的帮助,请参阅 MSDN 文档位于 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata交流 错误 URI:http://"my ip":35001/SchoolLightService.svc
元数据包含无法解析的引用: 'http://"我的 ip":35001/SchoolLightService.svc'。没有 端点监听 http://"my ip":35001/SchoolLightService.svc 可以接受该消息。这通常是由不正确的 地址或 SOAP 操作。有关更多信息,请参阅 InnerException(如果存在) 细节。无法连接到远程服务器 无连接 可以因为目标机器主动拒绝它 "my ip":35001HTTP GET Error URI: http://"my ip":35001/SchoolLightService.svc 出现错误 正在下载'http://"my ip":35001/SchoolLightService.svc'。
无法连接到远程服务器 无法建立连接 因为目标机器主动拒绝了它 "my ip":35001

web.config 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SLBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpGetUrl="" />
          <!-- 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>
    <services >
      <service name="SchoolLightWCF.SchoolLightService"
              behaviorConfiguration="SLBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.1.4:35001" />
          </baseAddresses>
        </host>
        <endpoint
          address="/SchoolLightService"
          binding="wsHttpBinding"
          contract="SchoolLightWCF.ISchoolLightService"
        />
        <endpoint
           address="mex"
           binding="mexHttpBinding"
           contract="IMetadataExchange"
        />
      </service>

    </services>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <diagnostics>

      <messageLogging
       logMessagesAtTransportLevel="true" logMessagesAtServiceLevel="false"
            logMalformedMessages="true" logEntireMessage="true"
            maxSizeOfMessageToLog="65535000" maxMessagesToLog="500" />

    </diagnostics>
  </system.serviceModel>
  <system.diagnostics>
    <sharedListeners>
      <add name="sharedListener"
        type="System.Diagnostics.XmlWriterTraceListener"
        initializeData="c:\temp\tracelog.svclog" />
    </sharedListeners>
    <sources>
      <source name="System.ServiceModel" switchValue="Verbose, ActivityTracing" >
        <listeners>
          <add name="sharedListener" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="Verbose">
        <listeners>
          <add name="sharedListener" />
        </listeners>
      </source>
      <source name="ApplicationLogging" switchValue="Information" >
        <listeners>
          <add name="sharedListener" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="SLEntities" connectionString="...not relevant..." />
  </connectionStrings>
</configuration>

我做错了什么?

【问题讨论】:

  • 检查防火墙怎么样?
  • 我为端口添加了入站规则,但没有效果。我确实有一个路由器,但这应该没关系,因为我只是在本地网络中工作,不是吗?
  • 我尝试 telnet 到端口。如果我使用本地主机,它工作正常。如果我使用 IP 号(192.168.1.4),它无法连接。 Windows 防火墙被禁用,路由器设置了端口转发。

标签: wcf


【解决方案1】:

我通过将它发布到计算机上运行的真实 IIS 服务器来解决它。出于某种原因,Visual Studio 中的内置 Web 服务器没有解决它。

【讨论】:

  • 哇,我以为你是在 IIS 上做的。内置 Web 服务器无法响应设计的远程请求。
  • 是的,IIS 可以工作,但是在 Visual Studio 中进行调试时有什么方法可以实现这一点?
猜你喜欢
  • 1970-01-01
  • 2011-10-03
  • 2016-09-07
  • 2022-07-11
  • 2020-09-03
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
相关资源
最近更新 更多