WCF 局域网内无法访问服务器

llh24883 2017-12-03 05:27:59
本人小白,做了一个WCF的服务,宿主为IIS,提供数据库查询功能,客户端为WPF应用程序,有登录和数据显示。
在本地运行,客户端程序正常登录和显示,但将WCF的服务放到局域网内的其他机器上,客户端就登录不了了,表现为连接超时:



服务器配置:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<!--使用 Sessionid 来实现推送数据信息(适用于一台机器上有1个以上客户端)-->
<!--使用 HostName 来实现推送数据信息(适用于一台机器上只有1个客户端)-->
<add name="SendMessageType" connectionString="Sessionid"/>
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<!--
有关 web.config 更改的说明,请参见 http://go.microsoft.com/fwlink/?LinkId=235367。

可在 <httpRuntime> 标记上设置以下特性。
<system.Web>
<httpRuntime targetFramework="4.5" />
</system.Web>
-->
<system.web>
<compilation targetFramework="4.5" debug="true"/>
<httpRuntime targetFramework="4.5"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding closeTimeout="00:30:00" openTimeout="00:30:00"
receiveTimeout="00:30:00" sendTimeout="00:00:10" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
messageEncoding="Text">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="IDRMSService.GetData">
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.103:8088/GetData" />
</baseAddresses>
</host>
<endpoint address="" binding="wsDualHttpBinding" contract="IDRMSService.IGetData"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="2000" maxConcurrentSessions="1000"></serviceThrottling>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="wsDualHttpBinding" scheme="http"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/>
</modules>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>


客户端配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IGetData" receiveTimeout="00:30:00"
sendTimeout="00:00:10">
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.1.103:8088/GetData.svc" binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_IGetData" contract="IDRMSService.IGetData"
name="WSDualHttpBinding_IGetData">
</endpoint>
</client>
</system.serviceModel>
</configuration>


客户端引用服务,专门创建了一个获取服务实例的类:

public class ClientInstance
{
public static GetDataClient client { get; set; }

public ClientInstance()
{
try
{
client = new GetDataClient(new InstanceContext(new CallBack()));
}catch(Exception ex)
{
ErrorLog.Error(ex);
client = null;
}
}
}

CallBack为回调类
该类在登录界面启动后,创建实例
通过断点调试,发现client实例有如下错误



不知道这个问题是否是连接服务器失败的原因;在本地调试时,也会出现这个问题,但不会影响访问,运行正常。

项目的实际使用情况:
本地开发完成后,将WCF服务放到外网服务器上使用,客户端生成后,放到外网服务器所在的局域网内使用。
客户端在本地添加服务时,下载的服务器代码是在本地,上传至外网时,将整个解决方案的IP地址全部替换掉,换成了外网服务器的IP地址。在服务器所在局域网内,通过浏览器能访问到服务,就是客户端程序不行。

局域网内访问服务
引用


求各位大神解我疑惑,不胜感激!
...全文
897 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
微笑&星空 2020-04-04
  • 打赏
  • 举报
回复
我是一个编程初学者,最近也遇到了这个问题,在同一台机器运行服务端和客户端,客户端能正常访问到服务端的,但是在局域网内的其他机器上运行客户端时就无法访问到服务端了。能做个Demo或者有好的参考资料推荐吗?
XBodhi. 2017-12-23
  • 打赏
  • 举报
回复
问题解决就好。
llh24883 2017-12-15
  • 打赏
  • 举报
回复
引用 5 楼 qiaohuyue 的回复:
发源码, qiaohuyue@163.com 帮你看下
前段时间一直在忙其他事情,这块就没顾上,不过利用空闲时间又研究了下,发现双工模式,有两种:wsDualHttpBinding和netTcpBinding。 前者对于纯局域网貌似是不支持的,难怪我本机所在的局域网是可以用的,毕竟该局域网有外网的支持;而我上述的问题的实际环境是服务器无外网连接,只能在局域网内访问。 后者是通过TCP方式进行连接,支持纯局域网的情况,所有把模式由wsDualHttpBinding改成了netTcpBinding,现在系统正常运行起来了! 网上资料及自己理解,要是有错,请纠正! 最后,非常感谢各位能抽空对我的问题进行回复!谢谢!
XBodhi. 2017-12-12
  • 打赏
  • 举报
