• WCF 跨域TCP绑定


    Silverlight4在跨域访问TCP时会读取Http 80端口的跨域策略文件,可以放IIS上,也可以在服务控制台托管Http服务

    例子是控制台托管TCP服务和Http跨域策略文件服务

    配置文件:

    <?xml version="1.0"?>
    <configuration>
        <system.serviceModel>
          <services>
            <service name="ConsoleApplication1.DuxpexService">
              <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBindConfig"
                contract="ConsoleApplication1.IDuxpexService" />
              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
              <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost:8732/DuxpexService/" />
                  <add baseAddress="net.tcp://localhost:4505/DuxpexService/" />
                </baseAddresses>
              </host>
            </service>
            <service name="ConsoleApplication1.DomainService">
              <endpoint address="" behaviorConfiguration="DomainServiceBehavior"
                binding="webHttpBinding" bindingConfiguration="" contract="ConsoleApplication1.IDomainService">
                <identity>
                  <dns value="localhost" />
                </identity>
              </endpoint>
              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost/" />
                </baseAddresses>
              </host>
            </service>
          </services>
          
          <bindings>
            <netTcpBinding>
              <binding name="netTcpBindConfig">
                <security mode="None"/>
              </binding>
            </netTcpBinding>
          </bindings>
         
          <behaviors>
                <serviceBehaviors>
                    <behavior name="">
                        <serviceMetadata httpGetEnabled="true"/>
                        <serviceDebug includeExceptionDetailInFaults="false"/>
                    </behavior>
                </serviceBehaviors>
              <endpointBehaviors>
                 <behavior name="DomainServiceBehavior">
                     <webHttp/>
                </behavior>

              </endpointBehaviors>
            </behaviors>
        </system.serviceModel>
     
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
    </configuration>

    TCP跨域策略文件ClientAccessPolicy.xml

    <?xml version="1.0" encoding="utf-8"?>
    <access-policy>
      <cross-domain-access>
        <policy>
          <allow-from http-request-headers="*">
            <domain uri="*" />
          </allow-from>
          <grant-to>
            <socket-resource port="4502-4534" protocol="tcp"/>
      <resource path="/" include-subpaths="true"/>
          </grant-to>
        </policy>
      </cross-domain-access>
    </access-policy>

    跨域服务:

        [ServiceContract]
        public interface IDomainService
        {
            [OperationContract]
            [WebGet(UriTemplate = "ClientAccessPolicy.xml")]
            Message ProvidePolicyFile();
        }

        public class DomainService : IDomainService
        {
            public System.ServiceModel.Channels.Message ProvidePolicyFile()
            {

                XmlReader reader = XmlReader.Create(@"ClientAccessPolicy.xml");
                System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);

                return result;
            }
        }

    普通双向服务:

        [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
        public class DuxpexService : IDuxpexService
        {
            public int GetData()
            {
                IserviceCallBack client = OperationContext.Current.GetCallbackChannel<IserviceCallBack>();
                client.ClientMethod();

                return 10;
            }
        }

        [ServiceContract(CallbackContract = typeof(IserviceCallBack))]
        public interface IDuxpexService
        {
            [OperationContract]
            int GetData();
        }
        public interface IserviceCallBack
        {
            [OperationContract(IsOneWay = true)]
            void ClientMethod();
        }

    启动方法:

        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(DuxpexService));
                ServiceHost domainhost = new ServiceHost(typeof(DomainService));
                domainhost.Open();
                host.Open();
                Console.WriteLine("Service Start...");
                Console.ReadLine();
                domainhost.Close();
                host.Close();
            }
        }


    ClientAccessPolicy.xml

  • 相关阅读:
    SQLServer 事物与索引
    SQLServer 常见SQL笔试题之语句操作题详解
    测试思想-测试设计 测试用例设计之边界值分析方法
    测试思想-测试设计 测试用例设计之等价类划分方法
    测试思想-测试设计 测试用例设计之因果图方法
    测试思想-测试设计 测试用例设计之判定表驱动分析方法
    MySql 缓存查询原理与缓存监控 和 索引监控
    测试思想-测试设计 授客细说场景测试用例设计与实践
    产品相关 细说软件产品和业务 & 业务过程(流程) & 业务逻辑
    Postman Postman接口测试工具使用简介
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2579978.html
Copyright © 2020-2023  润新知