Tuesday, July 31, 2012

Data Table

Tuesday, July 10, 2012

WCF REST Service Consuming the Methods and Error Handling


private void Method3()
{
try
{
string URL = "http://Bookservices.intranet/Services/LibraryBookService.svc/insertBook";
string xmlTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<BookOrders>"
+ "<BookID>xxxx</BookID>"
+ "<BookName>215786</BookName>"
+ "<CustomerID>175254</CustomerID>"
+ "<RequestedDate>12/12/12</RequestedDate>"
+ "<Title>One</Title>"
+ "<Description>Two</Description>"
+ "</BookOrders>";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@URL);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(xmlTemplate);
request.ContentType = "application/xml";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string xmlString = reader.ReadToEnd();
}
catch (Exception ex)
{
string xmlErrorTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<ErrorDetails>"
+ "<HtmlStatusCode>HtmlStatusCode</HtmlStatusCode>"
+ "<StatusDescription>StatusDescription</StatusDescription>"
+ "</ErrorDetails>";
string request;
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlErrorTemplate);
XmlNodeList xNodeList = xDoc.SelectNodes("/ErrorDetails");
foreach (XmlNode xn in xNodeList)
{
xn["HtmlStatusCode"].InnerText = HttpContext.Current.Response.StatusCode.ToString();
xn["StatusDescription"].InnerText = "Method Name is not Correct..";
request = xn.OuterXml;
Label1.Text = request;

}
}


}

Wednesday, July 4, 2012

WCF With Rest Service Configuration


<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="connectionString" connectionString="server=XXX;user id=zzz;password=aaaa;database=DBNAME; persist security info=false;Connection TimeOut=3000; " />
</connectionStrings>
<system.serviceModel>
<services>
<service name="TestRestService.Service1" behaviorConfiguration="XmlServiceBehaviour" >
<endpoint address=""
binding="webHttpBinding"
contract="TestRestService.IService1"
behaviorConfiguration="web"></endpoint>
</service>
</services>
<!--END OF CHANGE #1-->
<bindings />
<client />
<behaviors>
<serviceBehaviors>
<!--CHANGE #2-->
<behavior name="XmlServiceBehaviour">
<!--END OF CHANGE #2-->
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<!--CHANGE #3-->
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
<!--END OF CHANGE #3-->
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>


Note:

<!--CHANGE #1-->
<!-- Name: TestRestService.Service1 is the NameSpace/Class
Behaviour Config: XmlServiceBehaviour is UserDefined
Contract: TestRestService.IService1 is NameSpace/Interface-->
<!-- IN Change #2 Change #2 behavior name=Change #1 behaviorConfiguration

I.e, Name=BehaviourConfig-->


and In Interface

[ServiceContract]
public interface IService1
{
[OperationContract]
//attribute for returning JSON format
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "ListJson/{UserID}")]
string ListJson(string UserID);
}


Monday, July 2, 2012

XML Converstions in ASP.net


XmlDocument xDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(EntityIDList);
            //string xNode = xDoc.OuterXml;
            if (EntityIDList != null && EntityIDList != "")

                ObjListBO.EntityIdList = xDoc.OuterXml.Replace("<RootXML>", "").Replace("</RootXML>", "");

Converting the DataSet Details into XML Format of String in ASP.Net
---------------------------------------------------------------


Public string GetDetails(int UserID, int Psw)
{
DataSet ds = new DataSet();

SqlParameter[] sqlParam = new SqlParameter[2];
try
{
sqlParam[0] = new SqlParameter("@UserID", SqlDbType.Int) { Direction = ParameterDirection.Input, Value = UserID };
sqlParam[1] = new SqlParameter("@Psw", SqlDbType.Int) { Direction = ParameterDirection.Input, Value = Psw };
ds = SqlHelper.ExecuteDataset(myConn, CommandType.StoredProcedure, "SP_GetUser", sqlParam);
xmlDoc = new XmlDocument();
XmlDocument xDoc = new XmlDocument();
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
xmlDoc.LoadXml(ds.GetXml());
XmlNodeList xnList = xmlDoc.SelectNodes("/NewDataSet/Table");
outResultXml = "<Root>";
foreach (XmlNode xn in xnList)
{
outResultXml += "<User>";
outResultXml += xn.InnerXml;
outResultXml += "</User>";
}
outResultXml += "</Root>";
}
}
catch (Exception ex)
{
throw ex;
}
return outResultXml;
}