Silverlight WCF 自托管似乎找不到 ClientAccessPolicy.xml
我在本地计算机中创建了 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 地址更改或客户端访问策略文件位置。
善意的建议将不胜感激。 先感谢您。
李港
I've created WCF self hosting service in local machine and silverlight App gets data
from this service and send it to remote server. It worked well for more than a month
but suddenly stopped complaining well known errors clientaccesspolicy.xml not resolved.
After spending quite some time to debug, I realized it failed since the remote server
address has been changed into IP address instead of domain addresss, for example
http://2xx.1xx.223 iso http://www.myserver.com, but domain address is not avaliable anymore
so I can't reproduce it and not sure really address changing is the criminal.
It still works fine if webserver and self hosting service both are running in my dev
machine and I can see file in my browser as "http://localhost:8000/clientaccesspolicy.xml"
but 404 error if typed "http://my-machine-name:8000/clientaccesspolicy.xml".
As I read some blogs,clientaccesspolicy.xml should be located in 80 port of local machine
but dno't know how to do and not sure it makes the problem.
My service host is configured as follows;
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());
And clientacceccpolicy.xml is used through interface
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);
}
And silverlight client uses service with proxy without ServiceReferences.ClientConfig
but with service refrence to easily get web methods.
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();
}
}
I read many threads here and google but not quite like mine and still vague to me
which one is the problem, IP address change or clientaccesspolicy file location.
Kind advice would be appreciated.
Thank you in advance.
HK.Lee
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用 2 个小方法制作了小型 SL 测试应用程序,并更改了 ClientConfig 的端点地址
进入 http://ipv4.fiddler:8000 而不是 http://locahost:8000。
Fiddler 从 127.0.0.1 iso localhost 查找 clientaccesspolicy.xml,
所以我将 SL 客户端代理地址更改为 127.0.0.1 而不是 localhost。
一切正常。
有趣的是,如果从 IP 地址 vc 域名下载 xap,为什么 localhost 不起作用?
我不知道细节,但有人给一些建议。
李港
I made small SL test app with 2 small methods and change endpoint address of ClientConfig
into http://ipv4.fiddler:8000 instead of http://locahost:8000.
Fiddler looks clientaccesspolicy.xml from 127.0.0.1 iso localhost,
so I changed my SL client proxy address into 127.0.0.1 instead of localhost.
Everything works fine.
It's funny why localhost does not work if xap is downloaded from IP address vc domain name?
I don't know the details but anyone give some advice.
HK.lee