Use the external interface provided by WCF and the WCF Interface
This article uses WCF to provide external interfaces through webservices. At the same time, NUnit is used to perform unit tests on webservices methods.
Development contract
The Contract Project is a Class Library Project, which contains ServiceContract in WCF. This is an interface added with Attribute [ServiceContract. Attribute [OperationContract] must be added to the methods in the interface.
In addition, considering the stress test for the interface in the next article, Attribute [WebGet] is added to the method in the interface, and the method can be accessed through get.
The following defines the Contract-IuserInfo interface of UserInfo.
Using
System. ServiceModel;
System. ServiceModel. Web; // webGet
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. ServiceModel;
Using System. ServiceModel. Web; // webGet
Namespace Lee. Contract
{
[ServiceContract]
Public interface IUserInfo
{
[OperationContract]
[WebGet]
Bool AddUserInfo (string name, string description, string state );
[OperationContract]
[WebGet]
Bool ExistUserInfo (string name );
[OperationContract]
[WebGet]
Bool UpdateUserInfo (string name, string description, string state );
}
}
Development ServiceServices
The Services Project is also a class library project. This project mainly implements Contract and calls the data access layer method provided by DAL.
Using
Add a reference to the Lee. Model Project.
Add references to the Lee. DAL project.
Add a reference to the Lee. Contract Project.
The Bool value is returned in all three methods of UserInfo. If the method returns an object, you need to add a reference to the Lee. Model Project.
In addition, to pass objects in WCF, you must add Attribute [DataContract] and [Serializable] to the object class. Attribute [DataMember] must be added to the Attribute.
The UserInfo service class in Lee. Services is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using Lee. DAL;
Using Lee. Model;
Using Lee. Contract;
Namespace Lee. Services
{
Public class UserInfo: IUserInfo
{
/** // <Summary>
/// Add a user
/// </Summary>
/// <Param name = "name"> User name </param>
/// <Param name = "description"> User description </param>
/// <Param name = "state"> Status </param>
/// <Returns> True-operation succeeded | False-operation failed </returns>
Public bool AddUserInfo (string name, string description, string state)
{
UserInfoDAL dal = new UserInfoDAL ();
Return dal. AddUserInfo (name, description, state );
}
/** // <Summary>
/// Check whether the user exists
/// </Summary>
/// <Param name = "name"> User name </param>
/// <Returns> True-user exists | False-user does not exist </returns>
Public bool ExistUserInfo (string name)
{
UserInfoDAL dal = new UserInfoDAL ();
Return dal. ExistUserInfo (name );
}
/** // <Summary>
/// Update user information
/// </Summary>
/// <Param name = "name"> User name </param>
/// <Param name = "description"> User description </param>
/// <Param name = "state"> Status </param>
/// <Returns> True-operation succeeded | False-operation failed </returns>
Public bool UpdateUserInfo (string name, string description, string state)
{
UserInfoDAL dal = new UserInfoDAL ();
Return dal. UpdateUserInfo (name, description, state );
}
}
}
Development host Hosting
The Hosting project is a WCF Service application. This project automatically adds references to System. Runtime. Serialization and System. ServiceModel.
Using
Add a reference to the Lee. Contract Project.
Add a reference to the Lee. Services Project.
Detailed steps
1) Add UserInfo. svc;
2) Delete the UserInfo. svc. cs file;
3) double-click to open UserInfo. svc.
<% @ ServiceHost Language = "C #" Debug = "true" Service = "Lee. Hosting. UserInfo" CodeBehind = "UserInfo. svc. cs" %>
To:
<% @ ServiceHost Language = "C #" Debug = "true" Service = "Lee. Services. UserInfo" CodeBehind = "Lee. Services. UserInfo. cs" %>
4) modify Web. config;
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConnectionStrings>
<Add name = "SQLConnection" connectionString = "Database = XX; User ID = sa; Password = saas; Server = XX;" providerName = "System. Data. SqlClient"/>
</ConnectionStrings>
<System. serviceModel>
<ServiceHostingEnvironment aspNetCompatibilityEnabled = "false"/>
<Services>
<Service behaviorConfiguration = "Lee. Hosting. UserInfoBehavior" name = "Lee. Services. UserInfo">
<Endpoint address = "" binding = "basicHttpBinding" contract = "Lee. Contract. IUserInfo">
<Identity>
<Dns value = "localhost"/>
</Identity>
</Endpoint>
<Endpoint address = "webhttp" behaviorConfiguration = "webHttp" binding = "webHttpBinding" contract = "Lee. Contract. IUserInfo">
<Identity>
<Dns value = "localhost"/>
</Identity>
</Endpoint>
</Service>
</Services>
<Behaviors>
<EndpointBehaviors>
<Behavior name = "webHttp">
<WebHttp/>
</Behavior>
</EndpointBehaviors>
<ServiceBehaviors>
<Behavior name = "Lee. Hosting. UserInfoBehavior">
<ServiceMetadata httpGetEnabled = "true"/>
<ServiceDebug includeExceptionDetailInFaults = "true"/>
</Behavior>
</ServiceBehaviors>
</Behaviors>
</System. serviceModel>
<System. web>
<Compilation debug = "true"/>
</System. web>
</Configuration>
5) Create the NHibernate configuration file hibernate. cfg. xml and set itAlways copy,Add a reference to nhib.pdf and nhib.pdf. ByteCode. Castle.
6) view results
Browse UserInfo. svc
Corresponding WSDL
View Schema format
So far, we have successfully released an external interface using WCF. Next we will perform a unit test on Webservices!
Unit Test
The unit test settings have been discussed in the previous article.
Test procedure
1) using
Add a reference to the Lee. Contract Project.
2) Add a service reference and click "Discover" to find the Service under the solution.
After successful addition, the client EndPoint is automatically created in App. Config.
3) create a service test class TestUserInfoSVC. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using Lee. Model;
Using Lee. DAL;
Using NUnit. Framework;
Namespace Lee. Test
{
[TestFixture]
Public class TestUserInfoSVC
{
[Test]
Public void AddUserInfo ()
{
UserInfoSVC. UserInfoClient client = new Lee. Test. UserInfoSVC. UserInfoClient ();
Bool result = client. AddUserInfo ("testname6", "testdesc", "teststate ");
Assert. AreEqual (true, result );
}
[Test]
Public void ExistUserInfo ()
{
UserInfoSVC. UserInfoClient client = new Lee. Test. UserInfoSVC. UserInfoClient ();
Bool result = client. ExistUserInfo ("testname ");
Assert. AreEqual (true, result );
}
[Test]
Public void UpdateUserInfo ()
{
UserInfoSVC. UserInfoClient client = new Lee. Test. UserInfoSVC. UserInfoClient ();
Bool result = client. UpdateUserInfo ("testname5", "hello, testname! "," Activation ");
Assert. AreEqual (true, result );
}
}
}
4) you can set a breakpoint for One-Step debugging in the method.