【问题标题】:System.ServiceModel.CommunicationException was unhandled by user code Message=An error occurred while trying to make a request to URI用户代码未处理 System.ServiceModel.CommunicationException 消息=尝试向 URI 发出请求时发生错误
【发布时间】:2014-12-05 14:59:07
【问题描述】:

在 Visual Studio 中调试 Silverlight 应用程序时,我收到此异常:

System.ServiceModel.CommunicationException was unhandled by user code
  Message=An error occurred while trying to make a request to URI 'http://localhost:8733/Design_Time_Addresses/WcfForecastService/Service1/'. 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.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at RollingActivityForecast.Services.ForecastServiceClient.ForecastServiceClientChannel.EndGetForecasts(IAsyncResult result)
   at RollingActivityForecast.Services.ForecastServiceClient.RollingActivityForecast.Services.IForecastService.EndGetForecasts(IAsyncResult result)
   at RollingActivityForecast.Services.ForecastServiceClient.OnEndGetForecasts(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__DisplayClassa.<EndGetResponse>b__9(Object sendState)
             at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
        InnerException: 

当我将应用程序部署到 inetpub 并从 IIS 管理器中浏览到它时,它可以正常工作,无一例外,因为我已将 clientaccesspolicy.xml 和 crossdomain.xml 文件与服务一起放置。

所以我的问题是,我应该将 clientaccesspolicy.xml/crossdomain.xml 文件放在我的 Visual Studio 解决方案中的什么位置,以便在调试时停止出现此异常?

我认为它应该放在 web.config 文件旁边的 MySilverlightApp.Web 项目中,但是当我这样做时仍然出现异常。我是否需要在 web.config 文件中添加一些额外的内容来引用 clientaccesspolicy.xml 文件?

仅供参考,我在与 MySilverlightApp.Web 项目不同的项目中拥有 WCF 服务。

TIA。

更新:

这里是文件:

clientaccesspolicy.xml:

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

crossdomain.xml:

<?xml version="1.0" encoding="utf-8" ?>
<!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="*"/>
</cross-domain-policy>

【问题讨论】:

    标签: wcf silverlight mef


    【解决方案1】:

    要将文件包含在本地开发中,只需将跨域文件添加到解决方案文件夹(Web 部件),然后将其 Copy To Output Directory 属性更改为 true

    【讨论】:

    • 感谢您的回复。是的,我忘了提到我已经将它设置为“始终复制”,但我仍然收到错误消息。这些文件是否应该与 WCF 服务位于同一目录中?
    【解决方案2】:

    通过更改解决了“跨域策略”异常,

    <add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfForecastService/Service1/" />
    

    到,

    <add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfForecastService/ForecastService/" />
    

    奇怪的是,为什么在 app.config 文件中有这个错误值的情况下,服务在 IIS 上完全可以工作?

    更新:

    或者,您可以摆脱单独的 WCF 服务项目并将 WCF 服务放入 MyApp.Web 项目中,这样您就根本不需要 clientaccesspolicy.xml 文件。

    【讨论】:

      猜你喜欢
      • 2020-07-10
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2020-05-06
      • 1970-01-01
      • 2012-01-20
      相关资源
      最近更新 更多