【发布时间】:2011-08-22 16:11:01
【问题描述】:
我在本地机器上创建了 WCF 自托管服务,silverlight 应用程序获取数据 从此服务并将其发送到远程服务器。一个多月运行良好 但突然停止抱怨众所周知的错误 clientaccesspolicy.xml 未解决。 花了很长时间调试后,我意识到它失败了,因为远程服务器 地址已更改为 IP 地址而不是域地址,例如 http://2xx.1xx.223 iso http://www.myserver.com,但域地址不再可用 所以我无法重现它,并且不确定地址更改是否真的是罪犯。
如果网络服务器和自托管服务都在我的开发中运行,它仍然可以正常工作 机器,我可以在浏览器中看到文件为“http://localhost:8000/clientaccesspolicy.xml” 但如果键入“http://my-machine-name:8000/clientaccesspolicy.xml”,则会出现 404 错误。 当我阅读一些博客时,clientaccesspolicy.xml 应该位于本地机器的 80 端口 但不知道该怎么做,也不确定它是否会导致问题。
我的服务主机配置如下;
string baseAddress = "http://localhost:8000";
//Create a ServiceHost for the OPCService type and
//provide the base address.
_serviceHost = new ServiceHost(typeof(OpcService), new Uri(baseAddress));
_serviceHost.AddServiceEndpoint(typeof(IOpcService), new BasicHttpBinding(), new Uri(baseAddress + "/OpcService"));
_serviceHost.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
通过接口使用clientacceccpolicy.xml
private Stream StringToStream(string result)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
return new MemoryStream(Encoding.UTF8.GetBytes(result));
}
public Stream GetSilverlightPolicy()
{
string result = @"<?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>";
return StringToStream(result);
}
silverlight 客户端在没有 ServiceReferences.ClientConfig 的情况下使用带代理的服务 但是通过服务引用可以轻松获取网络方法。
public class ServiceProxy
{
private const string ServiceEndPoint = "http://localhost:8000/OpcService";
private static Uri _serviceMap = new Uri(ServiceEndPoint, UriKind.Absolute);
public static T GetProxyFor<T>()
{
return new ChannelFactory<T>(new BasicHttpBinding(), new EndpointAddress(_serviceMap)).CreateChannel();
}
[Export]
public IOpcService SyrOpcService
{
get { return GetProxyFor<IOpcService>(); }
}
public static SYR.HMI.OpcProxy.ServiceReference.OpcServiceClient GetProxy()
{
return new SYR.HMI.OpcProxy.ServiceReference.OpcServiceClient();
}
}
我在这里和谷歌上阅读了很多主题,但与我的不太一样,对我来说仍然含糊不清 哪一个是问题,IP地址更改或客户端访问策略文件位置。
我们将不胜感激。 提前谢谢你。
HK.李
【问题讨论】:
-
欢迎来到 StackOverflow!