我尝试从SilverLight代码中运行WCF函数。WCF在网站上运行www.my-site.comSilverlight应用程序可从www.my-site.com下载 html页面的一部分,从网站www.vkontakte.ru下载。我收到一个a:ActionNotSupported错误。
怎么了?
这是我的silverlight应用程序发送的请求:
POST http://www.my-site.com/MyService.svc HTTP/1.1
接受:/
推荐人:http://www.my-site.com/ClientBin/MySlApp.xap
接收语言:ru-RU
内容长度:153
的content - type: text/xml;utf - 8字符集=
SOAPAction:"http://tempuri.org/IMyService/Add"
Accept-Encoding: gzip, deflate
用户代理:Mozilla/5.0(兼容;MSIE 9.0;Windows NT 6.1;三叉戟/5.0)
主持人:www.my-site.com
连接:维生
编译指示:no - cache
<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内部服务器错误
cache - control:私人
内容长度:710
内容类型:application/xml;utf - 8字符集=
服务器:microsoft iis/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP。净
日期:Sun, 2011年9月11日16:34:19 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服务代码:
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService: IMyService
{
public long Add(long a1, long a2)
{
return a1 + a2;
}
}
part of web.config:
<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>
crossdomain.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>
这不是跨域问题。问题是,您正在发送符合basicHttpBinding
的请求,而服务器端点使用webHttpBinding
。如果你改变了网络。配置到下面列出的,它应该工作。
<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访问。这是可以做到的(参见关于通过SL消费REST/POX服务的文章,以及关于通过SL消费REST/JSON服务的文章),但这不是一件太容易的事情(大多数人使用简单的类,如WebClient
或HttpWebRequest
来消费这些服务。