将名称空间添加到元素并序列化而不使用别名

问题描述 投票:0回答:1

我想将命名空间设置为包装器。但是不是用别名序列化它,而是需要它在包装元素本身上。

我得到的是以下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:my-namepsace.com/asdf">
   <soapenv:Header/>
   <soapenv:Body>
     <urn:YVDMS_TASK>
        ...
     </urn:YVDMS_TASK>
   </soapenv:Body>
<soapenv:Envelope>

但我希望它像这样:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
     <YVDMS_TASK xmlns="urn:my-namepsace.com/asdf">
        ...
     </YVDMS_TASK>
   </soapenv:Body>
<soapenv:Envelope>

这可能吗?

c# xml wcf
1个回答
0
投票

第一个图像指定ServiceContract的命名空间,以指定您的身体的命名空间,您可以尝试消息合同。但是你应该注意返回的类型也应该有MessageContract属性。

以下是我的代码。

 [MessageContract(WrapperNamespace = "www.message.com")]

public class Employee
{
    [MessageBodyMember(Namespace = "www.message.com")]
    public string Id { get; set; }
    [MessageBodyMember(Namespace = "www.message.com")]
    public string Name { get; set; }
    [MessageBodyMember(Namespace = "www.message.com")]
    public string Department { get; set; }
    [MessageBodyMember(Namespace = "www.message.com")]
    public string Grade { get; set; }
}


[ServiceContract]
public interface IEmployeeService
{
   [OperationContract]
    Employee GetEmployee(Employee employee);
}

 public class EmployeeService : IEmployeeService
{
    public Employee GetEmployee(Employee employee)
    {
        return employee;
    }
}

当我在客户端呼叫服务时

  using (ChannelFactory<IEmployeeService> ChannelFactory = new ChannelFactory<IEmployeeService>("emp"))
        {
            // ChannelFactory.Endpoint.EndpointBehaviors.Add(new MyEndpointBehavior());
            IEmployeeService employeeService = ChannelFactory.CreateChannel();
            employeeService.GetEmployee(new Employee() { Name = "abc", Department = "dep", Grade = "male", Id = "1er" });
            // List<Employee> list=  employeeService.GetList();
            Console.Read();
        }

结果。 enter image description here

有关更多信息,请参阅https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-message-contracts

© www.soinside.com 2019 - 2024. All rights reserved.