本文介绍了使用 WCF 时如何为 OperationContract 指定命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为无法更改的服务器 API 创建客户端.我的客户目前生成这种格式:

I am creating a client for a server api I can't change. My client currently produces this format:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <StoreSomething xmlns="urn:aaa">
      <Something>
        <SomeText xmlns="urn:bbb">My text</SomeText>
      </Something>
    </StoreSomething>
  </s:Body>
</s:Envelope>

但是,服务器希望 Something 位于 urn:bbb 命名空间中(即,将 xmlns 属性向上移动一级).我怎样才能做到这一点?OperationContractAttribute 没有 Namespace 属性.

However, the server expects Something to be in the urn:bbb namespace (i.e. move the xmlns attribute one level up). How can I achieve this?OperationContractAttribute has no Namespace property.

代码:

using System;
using System.ServiceModel;
using System.Xml.Serialization;

[XmlType(Namespace="urn:bbb")]
public class Something
{
    public string SomeText { get; set; }
}

[XmlSerializerFormat]
[ServiceContract(Namespace="urn:aaa")]
public interface IMyService
{
    [OperationContract]
    void StoreSomething(Something Something);
}

class Program
{
    static void Main(string[] args)
    {
        var uri = new Uri("http://localhost/WebService/services/Store");
        var factory = new ChannelFactory<IMyService>(new BasicHttpBinding(), new EndpointAddress(uri));
        IMyService service = factory.CreateChannel();

        service.StoreSomething(new Something
        {
            SomeText = "My text"
        });
    }
}

推荐答案

我设法使用 解包消息.不幸的是,这导致方法名称 参数名称都被遗漏了.因此,我不得不创建包装类,这会导致代码复杂.

I managed to get it working by using unwrapped messages. This unfortunately results in both the method name and parameter name being left out. I therefore had to create wrapper classes which results in convoluted code.

无论如何,这是使其工作的代码:

Anyway, here is the code that made it work:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [XmlSerializerFormat]
    void StoreSomething(StoreSomethingMessage message);
}

[MessageContract(IsWrapped=false)]
public class StoreSomethingMessage
{
    [MessageBodyMember(Namespace="urn:aaa")]
    public StoreSomething StoreSomething { get; set; }
}

[XmlType(Namespace="urn:bbb")]
public class StoreSomething
{
    public Something Something { get; set; }
}

public class Something
{
    public string SomeText { get; set; }
}

我还创建了一个实现 IMyService 并从 ClientBase 继承的 MyServiceClient因为 IMyService 现在需要一个 StoreSomethingMessage 对象,但为了简单起见,我省略了这部分.

I also created a MyServiceClient which implements IMyService and inherits from ClientBase<IMyService> since IMyService now requires a StoreSomethingMessage object, but i left that part out for simplicity.

我希望有一个更简单的解决方案.

I wish there was a simpler solution.

这篇关于使用 WCF 时如何为 OperationContract 指定命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:06