2009-08-25 71 views
0

*读者小心:大量代码转储,不适合心脏... *CrossDomainError在IIS7的Silverlight 3 WCF应用

你好微弱,

我试图找出如何部署带有WCF服务的IIS7 Silverlight 3应用程序。我想我已经找到了大部分,但是由于某种原因,我仍然遇到了跨域错误。我倾向于认为该服务没有找到客户端访问策略,但我不知道如何确认。我得到一个非常无用的例外(简单地说,CrossDomainError)。内部异常是不存在的。以下是我部署应用的步骤。如果有人看到任何不能加起来的东西,他们可以提供建议吗?我想不出别的在现在捅......

  • 在IIS管理器中我建立了一个新的网站。我将它命名为Silverlight,接受了同名的游戏池,并接受了其余的默认设置。我禁用了默认网站。我将Web根设置为C:\ WebApps
  • 我将Visual Studio Silverlight客户端项目的发布版本中的所有文件放入Web根目录中。
  • 我将Silverlight客户端项目中的以下配置文件放在Web根目录中:ServiceReferences.ClientConfig,Silverlight.js。
  • 我放在/ bin目录从Silverlight.Web项目的发布版本到网站根
  • 我把从Silverlight.Web发布版本的以下文件在网站根目录:crossdomain.xml的,clientaccesspolicy.xml ,Service1.svc,Service1.svc.cs,Web.config。
  • 我将TestPage.html文件重命名为index.html。

我意识到很多这些都是多余的,但我没有办法尝试,所以我开始添加任何看起来可能包含任何有用元数据的东西。

下面是代码,以我的各种配置文件:

clientaccesspolicy.xml:

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

的crossdomain.xml:

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

system.serviceModel配置,节录来自Web.config:

<system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="SilverlightApplication2.Web.Service1Behavior"> 
        <serviceMetadata httpGetEnabled="true"/> 
        <serviceDebug includeExceptionDetailInFaults="false"/> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <bindings> 
      <customBinding> 
       <binding name="customBinding0"> 
        <binaryMessageEncoding/> 
        <httpTransport/> 
       </binding> 
      </customBinding> 
     </bindings> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
     <services> 
      <service behaviorConfiguration="SilverlightApplication2.Web.Service1Behavior" name="SilverlightApplication2.Web.Service1"> 
       <endpoint address="http://win-xqawq222tag:2721/Service1.svc" binding="customBinding" bindingConfiguration="customBinding0" contract="SilverlightApplication2.Web.Service1"/> 
       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
      </service> 
     </services> 
    </system.serviceModel> 

ServiceReferences.ClientConfig:

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <customBinding> 
       <binding name="CustomBinding_Service1"> 
        <binaryMessageEncoding /> 
        <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> 
       </binding> 
      </customBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://win-xqawq222tag:2721/Service1.svc" binding="customBinding" 
       bindingConfiguration="CustomBinding_Service1" contract="ServiceReference1.Service1" 
       name="CustomBinding_Service1" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

Service1.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="SilverlightApplication2.Web.Service1" CodeBehind="Service1.svc.cs" %> 

现在的执行,然后客户端代码:

Service1.svc 。CS:

using System; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.Collections.Generic; 
using System.Text; 

namespace SilverlightApplication2.Web 
{ 
    [ServiceContract(Namespace = "")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class Service1 
    { 
     [OperationContract] 
     public DoWorkResult DoWork() 
     { 
      // Add your operation implementation here 
      int i = new Random().Next(); 
      string s = "test string"; 
      DoWorkResult r = new DoWorkResult() { String = s, Integer = i }; 
      return r; 
     } 

     // Add more operations here and mark them with [OperationContract] 
    } 

    [DataContract] 
    public class DoWorkResult 
    { 
     [DataMember] 
     public string String { get; set; } 

     [DataMember] 
     public int Integer { get; set; } 
    } 
} 

MainPage.xaml.cs中:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightApplication2 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 

