无法连接到silverlight应用程序中的wcf服务

无法连接到silverlight应用程序中的wcf服务,wcf,silverlight,Wcf,Silverlight,(已解决)[提醒] 对于遇到相同问题的人: 如果在将clientaccesspolicy.xml和crossdomain.xml放在正确的文件夹中时收到相同的错误消息,请检查您是否已接受服务端的自签名证书 接受证书:在浏览器中输入服务的url,浏览器将询问您是否信任该服务。还有一个安装证书的选项,该站点不会再收到有关证书的警告 [原始问题] 我的silverlight应用程序无法连接到wcf服务。异常被抛出 System.Reflection.TargetInvocationException:

(已解决)[提醒] 对于遇到相同问题的人: 如果在将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)
System.Reflection.targetInvestInvocationException:操作期间发生异常,导致结果无效。检查InnerException以了解异常详细信息。-->System.ServiceModel.CommunicationException:尝试向URI的请求时出错http://fyp-pc13:97/HelloWorldService/HelloWorld.svc'. 这可能是由于在没有适当的跨域策略或不适合SOAP服务的策略的情况下,试图以跨域方式访问服务。您可能需要联系服务的所有者以发布跨域策略文件,并确保它允许发送与SOAP相关的HTTP头。此错误也可能是由于在web服务代理中使用内部类型而未使用InternalsVisibleToAttribute属性造成的。有关详细信息,请参阅内部异常。-->System.Security.SecurityException-->System.Security.SecurityException:安全错误。
位于System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
在System.Net.Browser.BrowserHttpWebRequest.c__DisplayClassa.b__9(对象发送状态)中
在System.Net.Browser.AsyncHelper.c__DisplayClass4.b__1(对象发送状态)中
---内部异常堆栈跟踪的结束---
位于System.Net.Browser.AsyncHelper.BeginNoui(SendOrPostCallback beginMethod,对象状态)
位于System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
位于System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteTresponse(IAsyncResult结果)
---内部异常堆栈跟踪的结束---
在System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult结果)处
位于System.ServiceModel.Channels.ServiceChannel.EndCall(字符串操作,对象[]输出,IAsyncResult结果)
位于System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName,Object[]args,IAsyncResult结果)
在SilverlightApplication1.ServiceReference1.HelloServiceClient.HelloServiceClientChannel.EndHello(IAsyncResult结果)
位于SilverlightApplication1.ServiceReference1.HelloServiceClient.SilverlightApplication1.ServiceReference1.IHelloService.EndHello(IAsyncResult结果)
在SilverlightApplication1.ServiceReference1.HelloServiceClient.OnEndHello(IAsyncResult结果)
在System.ServiceModel.ClientBase`1.OnAsyncCallCompleted处(IAsyncResult结果)
---内部异常堆栈跟踪的结束---
位于System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfEssential()处
在SilverlightApplication1.ServiceReference1.HelloCompletedEventArgs.get_Result()中
在SilverlightApplication1.MainPage.ialert(对象发送方,HelloCompletedEventArgs消息)
这项服务在控制台应用程序中运行良好。所以我想这是我的silverlight应用程序配置的问题

Web.config

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


有人能帮我解决这个问题吗?我已经在配置文件中呆了好几天了。非常感谢。

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

clientaccesspolicy.xml文件放在域的根目录下,其中 托管该服务是为了将该服务配置为允许跨域 进入

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

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


[编辑]

如果这不起作用,还可以尝试将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>


请将配置更改为此服务调试includeExceptionDetailInFaults=“true”。您将收到一条更好的错误消息更新了错误日志,请看一看我的服务根目录中有这两个接受任何请求头的文件,但它仍然给我相同的错误。@Alvin:我更新了我的答案,以便更好地指定文件夹。链接的文章能帮你吗?很抱歉回复晚了。但是,在将这些策略放入crossdomain.xml和clientaccesspolicy.xml之后,仍然会收到相同的错误消息谢谢。我仍然收到该消息的原因是我通过https访问该服务,而我没有接受来自该服务的证书。Silverlight不会给出关于证书的明确异常,但与我上面引用的相同异常。
<?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>