在运行时创建WCF服务时出现ActionNotSupported错误

时间:2012-12-06 11:34:46

标签: c# wcf wcf-binding

我正在尝试在运行时创建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>

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:4)

如果您不需要特定的WCF功能或者您有权使用WCF,则应考虑为基于REST的服务使用不同的堆栈。例如ASP.NET web APIServiceStack。做一个简单的REST调用看起来很多工作。

如果启用服务诊断,这可能有助于诊断问题。您可以看到此SO for detailed instructions

您也可以参考此SO: WCF - ContractFilter mismatch at the EndpointDispatcher exception了解一些想法。

答案 1 :(得分:4)

当我将WebHttpBehavior添加到端点

时,我的问题已经解决了
httpEndpoint.Behaviors.Add(new WebHttpBehavior());

答案 2 :(得分:0)

就我而言,我需要向名为SOAPAction的请求中添加HTTP标头,其中该值设置为我所请求的服务的xmlns路径。