一、创建一个基于TCP绑定的WCF服务

1、创建一个的简单的服务具体代码如下

服务契约定义
namespace SimpleService
{
      // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IHelloService”。
      [ServiceContract]
      public interface IHelloService
      {
          [OperationContract]
          string GetMessage(string message);
      }
}
服务具体方法实现
namespace SimpleService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“HelloService”。
    public class HelloService : IHelloService
    {
        public string GetMessage(string message)
        {
            return message + "\t" + DateTime.Now;
        }
    }
}

 

在Web应用程序中创建SVC文件指向服务代码
 <%@ ServiceHost Language="C#" Debug="true" Service="SimpleService.HelloService" %>
具体配置文件设置如下
  <system.serviceModel>
    <services>
      <service name="SimpleService.HelloService" behaviorConfiguration="HttpGetEnable">
        <!--设置绑定协议为TCP协议-->
        <endpoint address="" bindingConfiguration="NoneSecurity" binding="netTcpBinding" contract="SimpleService.IHelloService">
        </endpoint>
        <!--源数据访问节点-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
        </endpoint>
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <!--允许端口共享,使其他基于TCP绑定的WCF服务也能使用808端口-->
        <binding name="NoneSecurity" portSharingEnabled="true">
          <!--取消安全验证-->
          <security mode="None">
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <!--允许通过http协议获取源数据-->
        <behavior name="HttpGetEnable">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--启用多站点绑定,方便使用IP地址或者域名访问-->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
  </system.serviceModel>
  <system.webServer>
    <!--启用目录浏览方便使用-->
    <directoryBrowse enabled="true"/>
  </system.webServer>
解决方案具体配置如下

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务

二、设置 IIS增加非Http协议支持

打开控制面——选择大图标——程序和功能——启用或关闭windows功能——勾选.net3.5中非Http激活,如果是win8/8.1/10则需要勾选.net4.6中所有的高级功能

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务

在IIS中创建要托管的站点目录指向WCF服务Web宿主所在目录

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务

增加站点非HTTP绑定支持

右键选择站点——编辑绑定——添加——类型选择net.tcp——绑定信息808:* 

IIS 中托管基于TCP绑定的WCF服务
IIS 中托管基于TCP绑定的WCF服务

设置站点支持net.tcp协议,右键站点——管理网站——高级设置——已启用的协议——添加net.tcp 

IIS 中托管基于TCP绑定的WCF服务

三、最终效果


在浏览器中访问站点的SVC文件,效果如下图

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务


源数据中可以看到已经是net.tcp绑定

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务

在WCFTestClient中调用服务

添加服务地址

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务


调用服务

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务


服务客户端配置文件信息

IIS 中托管基于TCP绑定的WCF服务

IIS 中托管基于TCP绑定的WCF服务

后记

在正式使用中应将客户端配置文件中的主机地址替换为主机的IP地址

相关文章:

  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-04-25
猜你喜欢
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-01-23
  • 2022-03-05
  • 2022-12-23
相关资源
相似解决方案