WCF-具有相同实现的两个接口(XML和DataContract序列化)

时间:2018-10-19 11:44:52

标签: c# wcf datacontractserializer xml-formatting

我正在尝试为Soap Service设置WCF REST端点。 问题是,我想对XML使用XMLSerializer,对其他类型使用DataContractSerializer。

试图使用DataContractSerializer,但尚未成功序列化请求。我需要更改类的结构以使其起作用,并且如果可能的话,希望避免这种情况。

问题:尽管我设法使端点与JSON和XML一起使用,但现在 SOAP功能受到损害。 从客户端项目添加服务引用时,我看到类定义已加倍。

Reference.cs

从XML服务合同中,我得到带有

注释的类
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://WebService/XML/REST")]

,并且从JSON / SOAP接口中,我得到了带有如下注释的相同类:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="MethodARequest", Namespace="http://schemas.datacontract.org/2004/07/WebService")]
[System.SerializableAttribute()]

这是我设置的2个界面:

1.(对于REST JSON请求,原始SOAP)

[ServiceContract]
public interface IWCFService
{
[OperationContract]
    [WebInvoke(Method ="POST",  UriTemplate = "MethodA")]
    MethodAResponse MethodA(MethodARequest request);

    [OperationContract]
    [WebInvoke(Method = "POST",  UriTemplate = "MethodB")]
    MethodBResponse MethodB(MethodBRequest request);
}

2.(用于XML请求)

[ServiceContract(Namespace = "")]
[XmlSerializerFormat]
public interface IXMLRestService
{
    [OperationContract]
    [WebInvoke(Method ="POST",  UriTemplate = "MethodA")]
    MethodAResponse MethodA(MethodARequest request);

    [OperationContract]
    [WebInvoke(Method = "POST",  UriTemplate = "MethodB")]
    MethodBResponse MethodB(MethodBRequest request);      
}

两个接口的实现:

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class XYService : IJSONRestService, IXMLRestService
{
   public MethodAResponse MethodA(MethodARequest request){}

   public MethodBResponse MethodB(MethodBRequest request){}
}

如何解决这个歧义问题? ** 是否可以隐藏由IXMLRestService接口创建的类定义?

我的app.config

  <service behaviorConfiguration="WebServiceBehavior" name="WebService">
    <endpoint address="http://localhost:45000/WebService/V2" binding="basicHttpBinding" bindingConfiguration="WebServiceTransportSecurity" contract="IWCFService">
    </endpoint>
    <endpoint address="http://localhost:45000/WebService/V2/REST"  binding="webHttpBinding" behaviorConfiguration="webhttp" contract="IWCFService">
    </endpoint>
    <endpoint address="http://localhost:45000/WebService/V2/XML/REST"  binding="webHttpBinding" behaviorConfiguration="webhttp" contract="IXMLRestService">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>

0 个答案:

没有答案