【问题标题】:MessageContract-based WCF service in MVC app won't serve up metadataMVC 应用程序中基于 MessageContract 的 WCF 服务不会提供元数据
【发布时间】:2011-09-15 05:46:35
【问题描述】:

我一直在阅读有关在 MVC 应用程序中托管 WCF 服务的所有问题,但遗憾的是,我没有找到有关指定 MessageContracts 的服务的太多信息。我需要进行流文件传输,所以如果我想接受有关流的元数据,我必须在消息合同中指定标头。

我已将此服务添加到我的 MVC 应用程序并添加了 ServiceRoute;浏览器无法按预期获取 WSDL,Visual Studio 无法获取生成客户端代理类所需的服务元数据。两者都收到如下错误:

Operation 'ImportMessageBody' in contract 'IImport' uses a MessageContract that has 
SOAP headers. SOAP headers are not supported by the None MessageVersion.

相关的堆栈跟踪:

[InvalidOperationException: Operation 'ImportMessageBody' in contract 'IImport' uses a MessageContract that has SOAP headers. SOAP headers are not supported by the None MessageVersion.]
   System.ServiceModel.Description.WebHttpBehavior.ValidateNoMessageContractHeaders(MessageDescription md, String opName, String contractName) +704271
   System.ServiceModel.Description.WebHttpBehavior.ValidateContract(ServiceEndpoint endpoint) +134
   System.ServiceModel.Description.WebHttpBehavior.Validate(ServiceEndpoint endpoint) +51
   System.ServiceModel.Description.ServiceEndpoint.Validate(Boolean runOperationValidators, Boolean isForService) +287
   System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost) +271
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +109
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +60
   System.ServiceModel.ServiceHostBase.OnBeginOpen() +27
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +50
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
   System.ServiceModel.Channels.CommunicationObject.Open() +36
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +184
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +615

[ServiceActivationException: The service '/a/import' cannot be activated due to an exception during compilation.  The exception message is: Operation 'ImportMessageBody' in contract 'IImport' uses a MessageContract that has SOAP headers. SOAP headers are not supported by the None MessageVersion..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +679246
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190
   System.ServiceModel.Activation.AspNetRouteServiceHttpHandler.EndProcessRequest(IAsyncResult result) +6
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +96

WcfTestClient 也有同样的问题(如我所料)。

我需要做什么才能启用客户端代理生成?

这是我的 MVC 应用程序的 web.config 文件的 serviceModel 部分:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

还有我添加的路线:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
            routes.IgnoreRoute("favicon.ico");

            routes.Add(new ServiceRoute("a/import", new WebServiceHostFactory(), typeof(Services.Import.Import)));
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

【问题讨论】:

    标签: asp.net-mvc wcf asp.net-mvc-3 wcf-binding wcf-client


    【解决方案1】:

    您正在通过 WebServiceHostFactory 托管服务,这意味着您正在创建一个 REST 端点。

    MessageContract 的目的是定义 SOAP 消息(标头和正文),因此使用 WebServiceHostFactory 创建的服务不支持它们

    您的意思是使用 SOAP 端点而不是 REST 端点吗?

    【讨论】:

    • 不,我想我只是没有想到 REST 端点不支持 SOAP 消息传递。
    • REST 是一种不同于基于 SOAP 的服务的架构风格,它使用 Web 风格范例而不是自我描述消息。 WCF 是一个通信基础,因此可以做的不仅仅是 SOAP
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多