zoukankan      html  css  js  c++  java
  • WCFSVC文件的分离

    项目结构图如下:

    新建一个实现内容和接口的项目:

    接口内部如下:

    using DataModel;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace wcfService
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IUserBusiness”。
        [ServiceContract]
        public interface IUserBusiness
        {
            [OperationContract]
            string DoWork(string name);
    
            [OperationContract]
            List<Student> GetStudentInfo(string stuName);
        }
    }

    实现代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DataBll;
    using DataModel;
    using wcfService;
    
    namespace WCFServiceCode
    {
        class UserBusiness : IUserBusiness
        {
            public string DoWork(string name)
            {
                return string.Format("hello Word by {0}", name);
            }
    
            public List<Student> GetStudentInfo(string stuName)
            {
                SchoolBusinees bussiness = new SchoolBusinees();
                return bussiness.GetStudentInfo(stuName);
            }
        }
    }

    编写完成后,编译生成DLL文件,然后新建一个WCF的项目

    引用这个实现的项目DLL,之后新建一个SVC文件,删除XX.SVC.CS这个文件,打开.SVC文件,在里面的service属性指向实现DLL.类,codebehind的名字要和类名一致例如:

    <%@ ServiceHost Language="C#" Debug="true" Service="WCFServiceCode.UserBusiness" CodeBehind="UserBusiness" %>

    之后点击发布后放入IIS,直接浏览会出现找不到类型XX

    ,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

    这个错误至今没有找到真正的解决办法,只能把IIS的WCF项目转换为应用程序后,才可以不能报错:选中项目后右键就可以看到转换为应用程序选项

    ,之后在如下界面取到地址:

    http://desktop-an72kei/WcfTest/UserBusiness.svc?wsdl  
    在需要添加引用的项目中按照添加服务引用的方式输入地址之后会添加引用,然后引用服务引用的命名空间,就可以直接调用WCF的方法了:
    代码如下:
    using MvcDemo.Bussiness;
    
    namespace MvcDemo.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
            public ActionResult Index()
            {
                //ViewData["pagePartialIndex"] = "分页视图";
                ViewData["pageIndex"] = "主页视图";
                UserBusinessClient bu = new UserBusinessClient();
                var res = bu.GetStudentInfo("张三");
                return View("Default");
            }

    至于为什么要分离页面和后台呢,我找不到具体的原因,可能是因为可以更加安全的管理接口避免暴漏的安全隐患吧。

    有不足之处还希望大家指教

  • 相关阅读:
    python爬虫(十六) -IndexError: list index out of range
    python爬虫(十五) 豆瓣电影爬虫
    python爬虫(十四)
    python爬虫(十三) lxml模块
    python爬虫(十二) XPath语法
    python爬虫(十一) session
    python爬虫(十) requests使用代理ip
    python爬虫(九) requests库之post请求
    python爬虫(八) requests库之 get请求
    PXE+kickstart网络安装CentOS7.4系统及过程中各种报错
  • 原文地址:https://www.cnblogs.com/llcdbk/p/5613447.html
Copyright © 2011-2022 走看看