星空网 > 软件开发 > ASP.net

使用WCF对外提供接口

本篇将通过WCF以webservices的方式对外提供接口。同时使用NUnit对webservices中的方法进行单元测试。

开发契约 contract

Contract项目为类库项目,该项目下会包含WCF中的ServiceContract,这是一些被加上Attribute [ServiceContract]的接口。同时接口中的方法也需要加上Attribute [OperationContract]。
另,考虑到下一篇要对接口进行压力测试,所以接口中的方法也加上Attribute [WebGet],可以通过get方式访问方法。

下面就开始定义UserInfo的Contract—IuserInfo接口。

using

    System.ServiceModel;

    System.ServiceModel.Web;//webGet

使用WCF对外提供接口
使用WCF对外提供接口using System;
使用WCF对外提供接口using System.Collections.Generic;
使用WCF对外提供接口using System.Linq;
使用WCF对外提供接口using System.Text;
使用WCF对外提供接口
使用WCF对外提供接口using System.ServiceModel;
使用WCF对外提供接口using System.ServiceModel.Web;//webGet
使用WCF对外提供接口
使用WCF对外提供接口namespace Lee.Contract
使用WCF对外提供接口使用WCF对外提供接口使用WCF对外提供接口{
使用WCF对外提供接口    [ServiceContract]
使用WCF对外提供接口    public interface IUserInfo
使用WCF对外提供接口使用WCF对外提供接口    使用WCF对外提供接口{
使用WCF对外提供接口        [OperationContract]
使用WCF对外提供接口        [WebGet]
使用WCF对外提供接口        bool AddUserInfo(string name, string description, string state);
使用WCF对外提供接口        [OperationContract]
使用WCF对外提供接口        [WebGet]
使用WCF对外提供接口        bool ExistUserInfo(string name);
使用WCF对外提供接口        [OperationContract]
使用WCF对外提供接口        [WebGet]
使用WCF对外提供接口        bool UpdateUserInfo(string name, string description, string state);
使用WCF对外提供接口    }
使用WCF对外提供接口}
使用WCF对外提供接口使用WCF对外提供接口

 

开发服务  Services

Services项目也是类库项目,该项目主要是对Contract的具体实现,同时会调用DAL提供的数据访问层方法。

using

    添加对Lee.Model项目的引用。

    添加对Lee.DAL项目的引用。

    添加对Lee. Contract项目的引用。

我们实现的UserInfo的三个方法中都是返回了Bool值,如果方法返回对象,这时就需要添加对Lee.Model项目的引用。
另,如果要在WCF中传递对象,需要为实体类添加Attribute [DataContract]和[Serializable]。属性需要添加Attribute [DataMember]。

下面是Lee.Services中的UserInfo 服务类

使用WCF对外提供接口
使用WCF对外提供接口using System;
使用WCF对外提供接口using System.Collections.Generic;
使用WCF对外提供接口using System.Linq;
使用WCF对外提供接口using System.Text;
使用WCF对外提供接口
使用WCF对外提供接口using Lee.DAL;
使用WCF对外提供接口using Lee.Model;
使用WCF对外提供接口using Lee.Contract;
使用WCF对外提供接口
使用WCF对外提供接口namespace Lee.Services
使用WCF对外提供接口使用WCF对外提供接口使用WCF对外提供接口{
使用WCF对外提供接口    public class UserInfo:IUserInfo
使用WCF对外提供接口使用WCF对外提供接口    使用WCF对外提供接口{
使用WCF对外提供接口使用WCF对外提供接口        /**//// <summary>
使用WCF对外提供接口        /// 添加用户
使用WCF对外提供接口        /// </summary>
使用WCF对外提供接口        /// <param name="name">用户名称</param>
使用WCF对外提供接口        /// <param name="description">用户描述</param>
使用WCF对外提供接口        /// <param name="state">状态</param>
使用WCF对外提供接口        /// <returns>True-操作成功|False-操作失败</returns>
使用WCF对外提供接口        public bool AddUserInfo(string name, string description, string state)
使用WCF对外提供接口使用WCF对外提供接口        使用WCF对外提供接口{
使用WCF对外提供接口            UserInfoDAL dal =new UserInfoDAL();
使用WCF对外提供接口            return dal.AddUserInfo(name,description,state);
使用WCF对外提供接口        }
使用WCF对外提供接口使用WCF对外提供接口        /**//// <summary>
使用WCF对外提供接口        /// 检查用户是否存在
使用WCF对外提供接口        /// </summary>
使用WCF对外提供接口        /// <param name="name">用户名称</param>
使用WCF对外提供接口        /// <returns>True-用户存在|False-用户不存在</returns>
使用WCF对外提供接口        public bool ExistUserInfo(string name)
使用WCF对外提供接口使用WCF对外提供接口        使用WCF对外提供接口{
使用WCF对外提供接口            UserInfoDAL dal =new UserInfoDAL();
使用WCF对外提供接口            return dal.ExistUserInfo(name);
使用WCF对外提供接口        }
使用WCF对外提供接口使用WCF对外提供接口        /**//// <summary>
使用WCF对外提供接口        /// 更新用户信息
使用WCF对外提供接口        /// </summary>
使用WCF对外提供接口        /// <param name="name">用户名称</param>
使用WCF对外提供接口        /// <param name="description">用户描述</param>
使用WCF对外提供接口        /// <param name="state">状态</param>
使用WCF对外提供接口        /// <returns>True-操作成功|False-操作失败</returns>
使用WCF对外提供接口        public bool UpdateUserInfo(string name, string description, string state)
使用WCF对外提供接口使用WCF对外提供接口        使用WCF对外提供接口{
使用WCF对外提供接口            UserInfoDAL dal = new UserInfoDAL();
使用WCF对外提供接口            return dal.UpdateUserInfo(name, description, state);
使用WCF对外提供接口        }
使用WCF对外提供接口    }
使用WCF对外提供接口}
使用WCF对外提供接口使用WCF对外提供接口

 

