【问题标题】:How to setup WCF service content type如何设置 WCF 服务内容类型
【发布时间】:2012-08-17 11:32:20
【问题描述】:

在使用 [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);

【问题讨论】:

    标签: c# .net wcf svcutil.exe


    【解决方案1】:

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

    /// 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");
    

    【讨论】:

    • 我不是已经在这里添加了吗host.Description.Behaviors.Add(smb);
    • 在不添加 smb 的情况下显示在服务 NEN 实施的合同列表中找不到合同名称“IMetadataExchange”。将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost 以启用对此合同的支持。
    • @МарияШарапова:您混淆了行为端点。行为告诉 WCF 基础结构,该特定服务主机能够发布其元数据。端点告诉,元数据将在哪里发布。
    • 我明白了,“mex”是真的有效地址吗?到目前为止它还没有解决问题:(
    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 2019-04-26
    • 2011-06-16
    • 2021-10-21
    • 2018-05-22
    • 2016-09-28
    • 2016-06-15
    • 1970-01-01
    • 2011-08-28
    相关资源
    最近更新 更多