Silverlight WCF Self Hosting seemed not to locate ClientAccessPolicy.xml

十年热恋 提交于 2019-12-11 04:32:40

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/5953932/silverlight-wcf-self-hosting-seemed-not-to-locate-clientaccesspolicy-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!