wcf配置mysql连接_在 Visual Studio 中创建 WCF 数据服务 | Microsoft Docs

创建数据服务Create the data service

08/24/2018

本文内容

在本主题中,你将创建一个示例数据服务,该服务使用 WCF 数据服务公开基于 Northwind 示例数据库的 Open Data Protocol (OData) 源。In this topic, you create a sample data service that uses WCF Data Services to expose an Open Data Protocol (OData) feed that's based on the Northwind sample database. 此任务涉及以下几个基本步骤:The task involves the following basic steps:

创建 ASP.NET Web 应用程序。Create an ASP.NET Web application.

使用实体数据模型工具定义数据模型。Define the data model by using the Entity Data Model tools.

向 Web 应用程序添加数据服务。Add the data service to the Web application.

启用对数据服务的访问。Enable access to the data service.

创建 ASP.NET web 应用Create the ASP.NET web app

在 Visual Studio 的“文件”菜单中,依次选择“新建” > “项目” 。In Visual Studio, on the File menu, select New > Project.

在 " 新建项目 " 对话框中,在 "Visual Basic" 或 "Visual c #" 下选择 " web " 类别,然后选择 " ASP.NET web 应用程序"。In the New Project dialog box, under either Visual Basic or Visual C# select the Web category, and then select ASP.NET Web Application.

输入 NorthwindService 作为项目名称,然后选择 "确定"。Enter NorthwindService as the name of the project and then select OK.

在 " 新建 ASP.NET Web 应用程序 " 对话框中,选择 " 空 ",然后选择 "确定"。In the New ASP.NET Web Application dialog, select Empty and then select OK.

(可选)为 Web 应用程序指定一个特定的端口号。(Optional) Specify a specific port number for your Web application. 注意: 12345 此系列快速入门主题中使用了端口号。Note: the port number 12345 is used in this series of quickstart topics.

在 解决方案资源管理器中,右键单击刚创建的 ASP.NET 项目,然后选择 " 属性"。In Solution Explorer, right-click on the ASP.NET project that you just created, and then choose Properties.

选择 " Web " 选项卡,并将 " 特定端口 " 文本框的值设置为 12345 。Select the Web tab, and set the value of the Specific port text box to 12345.

定义数据模型Define the data model

在解决方案资源管理器中,右键单击 ASP.NET 项目的名称,然后单击 "添加 > 新项"。In Solution Explorer, right-click the name of the ASP.NET project, and then click Add > New Item.

在 " 添加新项 " 对话框中,选择 " 数据 " 类别,然后选择 " ADO.NET 实体数据模型"。In the Add New Item dialog box, select the Data category, and then select ADO.NET Entity Data Model.

对于数据模型的名称,请输入 Northwind.edmx 。For the name of the data model, enter Northwind.edmx.

在 实体数据模型向导中,选择 " 数据库中的 EF 设计器",然后单击 " 下一步"。In the Entity Data Model Wizard, select EF Designer from Database, and then click Next.

执行以下步骤之一,将数据模型连接到数据库,然后单击 " 下一步":Connect the data model to the database by doing one of the following steps, and then click Next:

如果尚未配置数据库连接,请单击 " 新建连接 " 并创建一个新连接。If you don't have a database connection already configured, click New Connection and create a new connection. 此 SQL Server 实例必须附加了 Northwind 示例数据库。This SQL Server instance must have the Northwind sample database attached.

- 或 -- or -

如果已配置一个连接到 Northwind 数据库的数据库连接,请从连接列表中选择该连接。If you have a database connection already configured to connect to the Northwind database, select that connection from the list of connections.

在向导的最后一页中,选中数据库中所有表对应的复选框,并清除视图和存储过程对应的复选框。On the final page of the wizard, select the check boxes for all tables in the database, and clear the check boxes for views and stored procedures.

单击 " 完成 " 关闭向导。Click Finish to close the wizard.

创建 WCF 数据服务Create the WCF data service

在解决方案资源管理器中,右键单击 ASP.NET 项目,然后选择 "添加 > 新项"。In Solution Explorer, right-click on the ASP.NET project, and then choose Add > New Item.

在 "添加新项" 对话框中,从 " Web " 类别中选择 " WCF 数据服务" 项模板。In the Add New Item dialog box, select the WCF Data Service item template from the Web category.

7ca1a6073453635902ac430bf0b123cf.png

备注

WCF 数据服务模板在 visual studio 2015 中提供,但在 visual studio 2017 或更高版本中不可用。The WCF Data Service template is available in Visual Studio 2015, but not in Visual Studio 2017 or later.

对于服务的名称,请键入 Northwind 。For the name of the service, type Northwind.

Visual Studio 将为新服务创建 XML 标记和代码文件。Visual Studio creates the XML markup and code files for the new service. 默认情况下,代码编辑器窗口将打开。By default, the code-editor window opens. 在 解决方案资源管理器中,该服务的名称为 Northwind,扩展名为 svc.cs 或 .svc。In Solution Explorer, the service has the name Northwind with the extension .svc.cs or .svc.vb.

在数据服务的代码中,用数据模型的实体容器的类型(在此示例中为 /* TODO: put your data source class name here */)替换定义数据服务的类定义中的注释 NorthwindEntities。In the code for the data service, replace the comment /* TODO: put your data source class name here */ in the definition of the class that defines the data service with the type that is the entity container of the data model, which in this case is NorthwindEntities. 该类定义应如下所示:The class definition should look this the following:

public class Northwind : DataServicePublic Class Northwind

Inherits DataService(Of NorthwindEntities)

启用对数据服务资源的访问Enable access to data service resources

在数据服务的代码中,用下列代码替换 InitializeService 函数中的占位符代码:In the code for the data service, replace the placeholder code in the InitializeService function with the following:

// Grant only the rights needed to support the client application.

config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead

| EntitySetRights.WriteMerge

| EntitySetRights.WriteReplace );

config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead

| EntitySetRights.AllWrite);

config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);' Grant only the rights needed to support the client application.

config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead _

Or EntitySetRights.WriteMerge _

Or EntitySetRights.WriteReplace)

config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead _

Or EntitySetRights.AllWrite)

config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead)

这使得授权客户端能够对指定实体集的资源进行读写访问。This enables authorized clients to have read and write access to resources for the specified entity sets.

备注

任何可以访问该 ASP.NET 应用程序的客户端也能够访问由数据服务公开的资源。Any client that can access the ASP.NET application can also access the resources exposed by the data service. 在生产数据服务中,为防止对资源进行未经授权的访问,还应保护应用程序本身的安全。In a production data service, to prevent unauthorized access to resources you should also secure the application itself. For more information, see Securing WCF Data Services.

后续步骤Next steps

您已经成功地创建了一个公开基于 Northwind 示例数据库的 OData 源的新数据服务,并且您已为对 ASP.NET Web 应用程序具有权限的客户端启用了对源的访问。You have successfully created a new data service that exposes an OData feed that is based on the Northwind sample database, and you have enabled access to the feed for clients that have permissions on the ASP.NET Web application. 接下来,将从 Visual Studio 启动数据服务,并通过 Web 浏览器提交 HTTP GET 请求来访问 OData 数据源:Next, you'll start the data service from Visual Studio and access the OData feed by submitting HTTP GET requests through a Web browser:

请参阅See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值