Thursday, 21 June 2012

IRequestChannel - Demo 1

Let's assume below ServiceContract

[ServiceContract]
public interface IDemo1
{
    [OperationContract]
    string HelloWorld(string name);
}

so how can we invoke above using IRequestChannel

var httpsBasicBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
var endpoint = "https://host/service.svc";
var channelFactory = new ChannelFactory<IRequestChannel>(httpsBasicBinding, endpoint);
var channel = null;
 
try
{
    channelFactory.Open();
 
    channel = channelFactory.CreateChannel();
 
    channel.Open(); 
    
    string body = "<HelloWorld xmlns="http://tempuri.org/"><name>Suresh</name></HelloWorld>";
    var messageBody = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(body)
    Message message = Message.CreateMessage(MessageVersion.Soap11,  
                       "http://tempuri.org/IDemo1/HelloWorld");
 
    Message response = channel.Request(message);
    var result = response.GetReaderAtBodyContents().ReadOuterXml(); //parse based on the requirments
    
    Console.WriteLine(result);
    
    channel.Close();
}
finally
{
    channel.close(); //abort if faulted 
    channelFactory.Close(); //abort if faulted
}
 
reference : http://www.codeproject.com/Articles/34632/How-to-pass-arbitrary-data-in-a-Message-object-usi 

Wednesday, 13 June 2012

XSD: restriction on type to not be empty or blank

I wanted to create a SimpleType called 'ValidInput', basically i don't want to allow empty spaces. I started with below but for some reasons it worked on XML SPY, but not thru c#.

  <xs:simpleType name="ValidInput">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:whiteSpace value="collapse"/>
    </xs:restriction>
  </xs:simpleType>

But below worked, can't figure out why


<xs:simpleType name="ValidInput">
    <xs:restriction base = "xs:string">
       <xs:minLength value="1" />
       <xs:pattern value=".*[^\s].*" />
   </xs:restriction>
</xs:simpleType>