zoukankan      html  css  js  c++  java
  • 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();
            }
        }



     

  • 相关阅读:
    java.nio.channels.ClosedChannelException
    问题记录【CentOS磁盘空间满】
    vue@2.5.2 对等的vue-template-compiler【Vue】
    Azkaban 常见问题记录
    DataFrame 对其列的各种转化处理
    CICD
    Git通
    Hue问题记录
    多文件的wc程序【java版】
    Caused by: java.lang.RuntimeException: java.lang.Integer is not a valid external type for schema of
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2579979.html
Copyright © 2011-2022 走看看