WCF service creation to publication (SqlServer version)

Before the start of this example, let's first look at what is wcf? Wcf What are the characteristics?

wcf layered architecture is a comprehensive service-oriented programming. Entry layer of the architecture for the service model layer.

Allowing users to build their own model of software products and external communications with minimal time and effort. It enables developers to build a cross-platform secure, reliable, transactional solutions. And can be compatible with the existing system of writing.

Brief summary is: a set of application development interface for data communication.

Well, wcf introduce so much, need more in-depth understanding of your own Baidu or after use will go deep. Let's start our example.

In this example, we will learn how to create a WCF service, how to test WCF services, how to publish wcf service to the IIS. How to call a WCF service at the client.

    This example tutorial binding EF6.0, and a connection of a Dome SqlServer, Well, officially explained below.

 

Step 1: Create "WCF Service Application"

 

 

Click OK, we will create a WCF project, two will automatically generate class files "IService.cs" and "Service.cs" for us in the solution. As shown below.

 

 

Step Two: Let's create a class library for storing solid models.

In "Solution window", we right-click the solution and select "Add", the new project, then click the "library" OK.

 

 

Insert a step: creating a service need to install the database connection, right-click the solution, select the "management solutions nuget package" following interface pops up, enter the "Browse" tab in the search box "sql" select the first installation, need to select the item "library (in this example calsslibray1)" and "service (in this example wcfservice4)" as shown below:

 

                                                                             Database linkage


Similarly, right-click the solution and select "nuget management solution package", following interface pops up, select install EntityFramework, check the class lists, the project only Classlibrary1 and WcfService3 two classes, to be checked.

 

 

Step 3: Create EF solid model.

为了方便管理,我们先创建个文件夹用来存放实体类。

然后右击文件夹→添加→新建项。如下图所示:↓

 

 

       然后我们左边菜单选择:数据→ADO.NET实体数据模型。如下图所示。↓

 

 

点击添加之后,我们会看到有几个选择, 分别代表:数据库优先、模型优先、代码优先。

本Dome选择来自数据库的CodeFist,也就是代表:代码优先。如下图所示。↓

 

 

点击下一步之后,再点击“新建连接”,然后测试连接是否成功,再选择数据库。如下图所示。

 

 

 

然后再勾选自己所需要生成的实体表,如下图所示。

 

 

插入: 这里若出现选择是否包含敏感字符时, 选择“是”, app.config和web.config 里面最后一行会出现密码,若选择否,则不出现密码。

然后你就会看到EntityModel这个文件夹下会多出两个类文件,由于我只选择了一个表生成的,所以是两个类文件。如下图所示。

 

 

然后我们点开“Model1.cs”,首先看到的是一个构造函数, 底下是我们所选表生成的一些代码,这些我们都不需要关心,都是自动生成的。这个构造函数后面的“name=Model1”就是对应的连接字符串。我们打开App.config便可知。然后把此连接字符串复制粘贴到wcf服务应用程序中的Web.config里面(必须有的一步)。 如下图所示。

 

 

第五步:定义契约,我们开始写服务。

       首先我们在“IService1”中定义锲约。注意:每个锲约上都要加上[OperationContract]标记。如下图所示。

 

 


       然后我们在“Service1.svc”中实现我们所有定义的契约。如下图所示。

 

 

到这一步,我们的服务已经完成了。现在我们开始本地测试服务是否有问题,如没问题再发布到IIS上去。

测试步骤:右击“Service1.svc” 选择在浏览器中查看,如下图所示,说明服务没问题。然后我们再用wcf测试客户端测试一下我们做的一些操作是否正确。

 

 

 

如上图所示,只能说明我们的服务写的没问题,但并不确定服务中所写的一些代码访问数据是否正确。那么我们就在服务端利用WCF测试客户端调用我们所写的代码是否正确。

 

 

打开服务端步骤:首先,我们将“Service1.svc”设为起始页,然后把整个服务应用程序设为启动项目,最后F5运行。如下图所示。

 

 


 如上图,我们测试Del,传入值再调用,如果没报错且左下角提示服务调用完成,说明已经成功调用并执行我们所写的代码功能。

原文:https://blog.csdn.net/zhoubangbang1/article/details/79082788

Guess you like

Origin www.cnblogs.com/edllixiaoyu/p/11119336.html