开发宿主 Hosting

   Hosting项目为WCF服务应用程序,该项目会自动添加对System.Runtime.Serialization和System.ServiceModel的引用。

     using

        添加对Lee. Contract项目的引用。

        添加对Lee. Services项目的引用。

    详细步骤

    1)添加 UserInfo.svc;

    2)删除文件 UserInfo.svc.cs;

    3)双击打开 UserInfo.svc

        <%@ ServiceHost Language="C#" Debug="true" Service="Lee.Hosting.UserInfo" CodeBehind="UserInfo.svc.cs" %>

        修改为:

       <%@ ServiceHost Language="C#" Debug="true" Service="Lee.Services.UserInfo" CodeBehind="Lee.Services.UserInfo.cs" %>

    4)修改Web.config;

      

使用WCF对外提供接口
使用WCF对外提供接口<?使用WCF对外提供接口<configuration>
使用WCF对外提供接口  <connectionStrings>
使用WCF对外提供接口    <add name="SQLConnection" connectionString="Database=XX;User ID=sa;Password=saas;Server=XX;" providerName="System.Data.SqlClient"/>
使用WCF对外提供接口  </connectionStrings>
使用WCF对外提供接口  <system.serviceModel>
使用WCF对外提供接口    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
使用WCF对外提供接口    <services>
使用WCF对外提供接口      <service behaviorConfiguration="Lee.Hosting.UserInfoBehavior" name="Lee.Services.UserInfo">
使用WCF对外提供接口        <endpoint address="" binding="basicHttpBinding" contract="Lee.Contract.IUserInfo">
使用WCF对外提供接口          <identity>
使用WCF对外提供接口            <dns value="localhost" />
使用WCF对外提供接口          </identity>
使用WCF对外提供接口        </endpoint>
使用WCF对外提供接口        <endpoint address="webhttp" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="Lee.Contract.IUserInfo">
使用WCF对外提供接口          <identity>
使用WCF对外提供接口            <dns value="localhost" />
使用WCF对外提供接口          </identity>
使用WCF对外提供接口        </endpoint>
使用WCF对外提供接口      </service>
使用WCF对外提供接口    </services>
使用WCF对外提供接口    <behaviors>
使用WCF对外提供接口      <endpointBehaviors>
使用WCF对外提供接口        <behavior name="webHttp">
使用WCF对外提供接口          <webHttp />
使用WCF对外提供接口        </behavior>
使用WCF对外提供接口      </endpointBehaviors>
使用WCF对外提供接口      <serviceBehaviors>
使用WCF对外提供接口        <behavior name="Lee.Hosting.UserInfoBehavior">
使用WCF对外提供接口          <serviceMetadata httpGetEnabled="true" />
使用WCF对外提供接口          <serviceDebug includeExceptionDetailInFaults="true" />
使用WCF对外提供接口        </behavior>
使用WCF对外提供接口      </serviceBehaviors>
使用WCF对外提供接口    </behaviors>
使用WCF对外提供接口  </system.serviceModel>
使用WCF对外提供接口  <system.web>
使用WCF对外提供接口    <compilation debug="true"/>
使用WCF对外提供接口  </system.web>
使用WCF对外提供接口</configuration>
使用WCF对外提供接口
使用WCF对外提供接口
使用WCF对外提供接口
使用WCF对外提供接口

    5)创建NHibernate配置文件hibernate.cfg.始终复制,添加对NHibernate和NHibernate.ByteCode.Castle的引用。

    6)效果查看

        浏览UserInfo.svc

       使用WCF对外提供接口

        对应的WSDL

        使用WCF对外提供接口

        查看Schema格式

