1
votes

I have a WCF Service that I've added to a Silverlight project and is now hosting in Visual Studio's ASP.NET Dev Server. But tyring to run it I get the following error:

System.ServiceModel.CommunicationException was unhandled by user code
  Message="An error occurred while trying to make a request to URI 'http://localhost:51547/WCFService1/Service.svc'. 

This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details."
  StackTrace:
       at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
       at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
       at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
       at niki_lanik_latestnews.WCFXmlOps.IxmlLoadClient.IxmlLoadClientChannel.EndLoadArticleFromXML(IAsyncResult result)
       at niki_lanik_latestnews.WCFXmlOps.IxmlLoadClient.niki_lanik_latestnews.WCFXmlOps.IxmlLoad.EndLoadArticleFromXML(IAsyncResult result)
       at niki_lanik_latestnews.WCFXmlOps.IxmlLoadClient.OnEndLoadArticleFromXML(IAsyncResult result)
       at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
  InnerException: System.Security.SecurityException
       Message=""
       StackTrace:
            at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
            at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
            at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
       InnerException: System.Security.SecurityException
            Message="Security error."
            StackTrace:
                 at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
                 at System.Net.Browser.BrowserHttpWebRequest.c__DisplayClass5.b__4(Object sendState)
                 at System.Net.Browser.AsyncHelper.c__DisplayClass2.b__0(Object sendState)
            InnerException: 

It's not hosted cross domain as it's in the same project, so I'm not sure what is causing this? Any ideas anyone?

Tony

UPDATE

Below is my binding config in web.config:

        <bindings>
        <basicHttpBinding>
            <binding name="basicHTTP" 
             receiveTimeout="00:10:00" 
             sendTimeout="00:10:00" 
             closeTimeout="00:10:00" 
             openTimeout="00:03:00" 
             maxBufferSize="100000" 
             maxReceivedMessageSize="100000" 
             transferMode="StreamedResponse">

    </binding>
        </basicHttpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="mexBehavior" name="LoadXMLService.XMLOperations">
            <endpoint address="" 
              binding="basicHttpBinding" 
              bindingConfiguration="basicHTTP" 
              contract="LoadXMLService.IxmlLoad" />
        </service>
    </services>

Please remember this service is supposed download & upload files to the Silerlight 3 project.

1
I've added clientaccesspolicy.xml to wwwroot of Inetpub. I'm not sure if that the ASP.NET dev server location? I get this error in Fiddler2 when it tries to access the file: [Fiddler] Connection to localhost failed.<BR>Exception Text: No connection could be made because the target machine actively refused it ::1:51547Tony The Lion
If it is using localhost:51547 then I think you are using the Visual Studio built-in web host. In your project, you have a silverlight project and a web project? You would want to put the cross domain file in your web project (i.e. at the same level as Web.Config).Timothy Lee Russell
I have now put it in there, but it still gives me the same error. Don't I need to fill in Domain Uri and Http request header? I'm not sure what though, can you give me a guide line?Tony The Lion
With the project running -- can you get to the service by going directly in the browser to localhost:51547/WCFService1/Service.svc?Timothy Lee Russell
Actually, no. It tells me the same thing. Connection actively refused by target machine. I've switched off my firewalls too now, but makes no differenceTony The Lion

1 Answers

2
votes

I would suggest adding the file "clientaccesspolicy.xml" to your website root with the following contents (at least as a troubleshooting step). The error that you are receiving seems to suggest this will help.

It is my understanding that Silverlight considers cross-port calls to be cross-domain (i.e. localhost:51547 and localhost:4000 are different domains).

<?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>

[Edit]

Well, the first thing to get it working is to be able to connect directly to the hosted service, so I'll throw out some ideas of issues I ran into.

Do you have this line in your service?

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
}

and this line in the Web.Config?

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
</system.serviceModel>