Hi Dear:
I have Created a Restful Service in C#, and Now i want to use my this Service in C# Restful Client. Client may be a Console Client, but Asp.net Client is preferred.
I have tried a lot more Tutorials, but no Success till now,
Please suggest me some Working Sample,
My Project code is as follow:
Here is the code of
Web.Config File in Servie Project:
="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>
<services>
<service name="Restful.Restful" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="Restful.IRestful" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name ="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
My
IRestful.cs Code
My Project(Restful Service) Name is Restful,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace Restful
{
[ServiceContract]
public interface IRestful
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "name/id={id}")]
string test(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "xml/?id={id}")]
string xdata(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/?id={id}")]
string jdata(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "sum/?a={a}&b={b}")]
string sum(int a, int b);
}
}
My
Restful.svc.cs Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace Restful
{
public class Restful : IRestful
{
#region IRestService Members
public string test(string id)
{
return "Testing Phase is Continued. :" + id;
}
public string xdata(string id)
{
return "Your Requested XML Product is :" + id;
}
public string jdata(string id)
{
return "Your Requested XML Product is :" + id;
}
public string sum(int a, int b)
{
return "Sum of Numbers is: " + (a + b);
}
#endregion
}
}
I have Tried to Consume Service using ASP.net,
My ASP.net project name is WebClient
Here is the code of
WebForm.aspx.cs File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Http;
namespace WebClient
{
public partial class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpClient client = new HttpClient();
HttpResponseMessage msg = client.GetAsync("http://localhost:27629/Restful.svc/sum/?a=23&b=90").ContinueWith<httpresponsemessage>;
HttpContent strm = msg.Content();
var data = strm.ReadAsStreamAsync();
Label1.Text = Convert.ToString(data.Result);
}
}
}
Here is the code of
WebForm.aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebClient.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<div>
</div>
</form>
</body>
</html>
Please refer me tos solution, that may lead to get rid of this problem....
Thanks In Advance...