在运行时创建 WCF 服务时出现 ActionNotSupported 错误
Posted
技术标签:
【中文标题】在运行时创建 WCF 服务时出现 ActionNotSupported 错误【英文标题】:ActionNotSupported error when a WCF Service is created at runtime 【发布时间】:2012-11-24 10:21:49 【问题描述】:我正在尝试在运行时创建WCF
服务。我的服务接口是:
[ServiceContract]
public interface IInformationService : IService
[OperationContract]
[WebInvoke(Method = "Get", ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Test", RequestFormat = WebMessageFormat.Json)]
string Test();
我的服务如下:
var httpEnumerator = ImplementedContracts.Values.GetEnumerator();
httpEnumerator.MoveNext();
var httpContractType = httpEnumerator.Current.ContractType;
var webBinding = new WebHttpBinding()
Security =
Mode = WebHttpSecurityMode.None
;
var httpEndpoint = AddServiceEndpoint(
httpContractType,
webBinding, baseAddress+/Get"
);
httpEndpoint.Behaviors.Add(new CustomEndpointBehavior());
ServiceHost是通过这个方法创建的:
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
var host = new WcfServiceHost(serviceType, baseAddresses);
if (host.Description.Behaviors.Contains(typeof(ServiceDebugBehavior)))
(host.Description.Behaviors[typeof(ServiceDebugBehavior)] as
ServiceDebugBehavior).IncludeExceptionDetailInFaults = true;
else
var debug = new ServiceDebugBehavior
IncludeExceptionDetailInFaults = true
;
host.Description.Behaviors.Add(debug);
if (host.Description.Behaviors.Contains(typeof(ServiceMetadataBehavior)))
(host.Description.Behaviors[typeof(ServiceMetadataBehavior)] as ServiceMetadataBehavior).HttpGetEnabled = true;
(host.Description.Behaviors[typeof(ServiceMetadataBehavior)] as ServiceMetadataBehavior).HttpsGetEnabled = true;
else
var smb = new ServiceMetadataBehavior
HttpGetEnabled = true,
HttpsGetEnabled = true
;
host.Description.Behaviors.Add(smb);
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpsBinding(),
"mex"
);
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex"
);
return host;
服务路由创建:
var serviceRoute = new ServiceRoute(
"wcf.service/" + service.Value.Name,
new WcfServiceHostFactory(),
service.Value
);
if (!RouteTable.Routes.Contains(serviceRoute))
RouteTable.Routes.Add(serviceRoute);
当我尝试使用该地址从网络浏览器访问我的服务时
http://localhost/Werp.View/wcf.service/InformationService/Get/Test
我收到以下错误:
<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>
谁能帮帮我?
【问题讨论】:
【参考方案1】:如果您不需要特定的 WCF 功能或者您有使用 WCF 的授权,您应该考虑为基于 REST 的服务使用不同的堆栈。例如ASP.NET web API 或ServiceStack。做一个简单的 REST 调用似乎需要做很多工作。
如果您打开服务诊断,这可能有助于诊断问题。你可以看到这个SO for detailed instructions
您也可以参考此SO: WCF - ContractFilter mismatch at the EndpointDispatcher exception 了解一些想法。
【讨论】:
【参考方案2】:当我将 WebHttpBehavior 添加到端点时,我的问题已经解决了
httpEndpoint.Behaviors.Add(new WebHttpBehavior());
【讨论】:
【参考方案3】:在我的例子中,我需要向名为 SOAPAction
的请求添加一个 HTTP 标头,其中的值设置为我请求的服务的 xmlns 路径。
【讨论】:
以上是关于在运行时创建 WCF 服务时出现 ActionNotSupported 错误的主要内容,如果未能解决你的问题,请参考以下文章
在部分受信任的 AppDomain 中调用 WCF 操作时出现“请求失败”
尝试使用 XHR 联系 WCF 服务时出现 400 错误请求
使用 Azure 应用服务中托管的 WCF 服务时出现凭据错误
使用 wcf 对 sql server 运行查询时出现奇怪的错误