用户代码未处理System.ServiceModel.CommunicationException消息=尝试向URI发出请求时发生错误

时间:2014-12-05 14:59:08

标签: wcf silverlight mef

在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文件?

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

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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

已解决&#34;跨域政策&#34;改变例外,

<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文件。