Accessing self hosted WCF service from Silverlight 4 -
i have self hosted wcf 4 service, catering same contract via basichttpbinding silverlight 4 clients , wshttpbinding others. code short , simple , provided here.
i following error when trying access service method wcf:
message=an error occurred while trying make request uri
http://localhost:8008/wcf4silverlight.myservice/sl
. due attempting access service in cross-domain way without proper cross-domain policy in place, or policy unsuitable soap services. may need contact owner of service publish cross-domain policy file , ensure allows soap-related http headers sent. error may caused using internal types in web service proxy without using internalsvisibletoattribute attribute. please see inner exception more details.
i have method, getclientaccesspolicy() serving cross-domain policy using webget attribute, , kind of sure there problem getting exposed properly. insight problem highly appreciated. if type http://localhost:8008/wcf4silverlight.myservice/clientaccesspolicy.xml
in browser, xml same, call silverlight fails above error.
here code wcf service:
namespace wcf4silverlight { [servicecontract(sessionmode = sessionmode.notallowed)] public interface iclientaccesspolicy { [operationcontract, webget(uritemplate = "/clientaccesspolicy.xml")] stream getclientaccesspolicy(); } } namespace wcf4silverlight { public class myservice: imyservice, iclientaccesspolicy { public stream getclientaccesspolicy() { const 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>"; if (weboperationcontext.current != null) weboperationcontext.current.outgoingresponse.contenttype = "application/xml"; return new memorystream(encoding.utf8.getbytes(result)); } } //other service methods.... }
here code publishes service:
class program { static void main(string[] args) { servicehost myservicehost = new servicehost(typeof(myservice)); myservicehost.open(); //wait client action. myservicehost.close(); } }
here app.config wcf service host:
<service name="wcf4silverlight.myservice" behaviorconfiguration="myservicebehavior"> <host> <baseaddresses> <add baseaddress="http://localhost:8008/myservice/"/> </baseaddresses> </host> <endpoint address="general" binding="wshttpbinding" bindingconfiguration="wshttpbinding_imyservice" contract="wcf4silverlight.imyservice"/> <endpoint address="sl" binding="basichttpbinding" bindingconfiguration="basichttpbinding_imyservice" contract="wcf4silverlight.imyservice"/> <endpoint address="" binding="webhttpbinding" bindingconfiguration="webhttpbinding_imyservice" behaviorconfiguration="webhttpbehavior" contract="wcf4silverlight.iclientaccesspolicy" /> </service>
and here servicereferences.clientconfig silverlight client:
<system.servicemodel> <bindings> <basichttpbinding> <binding name="basichttpbinding_imyservice" maxbuffersize="2147483647" maxreceivedmessagesize="2147483647"> <security mode="none" /> </binding> </basichttpbinding> <custombinding> <binding name="wshttpbinding_imyservice"> <textmessageencoding messageversion="default" writeencoding="utf-8" /> <httptransport maxreceivedmessagesize="2147483647" maxbuffersize="2147483647" /> </binding> </custombinding> </bindings> <client> <endpoint address="http://localhost:8008/myservice/sl" binding="basichttpbinding" bindingconfiguration="basichttpbinding_imyservice" contract="mywcfservice.imyservice" name="basichttpbinding_imyservice" /> </client> </system.servicemodel>
this did resolve issue:
1) used fiddler see wcf calls directed. fiddler told calls failing host - http:/localhost:8008 , url - /clientaccesspolicy.xml.
2) created different class clientaccesspolicy implementing iclientaccesspolicy (with webget /clientaccesspolicy.xml).
3) added section in app.config of host new service hosting clientaccesspolicy class. 1 had base address http:/localhost:8008/
<service name="wcf4silverlight.clientaccesspolicy" behaviorconfiguration="clientaccesspolicybehavior"> <host> <baseaddresses> <add baseaddress="http://localhost:8008/"/> </baseaddresses> </host> <endpoint address="" binding="webhttpbinding" bindingconfiguration="webhttpbinding_imyservice" behaviorconfiguration="webhttpbehavior" contract="wcf4silverlight.iclientaccesspolicy" /> </service>
4) in hosting code, created instance of servicehost , launched new service clientaccesspolicy
servicehost clientaccesspolicyhost = new servicehost(typeof(clientaccesspolicy)); clientaccesspolicyhost.open();
5) in silverlight client, deleted existing reference wcf , added 1 newly hosted service.
the wcf calls silverlight going through.
Comments
Post a Comment