博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WCF 跨域TCP绑定
阅读量:5085 次
发布时间:2019-06-13

本文共 3366 字,大约阅读时间需要 11 分钟。

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="" />
              <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="" />
            </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

 

转载于:https://www.cnblogs.com/FlyCat/archive/2012/05/01/2579978.html

你可能感兴趣的文章
使用Gitblit 在windows 上部署你的Git Server
查看>>
217. Contains Duplicate
查看>>
vue2.0 关于Vue实例的生命周期
查看>>
jenkins 更换主数据目录
查看>>
Silverlight中恼人的g.i.cs错误
查看>>
SQLite 数据库增删改查
查看>>
<s:iterator>的status
查看>>
C++入门--1.0输入输出
查看>>
让搭建在Github Pages上的Hexo博客可以被Google搜索到
查看>>
Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十四章:曲面细分阶段...
查看>>
在WPF控件上添加Windows窗口式调整大小行为
查看>>
背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
查看>>
打开3389
查看>>
React学习记录
查看>>
nginx常见内部参数,错误总结
查看>>
对象与类
查看>>
《奸的好人2》财色战场----笔记
查看>>
BZOJ 1834网络扩容题解
查看>>
bzoj1878
查看>>
【Vegas原创】Mysql绿色版安装方法
查看>>