跨域服务:
特性WebGet在System.ServiceModel.Web中
[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服务:
[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:
<?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:
<?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>
启动方法
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();
}
}