如何在IIS中承载WCF NetTcpBinding 服务

这篇博客将介绍如何在IIS中承载NetTcpBinding的服务。

1. 首先准备服务代码。

Contract

namespace Contract
{
    [ServiceContract]
    public interface ICalculate
    {
        [OperationContract]
        double Add(double x, double y);

        [OperationContract]
        double Subtract(double x, double y);

        [OperationContract]
        double Multiply(double x, double y);

        [OperationContract]
        double Divide(double x, double y);
    }
}

Service

namespace Service
{
    public class Calculate : ICalculate
    {
        public double Add(double x, double y)
        {
            return x + y;
        }

        public double Divide(double x, double y)
        {
            return x - y;
        }

        public double Multiply(double x, double y)
        {
            return x * y;
        }

        public double Subtract(double x, double y)
        {
            return x / y;
        }
    }
}

Web.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MessageBehavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="65535" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Service.Calculate" behaviorConfiguration="MessageBehavior">
        <endpoint address="net.tcp://172.18.20.67:9998/CalculateService.svc" binding="netTcpBinding" contract="Contract.ICalculate" />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

</configuration>

SVC

<%@ServiceHost Service="Service.Calculate"%>

配置IIS环境,需要将Non-HTTP Activation开启,

下一步创建一个WebSite来承载WCF服务,

1). 在Advanced Settings中将net.tcp协议添加,

2).在WebSite的Bindings中添加需要开放的端口号,例如需要开放9998,如图:


最后,需要在Windows防火墙中开启需要端口的访问权限。

这样整个配置就完成了。

posted @ 2017-03-31 12:01  Yang-Fei  阅读(341)  评论(0编辑  收藏  举报