• WCF 跨域 Http绑定


    跨域服务:

    特性WebGet在System.ServiceModel.Web中

    View Code
        [ServiceContract]
        public interface IDomainService
        {
            [OperationContract]
            [WebGet(UriTemplate = "ClientAccessPolicy.xml")]
            Message ProvidePolicyFile();
        }
    
        public class DomainService:IDomainService
        {
            #region IDomainService 成员
    
            public System.ServiceModel.Channels.Message ProvidePolicyFile()
            {
    
                /*
                    这样第二次访问会产生文件独占的异常,应该应改为使用 MemoryStream 缓存读取,或者直接使用
                    XmlReader reader = XmlReader.Create(@"ClientAccessPolicy.xml");
                 */
    
                 //FileStream filestream = File.Open(@"ClientAccessPolicy.xml", FileMode.Open,FileAccess.Read);
                //XmlReader reader = XmlReader.Create(filestream);   
    
                //ClientAccessPolicy.xml文件要放在exe文件同级
                 XmlReader reader = XmlReader.Create(@"ClientAccessPolicy.xml");
                System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
    
               return result;
            }
    
            //CrossDomainServiceBehavior
    
            #endregion
        }

    普通WCF服务:

    View Code
        [ServiceContract]
        public interface IWCFService
        {
            [OperationContract]
            int DoWork(int a, int b);
    
            [OperationContract]
            int DoWork2(int a, int b);
        }
    
        public class WCFService : IWCFService
        {
            public int DoWork(int a,int b)
            {
                return a + b;
            }
    
            public int DoWork2(int a, int b)
            {
                return a - b;
            }
        }

    跨域策略文件clientaccesspolicy.xml:

    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <access-policy>
      <cross-domain-access>
        <policy>
          <allow-from http-request-headers="*">
            <domain uri="*"/>
          </allow-from>
          <grant-to>
            <resource path="/" include-subpaths="true"/>
          </grant-to>
        </policy>
      </cross-domain-access>
    </access-policy>

    配置文件App.config:

    View Code
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
          <services>
            <service behaviorConfiguration="WCFBase.WCFServiceBehavior" name="WCFBase.WCFService">
              <endpoint address="" binding="basicHttpBinding" contract="WCFBase.IWCFService">
                <identity>
                  <dns value="localhost" />
                </identity>
              </endpoint>
              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost:9090/WCFService/" />
                </baseAddresses>
              </host>
            </service>
            <service name="WCFBase.DomainService">
              <endpoint address="" behaviorConfiguration="DomainServiceBehavior"
                  binding="webHttpBinding" contract="WCFBase.IDomainService" />
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost:9090/" />
                </baseAddresses>
              </host>
            </service>
          </services>
          
          <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="WCFBase.WCFServiceBehavior">
                        <serviceMetadata httpGetEnabled="true" />
                        <serviceDebug includeExceptionDetailInFaults="false" />
                    </behavior>
                </serviceBehaviors>
              <endpointBehaviors>
                <behavior name="DomainServiceBehavior">
                  <webHttp/>
                </behavior>
              </endpointBehaviors>
            </behaviors>
          
          <!--
          <bindings>
            <basicHttpBinding>
              <binding name="NoneSecurity"
       maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false">
                <readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/>
                <security mode="None"/>
              </binding>
            </basicHttpBinding>
          </bindings>
          -->
          
        </system.serviceModel>
     
    </configuration>

    启动方法

    View Code
        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(WCFService));
                host.Open();
    
                ServiceHost crossDomainserviceHost = new ServiceHost(typeof(DomainService));
                crossDomainserviceHost.Open();
    
                Console.WriteLine("Service Start...");
                Console.ReadKey();
                host.Close();
            }
        }



     

  • 相关阅读:
    begin lydsy 2731
    关于js中this关键字的补充
    js中this关键字测试集锦
    js文件中函数前加分号和感叹号是什么意思?
    好用的wget命令从下载添加环境变量到各参数详解
    一个解析json串并组装echarts的option的函数解析
    oschina代码仓库远程push,pull免密实操总结
    yii 核心类classes.php详解(持续更新中...)
    yii2.0归档安装方法
    配置windows 系统PHP系统环境变量
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2579979.html
Copyright © 2020-2023  润新知