在WCF中开发Web API [英] Development of Web API within WCF

查看:114
本文介绍了在WCF中开发Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开发了一个wcf应用程序。以下是代码:



1) IService1.cs



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Runtime。序列化;

使用System.ServiceModel;

使用System.ServiceModel.Web;

使用System.Text;

使用DAL;

命名空间演示

{

//注意:您可以使用重构菜单上的重命名命令在代码和配置文件中一起更改接口名称IService1。

[ServiceContract]

公共接口IService1

{



[OperationContract]

string GetData(int value);



[OperationContract]

CompositeType GetDataUsingDataContract(CompositeType复合);



// TODO:在这里添加服务操作



//操作合同从这里开始

[OperationContract]



字符串测试();



[OperationContract]

List< client> GetAllClients();

[OperationContract]

客户端GetClientByID(长ClientID);



[OperationContract]

布尔值UpdateClient(客户端clientObj);

[OperationContract]

List< client> SearchClients(Client clientObj);

[OperationContract]

List< product> GetAllProducts();

[OperationContract]

Product GetProductByID(long ProductID);

[OperationContract]

布尔值UpdateProduct(Product productObj);

[OperationContract]

List< product> SearchProducts(Product productObj);



[OperationContract]

List< billing> GetAllBills();

[OperationContract]

发票GetInvoiceByID(长InvoiceID);

[OperationContract]

布尔值UpdateInvoice(Invoice invoiceObj);

[OperationContract]

List< invoice> SearchInvoices(Invoice invoiceObj);



}





//使用如下例所示的数据合同,为服务操作添加复合类型。

[DataContract]

公共类CompositeType

{

bool boolValue = true;

string stringValue =Hello;



[DataMember]

public bool BoolValue

{

get {return boolValue; }

set {boolValue = value; }

}



[DataMember]

public string StringValue

{

get {return stringValue; }

set {stringValue = value; }

}







}

}





2) Service1.svc.cs



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Runtime.Serialization;

使用System.ServiceModel;

使用System.ServiceModel.Web;

使用System.Text;

使用DAL;



命名空间演示

{

//注意:您可以使用重构菜单上的重命名命令将代码,svc和配置文件中的类名Service1一起更改。

//注意:为了启动WCF测试客户端要测试此服务,请在Solution Explorer中选择Service1.svc或Service1.svc.cs并开始调试。

公共类Service1:IService1

{

public string GetData(int value)

{

return string.Format(你输入:{0},值);

}



public CompositeType GetDataUsingDataContract(CompositeType composite)

{

if(composite == null)

{

抛出新的ArgumentNullException(复合);

}

if(composite.BoolValue)

{

composite.StringValue + =Suffix;

}

返回复合;

}



public string Test()

{

返回你好;

}

public List< client> GetAllClients()

{

返回DAL.Class1.GetAllClients();

}







public Client GetClientByID(long ClientID)

{

返回DAL.Class1.GetClientByID (ClientID);

}



public Boolean UpdateClient(Client clientObj)

{

返回DAL.Class1.UpdateClient(clientObj);

}

public List< client> SearchClients(Client clientObj)

{

返回DAL.Class1.SearchClients(clientObj);

}

public列表与LT;产品> GetAllProducts()

{

返回DAL.Class1.GetAllProducts();

}

公共产品GetProductByID( long ProductID)

{

返回DAL.Class1.GetProductByID(ProductID);

}

public Boolean UpdateProduct(Product productObj)

{

返回DAL.Class1.UpdateProduct(productObj) ;

}

public List< product> SearchProducts(Product productObj)

{

返回DAL.Class1.SearchProducts(productObj);

}



public List< billing> GetAllBills()

{

返回DAL.Class1.GetAllBills();

}

公开发票GetInvoiceByID( long InvoiceID)

{

返回DAL.Class1.GetInvoiceByID(InvoiceID);

}

public Boolean UpdateInvoice (发票invoiceObj)

{

返回DAL.Class1.UpdateInvoice(invoiceObj);

}

public List< ;发票> SearchInvoices(Invoice invoiceObj)

{

返回DAL.Class1.SearchInvoices(invoiceObj);

}

< br $>




}

}



如何为这个wcf代码开发web apis。是否可以在此wcf应用程序中使用asp.net实现web api概念。

I just developed a wcf application . Below is the code:

1)IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DAL;
namespace Demo
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here

//Operation Contracts starts here
[OperationContract]

string Test();

[OperationContract]
List<client> GetAllClients();
[OperationContract]
Client GetClientByID(long ClientID);

[OperationContract]
Boolean UpdateClient(Client clientObj);
[OperationContract]
List<client> SearchClients(Client clientObj);
[OperationContract]
List<product> GetAllProducts();
[OperationContract]
Product GetProductByID(long ProductID);
[OperationContract]
Boolean UpdateProduct(Product productObj);
[OperationContract]
List<product> SearchProducts(Product productObj);

[OperationContract]
List<billing> GetAllBills();
[OperationContract]
Invoice GetInvoiceByID(long InvoiceID);
[OperationContract]
Boolean UpdateInvoice(Invoice invoiceObj);
[OperationContract]
List<invoice> SearchInvoices(Invoice invoiceObj);

}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}



}
}


2) Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DAL;

namespace Demo
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}

public string Test()
{
return "hello";
}
public List<client> GetAllClients()
{
return DAL.Class1.GetAllClients();
}



public Client GetClientByID(long ClientID)
{
return DAL.Class1.GetClientByID(ClientID);
}

public Boolean UpdateClient(Client clientObj)
{
return DAL.Class1.UpdateClient(clientObj);
}
public List<client> SearchClients(Client clientObj)
{
return DAL.Class1.SearchClients(clientObj);
}
public List<product> GetAllProducts()
{
return DAL.Class1.GetAllProducts();
}
public Product GetProductByID(long ProductID)
{
return DAL.Class1.GetProductByID(ProductID);
}
public Boolean UpdateProduct(Product productObj)
{
return DAL.Class1.UpdateProduct(productObj);
}
public List<product> SearchProducts(Product productObj)
{
return DAL.Class1.SearchProducts(productObj);
}

public List<billing> GetAllBills()
{
return DAL.Class1.GetAllBills();
}
public Invoice GetInvoiceByID(long InvoiceID)
{
return DAL.Class1.GetInvoiceByID(InvoiceID);
}
public Boolean UpdateInvoice(Invoice invoiceObj)
{
return DAL.Class1.UpdateInvoice(invoiceObj);
}
public List<invoice> SearchInvoices(Invoice invoiceObj)
{
return DAL.Class1.SearchInvoices(invoiceObj);
}



}
}

How to develop web apis for this wcf code. Is it possible to implement web api concept using asp.net in this wcf application.

推荐答案

否。 ASP.NET WepApi是一种用于在Http之上构建Web服务的新技术。 WebApi本身是WCF和ASP.NET MVC的组合功能。



希望这有助于
No. ASP.NET WepApi is a new technology for building webservices on the top of Http. WebApi itself is a combined feature of WCF and ASP.NET MVC.

Hope this helps


这篇关于在WCF中开发Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