wcf可以使用iis或者application作为宿主,使用iis的情况部署麻烦.需要装iis,还要设置mime,等等.
公司的wcf从开始的iis宿主,变更成现在的application宿主.不过使用silverlight,需要用http协议去取得相应端口的穿域文件.
最开始解决办法是wcf使用application宿主,然后安装iis,建1个同样虚拟目录同样端口的站点,只放穿域文件.
现在,参考了一些帖子,解决仍需要iis的问题.
例子代码如下.
建立个控制台项目.然后在项目上添加wcf服务.改名成MyService.cs
然后代码为
MyService.cs
- public class MyService : IMyService
- {
- public string DoWork()
- {
- return "This is A Sample From cross-domain.by Magic_mm";
- }
- }
IMyService.cs 为
- [ServiceContract]
- public interface IMyService
- {
- [OperationContract]
- string DoWork();
- }
下面再添加个wcf服务
名字就叫CrossDomainService.cs
CrossDomainService.cs 为
- public class CrossDomainService : ICrossDomainService
- {
- public System.ServiceModel.Channels.Message ProvidePolicyFile()
- {
- FileStream filestream = File.Open(@"ClientAcessPolicy.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- XmlReader reader = XmlReader.Create(filestream);
- System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
- return result;
- }
- }
ICrossDomainService.cs 为
- [ServiceContract]
- public interface ICrossDomainService
- {
- [OperationContract]
- [WebGet(UriTemplate = "ClientAccessPolicy.xml")]
- Message ProvidePolicyFile();
- }
然后,添加个xml文件吧.
名字就叫ClientAcessPolicy.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>
ps:这里运行会出现url错误,建好后可以手动把它复制1份到debug目录
然后在Program.cs写
- static void Main(string[] args)
- {
- ServiceHost myserviceHost = new ServiceHost(typeof(MyService));
- ServiceHost crossDomainserviceHost = new ServiceHost(typeof(CrossDomainService));
- myserviceHost.Open();
- crossDomainserviceHost.Open();
- Console.WriteLine("服务运行中");
- Console.ReadLine();
- }
好了,还有
app.config 文件修改如下
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.serviceModel>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
- <behaviors>
- <serviceBehaviors>
- <behavior name="wcftest.MyServiceBehavior">
- <serviceMetadata httpGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="false" />
- </behavior>
- </serviceBehaviors>
- <endpointBehaviors>
- <behavior name="wcftest.CrossDomainServiceBehavior">
- <webHttp/>
- </behavior>
- </endpointBehaviors>
- </behaviors>
- <services>
- <service behaviorConfiguration="wcftest.MyServiceBehavior" name="wcftest.MyService">
- <endpoint address="" binding="basicHttpBinding" contract="wcftest.IMyService">
- </endpoint>
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8731/wcftest/MyService/" />
- </baseAddresses>
- </host>
- </service>
- <service
- name="wcftest.CrossDomainService">
- <endpoint address="" binding="webHttpBinding" contract="wcftest.ICrossDomainService" behaviorConfiguration="wcftest.CrossDomainServiceBehavior">
- </endpoint>
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8731/" />
- </baseAddresses>
- </host>
- </service>
- </services>
- </system.serviceModel>
- </configuration>
ps: 我这里的wcftest是我项目名,你需要改成你的.
ok
再建个测试的silverlight项目吧.
编译好wcf,找到项目debug目录下的exe,先运行
现在可以再添加个silverlight项目,完了就添加wcf引用,我的是http://localhost:8731/wcftest/MyService/
我给起个名字叫ConsoleServiceReference
ok,然后我的page.xaml.cs的代码如下
- public partial class Page : UserControl
- {
- public Page()
- {
- InitializeComponent();
- ConsoleServiceReference.MyServiceClient consoleClient = new crosstest.ConsoleServiceReference.MyServiceClient();
- consoleClient.DoWorkCompleted += new EventHandler<crosstest.ConsoleServiceReference
- .DoWorkCompletedEventArgs>(consoleClient_DoWorkCompleted);
- consoleClient.DoWorkAsync();
- }
- void consoleClient_DoWorkCompleted(object sender, crosstest.ConsoleServiceReference
- .DoWorkCompletedEventArgs e)
- {
- if (e.Result != null)
- {
- TextBlock serviceText = new TextBlock();
- serviceText.Text = e.Result;
- LayoutRoot.Children.Add(serviceText);
- }
- }
- }
运行....成功显示This is A Sample From cross-domain.by Magic_mm