使用WCF对外提供接口

    到现在为止,我们已经用WCF成功的对外发布了接口。下面我们对Webservices进行单元测试!

单元测试

    单元测试的相关设置在上一篇已经讲过了,这里不再介绍。

  测试步骤

    1)using

        添加对Lee. Contract项目的引用。

    2)添加服务引用,直接点“发现“,可以找到该解决方案下的服务。

    使用WCF对外提供接口

  成功添加后,会自动在App.Config中创建client端EndPoint。

    3)创建服务测试类TestUserInfoSVC.cs

   

使用WCF对外提供接口
使用WCF对外提供接口using System;
使用WCF对外提供接口using System.Collections.Generic;
使用WCF对外提供接口using System.Linq;
使用WCF对外提供接口using System.Text;
使用WCF对外提供接口
使用WCF对外提供接口using Lee.Model;
使用WCF对外提供接口using Lee.DAL;
使用WCF对外提供接口using NUnit.Framework;
使用WCF对外提供接口
使用WCF对外提供接口namespace Lee.Test
使用WCF对外提供接口使用WCF对外提供接口使用WCF对外提供接口{
使用WCF对外提供接口    [TestFixture]
使用WCF对外提供接口    public class TestUserInfoSVC
使用WCF对外提供接口使用WCF对外提供接口    使用WCF对外提供接口{
使用WCF对外提供接口        [Test]
使用WCF对外提供接口        public void AddUserInfo()
使用WCF对外提供接口使用WCF对外提供接口        使用WCF对外提供接口{
使用WCF对外提供接口            UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
使用WCF对外提供接口            bool result = client.AddUserInfo("testname6", "testdesc", "teststate");
使用WCF对外提供接口            Assert.AreEqual(true, result);
使用WCF对外提供接口        }
使用WCF对外提供接口        [Test]
使用WCF对外提供接口        public void ExistUserInfo()
使用WCF对外提供接口使用WCF对外提供接口        使用WCF对外提供接口{
使用WCF对外提供接口            UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
使用WCF对外提供接口            bool result = client.ExistUserInfo("testname");
使用WCF对外提供接口            Assert.AreEqual(true, result);
使用WCF对外提供接口        }
使用WCF对外提供接口        [Test]
使用WCF对外提供接口        public void UpdateUserInfo()
使用WCF对外提供接口使用WCF对外提供接口        使用WCF对外提供接口{
使用WCF对外提供接口            UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
使用WCF对外提供接口            bool result = client.UpdateUserInfo("testname5", "hello,testname!", "activation");
使用WCF对外提供接口            Assert.AreEqual(true, result);
使用WCF对外提供接口        }
使用WCF对外提供接口    }
使用WCF对外提供接口}
使用WCF对外提供接口使用WCF对外提供接口

    4)可以在方法中设置断点单步调试。




原标题:使用WCF对外提供接口

关键词:wcf

wcf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

中国电商海外突围!TikTok亿级流量强势入局:https://www.kjdsnews.com/a/1696240.html
如何申请入驻亚马逊VC账号?:https://www.kjdsnews.com/a/1696241.html
一年卖出9亿!这类小众产品杀入红海,虽好卖但也暗藏侵权风险!:https://www.kjdsnews.com/a/1696242.html
机器人公司GreyOrange在D轮融资1.35亿美元:https://www.kjdsnews.com/a/1696243.html
被标记“商品真实性”违规,这样处理申诉无忧!:https://www.kjdsnews.com/a/1696244.html
亚马逊美国站"合作承运商计划"全新升级!助力卖家提升运输服务品质:https://www.kjdsnews.com/a/1696245.html
大批Listing被下架,“黄色警告”!提示存在停用风险:https://www.kjdsnews.com/a/1836647.html
跨境支付百科——巴西支付篇:https://www.kjdsnews.com/a/1836648.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流