2011-09-30 55 views
0

閱讀後XmlSerializer with specified pattern not working我嘗試實現這樣的服務:OperationContract與XmlSerializerFormat。但是我的Soap消息包含一個額外的標記,它是操作參數。我如何刪除該標籤?WCF XmlSerializerFormat和OperationContract:嵌套的soap參數序列化

這裏是我的服務樣本

[System.ServiceModel.ServiceContractAttribute(Namespace = "http://mynamespace.com/", ConfigurationName = "ConfigName")] 
public interface MyInterfacePort 
{ 
    [System.ServiceModel.OperationContractAttribute(Action = "http://mynamespace.com/opName", ReplyAction = "*")] 
    [System.ServiceModel.FaultContractAttribute(typeof(MyError), Action = "http://mynamespace.com/opName", Name = "opErr")] 
    [System.ServiceModel.XmlSerializerFormatAttribute()] 
    opResponse opName(opRequest request); 

然後序列化的要求:

[System.Serializable] 
public partial class opRequest 
{ 
     public string myProperty; 

生成的SOAP消息:

<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"> 
     <opName xmlns="http://mynamespace.com/"> 
      <request> 
       <myProperty>262157</myProperty> 
      </request> 
     </opName> 
    </s:Body> 
</s:Envelope> 

我的服務不處理額外<要求>標籤

感謝您的幫助。

回答

0

如果您想要移除XML請求中的附加元素,則可以使用解開的[MessageContract]類。下面的代碼顯示了這種合同的例子。

public class StackOverflow_7607564 
{ 
    [System.ServiceModel.ServiceContractAttribute(Namespace = "http://mynamespace.com/", ConfigurationName = "ConfigName")] 
    public interface MyInterfacePort 
    { 
     [System.ServiceModel.OperationContractAttribute(Action = "http://mynamespace.com/opName", ReplyAction = "*")] 
     [System.ServiceModel.FaultContractAttribute(typeof(MyError), Action = "http://mynamespace.com/opName", Name = "opErr")] 
     [System.ServiceModel.XmlSerializerFormatAttribute()] 
     opNameResponse opName(opNameRequest request); 
    } 
    public class MyError { } 
    [MessageContract(IsWrapped = false)] 
    public class opNameRequest 
    { 
     [MessageBodyMember(Name = "opName")] 
     public opRequest request; 
    } 
    [MessageContract(IsWrapped = false)] 
    public class opNameResponse 
    { 
     [MessageBodyMember(Name = "opNameResponse")] 
     public opResponse response; 
    } 
    [System.Serializable] 
    public partial class opRequest 
    { 
     public string myProperty; 
    } 
    [System.Serializable] 
    public partial class opResponse 
    { 
     public string myProperty; 
    } 
    public class Service : MyInterfacePort 
    { 
     public opNameResponse opName(opNameRequest request) 
     { 
      return new opNameResponse { response = new opResponse { myProperty = request.request.myProperty } }; 
     } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(MyInterfacePort), new BasicHttpBinding(), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     var factory = new ChannelFactory<MyInterfacePort>(new BasicHttpBinding(), new EndpointAddress(baseAddress)); 
     var proxy = factory.CreateChannel(); 
     Console.WriteLine(proxy.opName(new opNameRequest { request = new opRequest { myProperty = "hello world" } }).response.myProperty); 

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

這通常是svcutil生成的適配器,但是使用messageContract您不能使用可選字段。每個消息體將被髮送到服務。我的需求就像引用的帖子(http://stackoverflow.com/questions/7557887/xmlserializer-with-specified-pattern-not-working),其中「degorolls」暗示不使用MessageContract。 – FXB