我一直在阅读关于在MVC应用程序中托管WCF服务的所有问题,很遗憾,我没有找到有关指定MessageContract
的服务的更多信息。我需要进行流式文件传输,所以如果我想接受有关流的元数据,我必须在消息合同中指定标头。
我已将此服务添加到我的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
);
}
答案 0 :(得分:1)
您通过WebServiceHostFactory托管服务,这意味着您正在创建REST端点。
MessageContract的目的是定义SOAP消息(标题和正文),因此使用WebServiceHostFactory创建的服务不支持它们
您的意思是使用SOAP端点而不是REST端点吗?