问题
I want to know how I can consume a normal WCF service (not Silverlight enabled WCF service) in my Silverlight application. Here is what I tried
- I have created a simple normal WCF service
- I changed the binding to
basicHttpBinding
in theweb.config
file - I have placed
clientaccesspolicy.xml
andcrossdomain.xml
files inwwwroot
folder.
But even though I am unable to consume the WCF service. I get this exception
System.ServiceModel.CommunicationException was unhandled by user code
Message=An error occurred while trying to make a request to URI 'http://localhost/HWT/HelloWorldService.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.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.ClientBase1.ChannelBase
1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
at ServiceRef.HelloWorldServiceClient.HelloWorldServiceClientChannel.EndGetName(IAsyncResult result)
at ServiceRef.HelloWorldServiceClient.TestSilClient.ServiceRef.IHelloWorldService.EndGetName(IAsyncResult result)
at TestSilClient.ServiceRef.HelloWorldServiceClient.OnEndGetName(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_DisplayClass4.b_1(Object sendState)
Can anyone explain what is the best way to consume normal WCF service in a Silverlight app? An example will help me.
Thank you.
回答1:
Possible reasons of your problem to my knowledge:
- wrongly constructed policy files
- wrongly placed policy files (should be placed in the root folder of the web application hosting your web sevices not in the services folder if you have one)
You can check if your SL application requests any of the policy files by using ie Fiddler. If you see that a policy file is downloaded but the app is not working point 1 is probably the reason. If no files are requested you've probably misplaced them (as in point 2).
There is a possibility to not use the policy files at all. If your web services are hosted in the same web application as the Silverlight application you can pass a relative path to WS in the endpoint configuration of the client (.ClientConfig) and the cross domain/policy files won't be required. For example, instead of using address="http://localhost/Services/ExampleService.svc"
use sth like address="../Services/ExampleService.svc"
(depending on the folder in which the XAP file is located). For more info check out http://msdn.microsoft.com/en-us/library/cc197941%28v=vs.95%29.aspx.
回答2:
Try to loosen up your crossdomain file (just for the sake of test) to verify if it's the issue. Use this code in your crossdomain.xml file and see if it works:
<?xml version="1.0" ?>
<cross-domain-policy>
<allow-access-from domain="*"/>
<site-control permitted-cross-domain-policies="all"/>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
来源:https://stackoverflow.com/questions/8687492/how-to-consume-wcf-in-silverlight-app