I have created WCF Rest service that take input/para from client and save it in database and knowledge with identity of table in which data is added.When I am consuming service throgh client that is nothing but web application making url as web request and getting proper output.But when I am consuming through browser getting error. First I tried like this :-
http://localhost:Portnumber/Test.svc/ADD/Bodywash/Orange/50
after that like this ht
tp://localhost:Portnumber/Test.svc/ADD?Name=Bodywash&CategoryName=Orange&Price=50
I am getting error'Entpoint not found'.How to consume rest service through browser?Will I able to do that
// Code of service
-- Table
CREATE TABLE [dbo].[Product](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[CategoryName] [varchar](50) NULL,
[Price] [int] NULL
)
-- Sp
ALTER procedure [dbo].[proc_InsertProduct]
(
@id int out,
@Name nvarchar(50),
@CategoryName nvarchar(50),
@Price int
)
as insert into Product
(
Name,
CategoryName,
Price
)
values
(
@Name,
@CategoryName,
@Price
)
set @id = @@identity
return @id
-- Interface
public interface ITest
{
[WebInvoke(UriTemplate = "ADD/{Name}/{CategoryName}/{Price}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
int InsertProduct(string Name, string CategoryName, string Price);
}
-- class
public class Test : ITest
{
public int InsertProduct(string Name, string CategoryName, string Price)
{
string constr = ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("proc_InsertProduct", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
cmd.Parameters.AddWithValue("@Price", Price);
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Direction = ParameterDirection.Output;//Output parameter
cmd.ExecuteNonQuery();
con.Close();
int result = (int)(cmd.Parameters["@id"].Value);
return result;//returning id
}
}
// WebConfig
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<connectionStrings>
<add name="SampleConnectionString" connectionString="Data Source=xxx.xxx.x.x;Initial Catalog=sample;Persist Security Info=True;User ID=sa;Password=xxxx" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="RestApiTest.Test" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="webHttpBinding" contract="RestApiTest.ITest" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="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"/>
<!--
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>