zoukankan      html  css  js  c++  java
  • (原创)如何将WCF服务发布到IIS中去VS2010版

    上一遍我转载了别人的的帖子,如何在VS2010中去创建WCF服务,这一遍我就详细的讲解下如何将WCF Service Library服务部署到IIS中去,供Web应用程序使用。


    附上Demo:WCF完整的一个Demo(WCF创建、部署、调用).rar 

    1. 创建好一个WCF Service Library(具体可以参考我的上一遍文章VS2010中如何创建一个WCF
    2. 创建一个Web Application(准备放到IIS中去的就是这个Application了)让我们一起来从头开始做一遍吧。
    第一步,我们在刚刚创建完的解决方案里,再添加一个WebApplication的项目取名叫做“WebServicePerson”
     

     

     第二步,给WebServicePerson这个项目添加一个.SVC的文件,取名叫做“PersonService”

     

    接下来呢,将创建项目时自带的文件夹和.aspx文件都删除掉(如下图)然后打开 PersonService.svc 文件,我们需要将文件修改如下。

    <%@ ServiceHost Language="C#" Debug="true" Service="WCFService_Library2010.PersonService" %>  

     Service="WCFService_Library2010.PersonService"是需要自己定义的,服务指向那个WCF的服务库

     

     第三步,非常重要绝对不能忽略,就是要将WCF服务引用到当前的项目中来。

     

     

    第四步,经过上面三步的操作项目基本已成成型了,接下来只需要最后一步了,那就是配置Web.Config. 自己手写去配置Web.config是非常痛苦的,所幸微软给我我们提供了非常便捷的配置功能“Edit WCF Configuration”

     

    我们接着往下看.....

    进入配置界面后,我们发现Services下面并没有任何节点,需要我们自己去新建一个,可以点击“Create a New Service...”去创建 

     

    去选择,我们刚刚创建的 WCF Service Library的服务。

     

     

    一路next往下。 

     

     

     

     

    创建完之后,我们会得到这样的一个界面。 

     

     这一步很重要的,因为之前我们已经添加了一个.svc的文件PersonService并且已经配置好了Service的内容,所以系统自动已经在Service Behaviors下面创建了一个PersonService的节点。

    此时我们只需要填写一个name就可以了(如下图 )

     

     回到Services 选中刚刚创建的name名称。

     

    最后,最关键的一步就是要记得“保存”。。不然我们刚做的所有配置都白费了。 

     

    最后去运行PersonService.svc,右击 → 在浏览器中查看,一切大功告成了。此时你就可以将这个WebAppliction部署到IIS中去了,供Web项目所使用。

    至于如何在项目里调用WebService,上图中其实已经有详细的说明了并且给出调用的代码了,我就不多说了。

     附上代码:

        <p> 

            <asp:TextBox ID="TextAge" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextName" runat="server"></asp:TextBox>
            <asp:Button ID="Button1"   runat="server" Text="新增" onclick="Button1_Click" />
        </p>
        <p>
            <asp:Label ID="LabInfo" runat="server" Text=""></asp:Label>
            <asp:Button ID="Button2"   runat="server" Text="获取" onclick="Button2_Click" 
                 
    />
        </p>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using WebPerson.PersonService;//这里是引用webservice


    namespace WebPerson
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                PersonServiceClient client = new PersonServiceClient();

                client.AddPerson(new Person() { Age=Convert.ToInt32(TextAge.Text), Name=TextName.Text });

                client.Close();

            }

            protected void Button2_Click(object sender, EventArgs e)
            {
                LabInfo.Text = "";

                PersonServiceClient client = new PersonServiceClient();
                Person [] personList = client.GetAllPersons();
                foreach (Person model in personList)
                {
                    LabInfo.Text += model.Age + "," + model.Id + "," + model.Name + "<br />";
                }
                 
            }


        }


  • 相关阅读:
    UVALive 7141 BombX
    CodeForces 722D Generating Sets
    CodeForces 722C Destroying Array
    CodeForces 721D Maxim and Array
    CodeForces 721C Journey
    CodeForces 415D Mashmokh and ACM
    CodeForces 718C Sasha and Array
    CodeForces 635C XOR Equation
    CodeForces 631D Messenger
    田忌赛马问题
  • 原文地址:https://www.cnblogs.com/zhangliangzlee/p/2661589.html
Copyright © 2011-2022 走看看