यहाँ मैं WCF बाकी हिस्सों में POST पद्धति के लिए कोड काम कर रहा है: -
प्रथम आईडी, uname और लोक निर्माण विभाग के क्षेत्रों के साथ डेटाबेस तालिका बनाएँ। मूल्यों को सम्मिलित करने के लिए संग्रहीत प्रक्रिया बनाएं।
create procedure [dbo].[sproc_Insertusers]
(
@id int out,
@uname nvarchar(50),
@pwd nvarchar(50)
)
as insert into tbl_register
(
[uname],
[pwd]
)
values
(
@uname,
@pwd
)
set @id = @@identity
return @id
नई WCF प्रोजेक्ट बना
IService1.cs में
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "user_register/{uname}/{pwd}")]
int user_register(string uname,string pwd);
}
Service1.cs
public class Service1 : IService1
{
SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["iwealth_db"]);
SqlCommand cmd;
DataSet ds;
SqlDataAdapter da;
int result;
public int user_register(string uname, string pwd)
{
cmd = new SqlCommand("sproc_Insertusers", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@uname",uname);
cmd.Parameters.AddWithValue("@pwd", pwd);
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Direction = ParameterDirection.Output;//Output parameter
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
result = (int)(cmd.Parameters["@id"].Value);
return result;//returning id
}
}
web.config में
में: -
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="iwealth_db" value="Data Source=localhost;Initial Catalog=iwealth; Trusted_Connection=true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="iWealthService.Service1" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="iWealthService.IService1" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- 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>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
डब्ल्यूसीएफ बनाएं।
protected void Button1_Click(object sender, EventArgs e)
{
//in 'sURL' paste WCF service link up to .svc and write -> /user_register/Prateek/1234
//here user_register is service method path and Prateek and 1234 are parameters that will enter to database
string sURL = "http://localhost:49271/Service1.svc/user_register/Prateek/1234";
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
wrGETURL.Method = "POST";
wrGETURL.ContentType = @"application/json; charset=utf-8";
HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse;
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
// read response stream from response object
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
// read string from stream data
strResult = loResponseStream.ReadToEnd();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
// assign the final result to text box
Response.Write(strResult);
}
-: अब, नीचे दिए गए कोड का उपयोग कैसे करें या उपभोग करने के लिए इस WCF सेवा
इस WCF सेवा
और बटन क्लिक कोड पर परीक्षण करने के लिए
जोड़ें रजिस्टर बटन नई वेबसाइट बनाएं है
कृपया से हम आपके लिए समस्या को हल करने के लिए न कहें। हमें दिखाएं कि _you_ ने समस्या को हल करने का प्रयास कैसे किया, फिर हमें दिखाएं कि परिणाम क्या था, और हमें बताएं कि आपको ऐसा क्यों लगता है कि यह काम नहीं करता है। देखें "[आपने क्या प्रयास किया है?] (Http://whathaveyoutried.com/)" एक उत्कृष्ट लेख के लिए जिसे आपको केवल पढ़ने की आवश्यकता है। –