You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CoreWCF中如何将http://tempuri.org/替换为自定义命名空间?

解决CoreWCF SOAP响应命名空间默认使用http://tempuri.org/的问题

问题根源

你当前的代码仅在接口上添加了[XmlRoot],但没有给[ServiceContract]指定命名空间,同时返回的HelloWorldResponse等DTO类也未配置对应的命名空间属性,导致CoreWCF默认使用http://tempuri.org/作为响应消息的命名空间。

修复步骤

  1. 给ServiceContract指定命名空间
    在[ServiceContract]特性中添加Namespace参数,替换默认的临时命名空间:

    [ServiceContract(Name = "UploaderAPI", Namespace = "http://x-rm.com")]
    [XmlSerializerFormat]
    public interface IUploaderAPISoap
    {
        [OperationContract(Action = "http://x-rm.com/uploaderapi/HelloWorld", 
                           ReplyAction = "http://x-rm.com/uploaderapi/HelloWorldResponse")]
        HelloWorldResponse HelloWorld(string echo);
    }
    
  2. 给响应DTO类配置命名空间
    针对返回的HelloWorldResponse和HelloWorldResult类,添加[XmlRoot]或[XmlElement]特性指定命名空间(因为你用了XmlSerializerFormat,所以使用Xml序列化相关特性):

    [XmlRoot(Namespace = "http://x-rm.com")]
    public class HelloWorldResponse
    {
        [XmlElement(Namespace = "http://x-rm.com")]
        public HelloWorldResult HelloWorldResult { get; set; }
    }
    
    [XmlRoot(Namespace = "http://x-rm.com")]
    public class HelloWorldResult
    {
        [XmlElement(Namespace = "http://x-rm.com")]
        public string HelloWorldResult { get; set; }
    }
    

验证效果

修改后,SOAP响应中的HelloWorldResponse节点会使用你指定的http://x-rm.com命名空间,输出示例如下:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <HelloWorldResponse xmlns="http://x-rm.com">
            <HelloWorldResult>
                <HelloWorldResult xmlns="http://x-rm.com">Hi there, foo</HelloWorldResult>
            </HelloWorldResult>
        </HelloWorldResponse>
    </s:Body>
</s:Envelope>

内容的提问来源于stack exchange,提问作者NickG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 04:41:10