1

(已解决)[提醒] 遇到同样问题的朋友:如果您将clientaccesspolicy.xml和crossdomain.xml放在正确的文件夹中时收到相同的错误消息,请检查您是否接受了自签名服务端的证书。

接受证书:在浏览器中输入服务的url,浏览器会询问你是否信任它。还有一个安装证书的选项,并且不会从该站点收到有关证书的更多警告。

[原始问题] 我的 silverlight 应用程序无法连接到 wcf 服务。抛出异常。

System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid.  Check InnerException for exception details. ---> System.ServiceModel.CommunicationException: An error occurred while trying to make a request to URI 'http://fyp-pc13:97/HelloWorldService/HelloWorld.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. ---> System.Security.SecurityException ---> System.Security.SecurityException: Security error.
   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(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.HelloServiceClient.HelloServiceClientChannel.EndHello(IAsyncResult result)
   at SilverlightApplication1.ServiceReference1.HelloServiceClient.SilverlightApplication1.ServiceReference1.IHelloService.EndHello(IAsyncResult result)
   at SilverlightApplication1.ServiceReference1.HelloServiceClient.OnEndHello(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
   --- End of inner exception stack trace ---
   at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
   at SilverlightApplication1.ServiceReference1.HelloCompletedEventArgs.get_Result()
   at SilverlightApplication1.MainPage.ialert(Object sender, HelloCompletedEventArgs msg)

该服务在控制台应用程序中运行良好。所以我想这是我的 silverlight 应用程序配置的问题。

网页配置

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>

      <basicHttpBinding>
        <binding name="BasicHttpBinding_IHelloService" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None" />
        </binding>
      </basicHttpBinding>

    </bindings>

    <services>
      <service name="WcfService1.HelloWorld">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHelloService" contract="WcfService1.IHelloService" name="BasicHttpBinding_IHelloService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

ServiceReferences.ClientConfig:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IHelloService" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://fyp-pc13:97/HelloWorldService/HelloWorld.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHelloService"
                contract="ServiceReference1.IHelloService" name="BasicHttpBinding_IHelloService" />
        </client>
    </system.serviceModel>
</configuration>

有人可以帮我解决这个问题吗?我一直坚持配置文件好几天了。非常感谢。

4

1 回答 1

0

这可能是由于 Silverlight 中的安全原因,您的异常文本提供了这个方向的线索:“ [...]可能是由于在没有适当的跨域策略的情况下尝试以跨域方式访问服务,[...]

clientaccesspolicy.xml文件放置在托管服务的域的根目录中,以将服务配置为允许跨域访问。

如果您使用的是 IIS,这可能是\inetpub\wwwroot\文件夹,但这可能因您的配置而异。

这是一个纯文本 XML 文件,包含以下几行(可能会根据您的需要进行调整):

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="SOAPAction">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

此处的更多信息:使服务跨域边界可用

[编辑]

如果这不起作用,还请尝试将crossdomain.xml文件放入 Web 服务。

将有效的crossdomain.xml文件放在托管服务的域的根目录中。该文件必须将整个域标记为公共。

这包含以下几行:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
</cross-domain-policy>
于 2012-09-28T08:14:05.417 回答