• 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

  • 相关阅读:
    golang 实现生产者消费者模式(转)
    ssh设置免密登录后登录仍需密码可能的原因,以及 ssh 出问题或的调试方法
    http 请求 Cros 跨域问题记录(转)
    问题解决——SSH时出现WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!(转)
    Git撤销本地commit(转)
    golang project 不显示文件夹 或者某个包明明能 import 但就是 import 不进来,提示Unresolved reference
    Qt6.2.4 qml 实现登录注册及显示详情demo
    Qt6.2.4 qml 实现文件选择与内容读取
    【转载】Qt6.2.4 qml ChartView 实现饼状图与问题解决
    【转载】AntvX6 流程图 demo 基于原生 js 支持导入与导出数据
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2579978.html
Copyright © 2020-2023  润新知