我正在尝试从silverlight连接到现有的WCF服务(我已尝试过Silverlight 4和3,因此答案可能会很棒)通过Windows服务启动(尽管我只是从命令行运行它)暂时的项目)而不是IIS,我收到以下错误,看起来好像是clientaccesspolicy.xml相关:
{System.ServiceModel.CommunicationException: An error occurred while trying to make a request to URI 'http://localhost:8082/SecurityService'. 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. ---> System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
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)
--- End of inner exception stack trace ---
at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult 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 SilverlightApplication1.ServiceReference1.SecurityServiceClient.SecurityServiceClientChannel.EndGetVersionInformation(IAsyncResult result)
at SilverlightApplication1.ServiceReference1.SecurityServiceClient.SilverlightApplication1.ServiceReference1.SecurityService.EndGetVersionInformation(IAsyncResult result)
at SilverlightApplication1.ServiceReference1.SecurityServiceClient.OnEndGetVersionInformation(IAsyncResult result)
at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)}
我在服务项目中添加了另一项服务,因此我可以在查询服务的根目录时获取clientaccesspolicy.xml,并且可以在fiddler中看到此调用成功。
服务配置如下:
<behavior name="serviceMetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceThrottling maxConcurrentCalls="30" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
...
<endpointBehaviors>
<behavior name="serviceEndpointBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
<behavior name="webEndpointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
...
<service behaviorConfiguration="serviceMetadataBehavior"
name="PolicyService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8082" />
</baseAddresses>
</host>
<endpoint binding="webHttpBinding"
name="PolicyEndpoint"
bindingName=""
contract="IPolicyService"
behaviorConfiguration="webEndpointBehavior" />
</service>
...
<service behaviorConfiguration="serviceMetadataBehavior" name="SecurityService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8082/SecurityService" />
<add baseAddress="net.tcp://localhost:8083/SecurityService" />
</baseAddresses>
</host>
<endpoint binding="basicHttpBinding"
bindingConfiguration="basicHttpBindingConfiguration"
name="SecurityServiceEndpoint"
bindingName=""
contract="ISecurityService"
behaviorConfiguration="serviceEndpointBehavior" />
</service>
这些服务的接口是:
[ServiceContract]
public interface IPolicyService
{
[OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
Stream GetClientAccessPolicy();
[OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
Stream GetCrossDomainAccessPolicy();
}
[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.ExceptionShielding("WebService")]
[ServiceContract(Namespace = Namespaces.Services, Name = "SecurityService")]
public interface ISecurityService
{
[OperationContract]
[FaultContract(typeof(GeneralServiceFault))]
Custom.VersionInformation GetVersionInformation(string userName);
}
使用策略服务将clientaccesspolicy.xml作为流返回:
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
如果我需要提供更多信息,请告诉我。任何帮助非常感谢。
答案 0 :(得分:0)
您应该先进一步调查您的问题:
使用fiddler或Firebug等工具来跟踪客户端行为并检查它是否能够获取策略。然后检查它是否可以使用适当的语法获得良好的策略:
ClientAccessPolicy.xml和crossDomain.xml不共享相同的语法,然后使用其他语法获取一个将阻止您的客户端理解它。因此,您应该只提供其中一个。