1. Create Interface like,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace PatentService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
List<PatentModel> GetPatentList(string PatentType);
}
[DataContract]
public class PatentModel
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Link { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string PublishedDate { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace PatentService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
List<PatentModel> GetPatentList(string PatentType);
}
[DataContract]
public class PatentModel
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Link { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string PublishedDate { get; set; }
}
}
2. In the SVC.cs file.
using PatentService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using System.Xml.Linq;
namespace PatentService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public List<PatentModel> GetPatentList(string PatentType)
{
try
{
List<PatentModel> patentList = new List<PatentModel>();
PatentModel patentInfo = new PatentModel() { Description = "No patents available" };
XDocument xDocument = XDocument.Load(HttpContext.Current.Server.MapPath(@"~\Files\PatentTypeList.xml"));
var FeedList = xDocument.Element("Feeds").Elements();
if (FeedList.Count() > 0)
{
List<PatentTypeModel> availablePatentList = FeedList.Select(p => new PatentTypeModel
{
Category = p.Element("Category").Value,
FeedURL = p.Element("FeedURL").Value,
Type = p.Element("Type").Value
}).ToList();
PatentTypeModel selectedPatent = availablePatentList.Find(p => p.Type.ToLower() == PatentType.ToLower());
if (selectedPatent != null)
{
string feedSource = new WebClient().DownloadString(selectedPatent.FeedURL);
XDocument feedDocument = XDocument.Parse(feedSource);
var feeds = feedDocument.Descendants("channel").Elements("item");
if (feeds.Count() > 0)
{
patentList = feeds.Select(f => new PatentModel
{
Title = f.Element("title").Value,
Description = f.Element("description").Value,
Link = f.Element("link").Value,
PublishedDate = f.Element("pubDate").Value
}).ToList();
return patentList;
}
else
{
patentList.Add(patentInfo);
return patentList;
}
}
else
{
patentList.Add(patentInfo);
return patentList;
}
}
else
{
patentList.Add(patentInfo);
return patentList;
}
}
catch (Exception ex)
{
throw;
}
}
}
}
3. Create XML file in Files\ folder
<?xml version="1.0" encoding="utf-8" ?>
<Feeds>
<Feed>
<Category>Computers</Category>
<Type>Graphic processing</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat345.xml</FeedURL>
</Feed>
<Feed>
<Category>Computers</Category>
<Type>I/O Systems</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat710.xml</FeedURL>
</Feed>
<Feed>
<Category>Computers</Category>
<Type>CPUs</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat712.xml</FeedURL>
</Feed>
<Feed>
<Category>Computers</Category>
<Type>Dynamic Storage</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat369.xml</FeedURL>
</Feed>
<Feed>
<Category>Computers</Category>
<Type>Static Storage</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat365.xml</FeedURL>
</Feed>
<Feed>
<Category>Computers</Category>
<Type>Printers/Printing </Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat347.xml</FeedURL>
</Feed>
<Feed>
<Category>Software</Category>
<Type>Artificial Intelligence</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat706.xml</FeedURL>
</Feed>
<Feed>
<Category>Software</Category>
<Type>Databases</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat707.xml</FeedURL>
</Feed>
<Feed>
<Category>Software</Category>
<Type>Development</Type>
<FeedURL>http://www.freepatentsonline.com/rssfeed/rsspat717.xml</FeedURL>
</Feed>
</Feeds>
4. Change the Web.Config file (in bold letters are needs to add)
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<!-- START :: Need to configure services and behaviors section while deploy in to IIS-->
<services>
<service behaviorConfiguration="PatentServiceBehavior" name="PatentService.Service1">
<endpoint address="http://localhost:59285/Service1.svc" binding="basicHttpBinding" contract="PatentService.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="PatentServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
</behaviors>
<!-- END :: Need to configure services and behaviors section while deploy in to IIS-->
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
5. Save and run the SVC file in WCF Test client environment.
Happy coding....
No comments:
Post a Comment