2011-09-12 67 views
1

我試圖從SilverLight代碼運行WCF函數。 WCF正在網站上運行www.my-site.com Silverlight應用程序從網站www.my-site.com下載爲< IFrame />從網站www.vkontakte.ru下載的html頁面的一部分。我收到了一個a:ActionNotSupported錯誤。
有什麼不對?跨域WCF - Silverlight配置錯誤a:ActionNotSupported

這裏的請求是由我的Silverlight應用程序發送:

POST http://www.my-site.com/MyService.svc HTTP/1.1
接受:/
的Referer:http://www.my-site.com/ClientBin/MySlApp.xap
接受語言:RU-RU
的Content-Length :153
Content-Type:text/xml;字符集= UTF-8
的SOAPAction: 「http://tempuri.org/IMyService/Add」
接受編碼:gzip,放氣
的User-Agent:Mozilla的/ 5.0(兼容; MSIE 9.0; Windows NT的6.1;三叉戟/ 5.0)
主機:www.my-site.com
連接:保持活動
附註:無緩存

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
    <Add xmlns="http://tempuri.org/"> 
     <a1>1</a1> 
     <a2>1</a2> 
    </Add> 
    </s:Body> 
</s:Envelope> 

作爲結果我收到如下因素響應:

HTTP /1.1 500內部服務器錯誤
緩存控制:私人
的Content-Length:710
內容類型:應用/ XML;字符集= UTF-8
服務器:Microsoft-IIS/7.5
X-ASPNET-版本:4.0.30319
X供電,通過:ASP.NET
日期:孫老師,2011年9月11日16點34分十九秒GMT

<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"> 
    <Code> 
    <Value>Sender</Value> 
    <Subcode> 
     <Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none"> 
     a:ActionNotSupported 
     </Value> 
    </Subcode> 
    </Code> 
    <Reason> 
    <Text xml:lang="en-US"> 
     The message with Action '' cannot be processed at the receiver, 
     due to a ContractFilter mismatch at the EndpointDispatcher. 
     This may be because of either a contract mismatch 
     (mismatched Actions between sender and receiver) 
     or a binding/security mismatch between the sender and the receiver. 
     Check that sender and receiver have the same contract and the same binding 
     (including security requirements, e.g. Message, Transport, None). 
    </Text> 
    </Reason> 
</Fault> 

Silverlight的請求的代碼:

 BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); 
     EndpointAddress endpointAddress = 
      new EndpointAddress("http://www.my-site.com/MyService.svc"); 
     IDbService service = 
      new ChannelFactory<IDbService>(basicHttpBinding, endpointAddress) 
       .CreateChannel(); 
     AsyncCallback asy = delegate(IAsyncResult result) 
           { 
            Trace.WriteLine(service.EndAdd(result)); 
           }; 
     service.BeginAdd(1, 1, asy, null); 

Silverlight的操作合同接口:

[ServiceContract] 
public interface IMyService 
{ 
    [OperationContract(AsyncPattern = true)] 
    IAsyncResult BeginAdd(long a1, long a2, AsyncCallback callback, object state); 

    long EndAdd(IAsyncResult result); 
} 

WCF操作合同接口:

[ServiceContract] 
public interface IMyService 
{ 
    [OperationContract] 
    long Add(long a1, long a2); 
} 

WCF服務代碼:web.config中的

[AspNetCompatibilityRequirements(
    RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyService: IMyService 
{ 
    public long Add(long a1, long a2) 
    { 
     return a1 + a2; 
    } 
} 

部分:

<system.serviceModel> 
    <behaviors> 
    <serviceBehaviors> 
     <behavior name="MySlApp.Web.ServiceMyServiceAspNetAjaxBehavior"> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
     <behavior name="MySlApp.Web.MyServiceAspNetAjaxBehavior"> 
     <enableWebScript /> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    <services> 
    <service name="MySlApp.Web.DbService" 
      behaviorConfiguration="MySlApp.Web.ServiceMyServiceAspNetAjaxBehavior"> 
     <endpoint address="" 
      binding="webHttpBinding" contract="MySlApp.Web.IMyService" /> 
    </service> 
    </services> 
</system.serviceModel> 

clientaccesspolicy.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<access-policy> 
    <cross-domain-access> 
    <policy> 
     <allow-from http-request-headers="*"> 
     <domain uri="http://www.my-site.com" /> 
     <domain uri="http://www.vkontakte.ru" /> 
     <domain uri="http://*.vkontakte.ru" /> 
     <domain uri="http://www.vk.com" /> 
     <domain uri="http://*.vk.com" /> 
     </allow-from> 
     <grant-to> 
     <resource path="/" include-subpaths="true"/> 
     </grant-to> 
    </policy> 
    </cross-domain-access> 
</access-policy> 

跨域。xml:

<?xml version="1.0" encoding="utf-8" ?> 
<cross-domain-policy> 
    <allow-access-from domain="www.my-site.com" /> 
    <allow-access-from domain="www.vkontakte.ru" /> 
    <allow-access-from domain="*.vkontakte.ru" /> 
    <allow-access-from domain="www.vk.com" /> 
    <allow-access-from domain="*.vk.com" /> 
</cross-domain-policy> 
+0

你創建它作爲一個Silverlight兼容的WCF服務或普通的舊WCF服務? –

回答

1

這不是一個跨域問題。問題是您發送的請求符合basicHttpBinding,而服務器端點使用webHttpBinding。如果將web.config更改爲下面列出的那個,它應該可以工作。

<system.serviceModel> 
    <behaviors> 
    <serviceBehaviors> 
     <behavior name="MySlApp.Web.ServiceMyServiceAspNetAjaxBehavior"> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
    </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    <services> 
    <service name="MySlApp.Web.DbService" 
      behaviorConfiguration="MySlApp.Web.ServiceMyServiceAspNetAjaxBehavior"> 
     <endpoint address="" 
      binding="basicHttpBinding" contract="MySlApp.Web.IMyService" /> 
    </service> 
    </services> 
</system.serviceModel> 

如果你不能改變你的服務,那麼你需要改變客戶端。 webHttpBinding端點也被稱爲WCF WebHttp端點,或者(以某種方式過度使用'REST'術語)WCF REST端點,不能通過使用WCF編程模型(客戶端類,ChannelFactory<T>等)的SL直接訪問。這是可以做到(詳見後約consuming REST/POX services via SL,約consuming REST/JSON services via SL後),但它不是太容易了(大多數人使用的簡單類,如WebClientHttpWebRequest來使用這些服務。