      ServiceReference1.Service1Client proxy = new SilverlightApplication2.ServiceReference1.Service1Client(); 
      proxy.DoWorkCompleted += new EventHandler<SilverlightApplication2.ServiceReference1.DoWorkCompletedEventArgs>(proxy_DoWorkCompleted); 
      proxy.OpenAsync(); 
      proxy.DoWorkAsync(); 
      proxy.CloseAsync(); 
     } 

     void proxy_DoWorkCompleted(object sender, SilverlightApplication2.ServiceReference1.DoWorkCompletedEventArgs e) 
     { 
      if (e.Error == null) 
      { 
       String.Text = "Test String is: " + e.Result.String; 
       Integer.Text = "Random Int is: " + e.Result.Integer; 
      } 
      else 
      { 
       String.Text = e.Error.Message + e.Error.InnerException.Message + e.Error.StackTrace; 
       Integer.Text = e.Error.Message + e.Error.InnerException.Message + e.Error.StackTrace; 
      } 
     } 
    } 
} 

所有这一切都在2008年VS上XP专业版能正常工作。在Server2008上的IIS7上,我可以在http://localhosthttp://[myComputerName]上点击默认页面index.html。

我也能够打http://localhost/Service1.svc服务。我无法使用http://[compNameHere]/Service1.svc进行服务。它抱怨此错误:

No protocol binding matches the given address ' http://win-xqawq222tag:2721/Service1.svc '. Protocol bindings are configured at the Site level in IIS or WAS configuration. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: No protocol binding matches the given address ' http://win-xqawq222tag:2721/Service1.svc '. Protocol bindings are configured at the Site level in IIS or WAS configuration.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: No protocol binding matches the given address ' http://win-xqawq222tag:2721/Service1.svc '. Protocol bindings are configured at the Site level in IIS or WAS configuration.]
System.ServiceModel.Channels.TransportChannelListener.OnOpening() +11513378 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +229
System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +72

[InvalidOperationException: The ChannelDispatcher at ' http://win-xqawq222tag:2721/Service1.svc ' with contract(s) '"Service1"' is unable to open its IChannelListener.] System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +118
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +261
System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +107
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +261
System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +121
System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +479

[ServiceActivationException: The service '/Service1.svc' cannot be activated due to an exception during compilation. The exception message is: The ChannelDispatcher at ' http://win-xqawq222tag:2721/Service1.svc ' with contract(s) '"Service1"' is unable to open its IChannelListener..] System.ServiceModel.AsyncResult.End(IAsyncResult result) +11531006
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +194
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext) +176
System.ServiceModel.Activation.HttpHandler.ProcessRequest(HttpContext context) +23
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

No protocol binding matches the given address ' http://win-xqawq222tag:2721/Service1.svc '. Protocol bindings are configured at the Site level in IIS or WAS configuration.

而且,我可以打政策文件在http://localhost/clientaccesspolicy.xmlhttp://[computerNameHere]/clientaccesspolicy.xml

还有什么我需要看看?

+0

更新:删除端口:CONFIGS 2721让我打从HTTP服务:// /Service1.svc,但是我仍然无法从Silverlight应用程序中获得服务。 – 2009-08-25 17:48:35

回答

0

我不得不做出以下更改ServiceReferences.ClientConfig文件,然后重新发布:

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <customBinding> 
       <binding name="CustomBinding_Service1"> 
        <binaryMessageEncoding /> 
        <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> 
       </binding> 
      </customBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost/Service1.svc" binding="customBinding" 
       bindingConfiguration="CustomBinding_Service1" contract="ServiceReference1.Service1" 
       name="CustomBinding_Service1" /> 
      <!--<endpoint address="http://localhost:2721/Service1.svc" binding="customBinding" 
       bindingConfiguration="CustomBinding_Service1" contract="ServiceReference1.Service1" 
       name="CustomBinding_Service1" />--> 
     </client> 
    </system.serviceModel> 
</configuration> 
1

我毫不犹豫地承认,我并没有竭尽全力地重现你的具体错误,但我注意到了一些让我感到困惑的事情。

Colin Cole在博客中发现,有关使用SSL的cap.xml格式发生了微妙的变化。 我没有使用SSL,但只有当我改变

<allow-from http-request-headers="*"> 
    <domain uri="*"/> 
</allow-from> 

<allow-from http-request-headers="*"> 
    <domain uri="http://*" /> 
    <domain uri="https://*" /> <!-- if needed --> 
</allow-from> 

访问该服务的工作。