1. وب سایت شما حتما باید ورژن 4.5 باشد ، درصورتیکه وب سایت شما با نسخه دیگری ساخته شده باشد باید ورژن آن را عوض کنید


<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
2. اضافه کردن refrence که در فهرست به نام WCF Service ثبت شده است .
در این پروژه اسم فایل را MyService.svc گذاشته ایم.

3. اضافه کردن فایل مربوط به سرویس

4. جستجوی : ServiceModel و انتخاب System.ServiceModel.Web
و اضافه کردن System.Runtime.Serialization

5. تنظیمات وب کانفیگ دقیقا بر اساس منطق زیر:
<system.serviceModel>
<services>
<service name="WebApplication2.MyService">
<endpoint address="../MyService.svc" binding="webHttpBinding" contract="WebApplication2.IMyService" behaviorConfiguration="webBehaviour"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<system.serviceModel/>
6. اضافه کردن using System.ServiceModel.Web; به فایل IMyService.cs و MyService.svc

7. تعریف کلاس kwStatus برای خروجی تابع که نتیجه و پیغام را بر میگرداند .
این کلاس هر جایی می تواند نوشته شود فقط ویژگی DataContract را باید داشته باشد و پس از آن متد LOGIN را در MyService اضافه می کنیم :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WebApplication2
{
[DataContract]
public class kwStatus
{
public kwStatus(string _status, string msg)
{
this.Status = _status;
this.msg = msg;
}
[DataMember]
public string Status { get; set; }
[DataMember]
public string msg { get; set; }
}
public class MyService : IMyService
{
public void DoWork()
{
}
public kwStatus login(string Username, string Password)
{
kwStatus _kwStatus;
if (Username=="karnaweb" && Password=="123456")
{
_kwStatus = new kwStatus("ok", "خوش آمدید");
}
else
{
_kwStatus = new kwStatus("fail", "نام کاربری یا رمز ورود اشتباه است");
}
return _kwStatus;
{
{
{
8. حالا متد Login را باید در فایل IMyService.cs بازنویسی کنیم :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WebApplication2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMyService" in both code and config file together.
[ServiceContract]
public interface IMyService
{
[OperationContract]
void DoWork();
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "login/{Username}/{Password}")]
kwStatus login(string Username, string Password);
{
{
9. حالا پروژه را اجرا کنید و ساختار URL را بر اساس UriTemplate که در بالا وارد کرده ایم + آدرس لوکال هاست :
http://localhost:28022/MyService.svc/login/karnaweb/123456
دانلود سورس کد