[ServiceContract]
public interface ISuggestService
{
[OperationContract]
string Search( string friend);
}
public class SuggestService : ISuggestService
{
public string Search( string friend)
{
return "Hello, " + friend;
}
}
[OperationContract]
[WebGet(UriTemplate = "/Search?q={friend}" ,
BodyStyle = WebMessageBodyStyle.Bare)]
string Search( string friend);
< system.serviceModel >
...
< behaviors >
...
< endpointBehaviors >
< behavior name ="REST" >
< webHttp />
</ behavior >
</ endpointBehaviors >
...
</ behaviors >
...
</ system.serviceModel >
< services >
< service behaviorConfiguration ="SuggestServiceBehavior"
name ="SuggestService" >
< endpoint address =""
binding ="webHttpBinding"
contract ="ISuggestService"
behaviorConfiguration ="REST" />
</ service >
</ services >
<? xml version ="1.0" encoding ="utf-8" ? >
< SearchSuggestion >
< Query > guy </ Query >
< Section >
< Item >
< Text > guyhaim </ Text >
< Description > @rohitbhargava tinyurl.com/6m9e4g </ Description >
< Url > twitter.com/home </ Url >
< Image source ="http://...normal.jpg"
height ="48" width ="48" alt ="guyhaim" />
</ Item >
< Item >
< Text > Guy Malachi </ Text >
< Description > Yahoo toolbar looks kinda weird. </ Description >
< Url > twitter.com/home </ Url >
< Image source ="http://...normal.jpg"
height ="48" width ="48" alt ="guym" />
</ Item >
< Item >
< Text > guy zohar </ Text >
< Description > switch screen </ Description >
< Url > twitter.com/home </ Url >
< Image source ="http://...normal.jpg"
height ="48" width ="48" alt ="guyzo" />
</ Item >
< Item >
< Text > guyzarz </ Text >
< Description > @ekampf May it rest in peace, in one piece. </ Description >
< Url > twitter.com/home </ Url >
< Image source ="http://...normal.jpg"
height ="48" width ="48" alt ="guyzarz" />
</ Item >
</ Section >
</ SearchSuggestion >
// Image.cs
[XmlType]
public class Image
{
[ XmlAttribute (AttributeName= "source" )]
public string Source { get ; set ; }
[ XmlAttribute (AttributeName = "height" )]
public int Height { get ; set ; }
[ XmlAttribute (AttributeName = "width" )]
public int Width { get ; set ; }
[ XmlAttribute (AttributeName = "alt" )]
public string Alt { get ; set ; }
}
// Item.cs
[XmlType]
public class Item
{
[XmlElement]
public string Text { get ; set ; }
[XmlElement(IsNullable= false )]
public string Description { get ; set ; }
[XmlElement(IsNullable = false )]
public string Url { get ; set ; }
[XmlElement(IsNullable = false )]
public Image Image { get ; set ; }
}
// SearchSuggestion.cs
[XmlRoot(Namespace= "" )]
public class SearchSuggestion
{
public SearchSuggestion()
{
this .Section = new List <Item>();
}
[XmlElement]
public string Query { get ; set ; }
[XmlArray]
public List <Item> Section { get ; set ; }
}
[ServiceContract]
public interface ISuggestService
{
[OperationContract]
[WebGet(UriTemplate = "/Search?q={friend}" ,
BodyStyle = WebMessageBodyStyle.Bare)]
[XmlSerializerFormat]
SearchSuggestion Search( string friend);
}
public SearchSuggestion Search( string friend)
{
SearchSuggestion suggestion = new SearchSuggestion
{
Query = friend
};
suggestion.Section.Add( new Item
{
Text = friend,
Description = "Hello, " + friend,
Url = "http://blogs.microsoft.co.il/blogs/bursteg" ,
Image = new Image
{
Source = "http://tinyurl.com/burstegprofileimage" ,
Alt = "Guy Burstein" ,
Width = 48,
Height = 48
}
});
return suggestion;
}
<? xml version ="1.0" encoding ="utf-8" ? >
< SearchSuggestion xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
< Query > omer </ Query >
< Section >
< Item >
< Text > omer </ Text >
< Description > Hello, omer </ Description >
< Url > blogs.microsoft.co.il/blogs/bursteg </ Url >
< Image source ="http://tinyurl.com/burstegprofileimage"
height ="48"
width ="48"
alt ="Guy Burstein" />
</ Item >
</ Section >
</ SearchSuggestion >
<? xml version ="1.0" encoding ="UTF-8" ? >
< OpenSearchDescription xmlns ="http://a9.com/-/spec/opensearch/1.1/" >
< ShortName > Friends Search </ ShortName >
< Url type ="text/html"
template ="http://localhost:50434/TwitterFriendsSearch/
SuggestService.svc/search?q={searchTerms}" />
< Url type ="application/x-suggestions+xml"
template ="http://localhost:50434/TwitterFriendsSearch/
SuggestService.svc/search?q={searchTerms}" />
< Image height ="16"
width ="16"
type ="image/icon" > twitter.com/favicon.ico </ Image >
</ OpenSearchDescription >
< script type ="text/javascript" >
function Register() {
window.external.AddSearchProvider( 'FriendsSuggestion.xml' );
}
</ script >
< form id ="form1" runat ="server" >
< div >
< button onclick ='Register();' > Click Here </ button >
</ div >
</ form >
string url = string .Format(
"http://twitter.com/statuses/friends/{0}.xml" , "bursteg" );
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
reader.Close();
XDocument document = XDocument .Parse(response, LoadOptions.None);
var query = from e in document.Root.Descendants( "user" )
where e.Element( "name" ).Value.Contains(friend) ||
e.Element( "screen_name" ).Value.Contains(friend)
select new Item
{
Text = e.Element( "name" ).Value,
Image = new Image
{
Source = e.Element( "profile_image_url" ).Value,
Alt = e.Element( "screen_name" ).Value,
Width = 48,
Height = 48
},
Description = (e.Element( "status" ) == null ? "" :
HttpUtility.HtmlDecode(e.Element( "status" ).Element( "text" ).Value)),
Url = ( String .IsNullOrEmpty(e.Element( "url" ).Value) ?
"http://twitter.com/home" :
e.Element( "url" ).Value)
};
suggestion.Section.AddRange(query);
public SearchSuggestion Search( string friend)
{
SearchSuggestion suggestion = new SearchSuggestion();
suggestion.Query = friend;
string url = string .Format( "http://twitter.com/statuses/friends/{0}.xml" , "bursteg" );
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
reader.Close();
XDocument document = XDocument .Parse(responseString, LoadOptions.None);
var query = from e in document.Root.Descendants( "user" )
where e.Element( "name" ).Value.Contains(friend) ||
e.Element( "screen_name" ).Value.Contains(friend)
select new Item
{
Text = e.Element( "name" ).Value,
Image = new Image
{
Source = e.Element( "profile_image_url" ).Value,
Alt = e.Element( "screen_name" ).Value,
Width = 48,
Height = 48
},
Description = (e.Element( "status" ) == null ? "" :
HttpUtility.HtmlDecode(e.Element( "status" ).Element( "text" ).Value)),
Url = ( String .IsNullOrEmpty(e.Element( "url" ).Value) ?
"http://twitter.com/home" :
e.Element( "url" ).Value)
};
suggestion.Section.AddRange(query);
return suggestion;
}
Source: https://habr.com/ru/post/68226/
All Articles