关键代码及步骤:
1 首先在WCF类库服务中添加System.ServiceModel.Web引用(引用System.ServiceModel.Web时必须把类库设FrameWork 4)
2 定义接口IDomainService(名字可以随便,但后面必须跟配置文件保持一致)
代码如下:
[ServiceContract] public interface IDomainService { [OperationContract] [WebGet(UriTemplate = "ClientAccesspolicy.xml")] Message ProvidePolicyFile(); }
实现接口IDomainService
public class DomainService : IDomainService
{
public System.ServiceModel.Channels.Message ProvidePolicyFile()
{
FileStream filestream = File.Open(@"ClientAccesspolicy.xml",FileMode.Open);
XmlReader reader = XmlReader.Create(filestream);
System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None,"",reader);
return result;
}
}
3 控制台应用程序除了打开之前需要打开的服务外,还需打开刚建立的DomainService服务,代码如下:
namespace ConsoleApplicationHttp { class Program { static ServiceHost host; static void Main(string[] args) { try { host = new ServiceHost(typeof(WCFNetHttpLib.Service1));//Wcf服务 host.open(); ServiceHost crossDomainserviceHost = new ServiceHost(typeof(WCFNetHttpLib.DomainService));//跨域处理服务 crossDomainserviceHost.open(); Console.WriteLine("my,service opened!"); Console.ReadKey(); } catch (Exception ex) { Console.Write("service opened Failed!" + ex.Message); Console.ReadKey(); } host.Close(); } } }
4 控制台中app.config中添加DomainServiceBehavior配置节点(红色部分为新增节点)
<?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service behaviorConfiguration="ServiceBehavior" name="WCFNetHttpLib.Service1"> <endpoint address="http://localhost:8080/Service1/" binding="basicHttpBinding" bindingConfiguration="" contract="WCFNetHttpLib.IService1"/> <endpoint address="http://localhost:8080/Service1/MEX/" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange"/> </service> <service name="WCFNetHttpLib.DomainService"> <endpoint address="" behaviorConfiguration="DomainServiceBehavior" binding="webHttpBinding" contract="WCFNetHttpLib.IDomainService" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/" /> </baseAddresses> </host> </service> </services> <behaviors> <endpointBehaviors> <behavior name="DomainServiceBehavior"> <webHttp/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。