回复
发源码, qiaohuyue@163.com 帮你看下
llh24883 2017-12-05
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
你是发布在iis上了么?关闭端口防火墙
是的,发布在IIS上,防火墙也是关闭的,局域网内,通过浏览器也是可以访问服务的。 有个问题,就是我的客户端在本地或本地局域网内下载的服务器引用,然后一起搬到其他局域网下,客户端是不是只需要修改配置文件中终结点的IP地址和端口,还是需要在新局域网内重新下载服务?
threenewbee 2017-12-04
  • 打赏
  • 举报
回复
你是发布在iis上了么?关闭端口防火墙
llh24883 2017-12-04
  • 打赏
  • 举报
回复
引用 1 楼 qiaohuyue 的回复:
你看下是不是 契约属性的问题,因为没有 源代码服务端的不好推断。
服务契约

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallbackService))]
    public interface IGetData
    {
        /// <summary>
        /// 根据填卡人ID获取集合
        /// </summary>
        /// <param name="tkrId"></param>
        /// <returns></returns>
        [OperationContract]
        List<IncompleteModel> GetList(string tkrId);

        /// <summary>
        /// 客户端注册上线
        /// </summary>
        [OperationContract(IsOneWay = false)]
        void Register(string id);

        /// <summary>
        /// 客户端发送消息
        /// </summary>
        /// <param name="id">患者编码</param>
        [OperationContract(IsOneWay = true)]
        void ClientSendMessage(string id);

        /// <summary>
        /// 客户端关闭
        /// </summary>
        /// <param name="id">客户端ID</param>
        [OperationContract(IsOneWay = true)]
        void ClientClose(string id);

        /// <summary>
        /// 客户端登录
        /// </summary>
        /// <param name="userName"></param>
        [OperationContract]
        bool ClientLogin(string userName);

        /// <summary>
        /// 客户端发送心跳包
        /// </summary>
        /// <param name="message"></param>
        [OperationContract(IsOneWay = true)]
        void ClientSendBeat(string message);
    }

    public interface ICallbackService
    {
        /// <summary>
        /// 服务器向客户端发送消息
        /// </summary>
        /// <param name="list">集合</param>
        /// <param name="flag">标识——审核未通过和其他4类的区别</param>
        [OperationContract(IsOneWay = true)]
        void SendMessage(List<IncompleteModel> list, string flag);

        /// <summary>
        /// 发送心跳包
        /// </summary>
        /// <param name="message"></param>
        [OperationContract(IsOneWay = true)]
        void SendBeat(string message);
    }
现在出现新的问题,就是我将WCF服务发布到本机所在局域网的其他电脑上,可以远程访问登录,IIS没有做任何修改,服务的配置做了一点改动。但现在问题是,我将本局域网访问成功的程序放到外网上的另一个局域网内时,就又访问不了了,IIS的配置方面,我看了下,没找出啥不同,防火墙也是关闭的,就是在调用服务方法时,出现超时现象。 服务端配置改动:

    <services>      
      <service name="IDRMSService.GetData">
       <!--只是将这部分注掉了-->
        <!--<host>
          <baseAddresses>
            <add baseAddress="http://192.168.1.103:8088/GetData" />
          </baseAddresses>
        </host>-->
        <endpoint address="" binding="wsDualHttpBinding" contract="IDRMSService.IGetData" bindingConfiguration="myBindingNone"></endpoint>
        
      </service>
    </services>
搞的很头大
XBodhi. 2017-12-04
  • 打赏
  • 举报
回复
你看下是不是 契约属性的问题,因为没有 源代码服务端的不好推断。

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