Вы можете использовать класс unwrapped [MessageContract]
, если хотите удалить дополнительный элемент в запросе XML.Код ниже показывает ваш пример с таким контрактом.
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();
}
}