如何设置WCF服务内容类型

时间:2012-08-22 04:43:59

标签: c# .net wcf svcutil.exe

用[KnownType]解决班级传输问题后:Providing array or list of class objects via WCF

尝试使用svcutil生成协议时,我得到了这个:

Attempting to download metadata from 'http://localhost:8080/NEN_Server' using WS-Metadata Exchange or DIS
    CO.
    Microsoft (R) Service Model Metadata Tool
    [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.2152]
    Copyright (c) Microsoft Corporation. All rights reserved.
    
    Error: Cannot obtain Metadata from http://localhost:8080/NEN_Server
    
    If this is a Windows (R) Communication Foundation service to which you have access, please check that you
     have enabled metadata publishing at the specified address. For help enabling metadata publishing, pleas
    e refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.
    
    
    WS-Metadata Exchange Error
        URI: http://localhost:8080/NEN_Server
    
        Metadata contains insoluble link: "http://localhost:8080/NEN_Server".
    
        Content Type application / soap + xml; charset = utf-8 is not supported by http://localhost:8080/N
    EN_Server. This can be caused by mismatch of client and service bindings.
    
        The remote server returned an error: (415) Cannot process the message because the content type 'applica
    tion / soap + xml; charset = utf-8 'was not the expected type' text / xml; charset = utf-8 '..
    
    
    HTTP GET Error
        URI: http://localhost:8080/NEN_Server
    
        There was an error loading "http://localhost:8080/NEN_Server".
    
        The request failed with the error message:
    -
    <HTML> <HEAD> <STYLE Type="text/css"> # content {FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px} BOD
    Y {MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: # 000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white} P {MARG
    IN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: # 000000; FONT-FAMILY: Verdana} PRE {BORDER-RIGHT: # f0f0e0 1px soli
    d; PADDING-RIGHT: 5px; BORDER-TOP: # f0f0e0 1px solid; MARGIN-TOP:-5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2
    em; PADDING-BOTTOM: 5px; BORDER-LEFT: # f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: # f0f0e0 1px sol
    id; FONT-FAMILY: Courier New; BACKGROUND-COLOR: # e5e5cc}. heading1 {MARGIN-TOP: 0px; PADDING-LEFT: 15px; FO
    NT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT:-30px; WIDTH: 1
    00%; COLOR: # ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: # 003366}. Intro {MARGIN-LEFT
    :-15px} </ STYLE> <TITLE> Service </ TITLE> </ HEAD> <BODY> <DIV id="content"> <P class="heading1"> Service </ P> <BR/>
    <P Class="intro"> The server was unable to process the request due to an internal error. For more informa
    tion about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute
     or from the <serviceDebug> configuration behavior) on the server in order to send the exception informat
    ion back to the client, or turn on tracing as per the Microsoft. NET Framework SDK documentation and insp
    ect the server trace logs. </ P> </ DIV> </ BODY> </ HTML>

所以我想我必须将应用程序类型设置为

application / soap + xml; charset = utf-8

但我无法理解如何制作它。

这是我如何创建当前的WCF主机:

private void createHost() {
    /// Create the ServiceHost.
    using (ServiceHost host = new ServiceHost(typeof(NEN), baseAddress)) {
        /// Enable metadata publishing.
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        host.Description.Behaviors.Add(smb);

2 个答案:

答案 0 :(得分:2)

要发布元数据,您必须添加元数据交换端点

/// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(NEN), baseAddress)) {
    /// Enable metadata publishing.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);

// Add MEX endpoint
  host.AddServiceEndpoint(
  ServiceMetadataBehavior.MexContractName,
  MetadataExchangeBindings.CreateMexHttpBinding(),
  "mex");

答案 1 :(得分:1)

除了添加ServiceMetadataBehavior之外,还必须添加元数据交换端点。