WCF入门

来源:互联网 发布:高分复读 知乎 编辑:程序博客网 时间:2024/05/17 09:11

先创建一个 空白解决方方案 命名为:WCFApp

创建WCF服务

1:Visual c# -- Web -- WCF 服务应用程序 

 

删除 service1.svc 及 Iservice1.cs

2::添加新建项 -- Visual c# -- WCF 服务 -- 改 Service1.svc 为 Calculator.svc --确定

系统自动添加 Calculator.svc,Calculator.svc.cs ,ICalculator.cs  3个文件

修改 ICalculator.cs  代码:

namespace WcfService1
{
    // 注意: 如果更改此处的接口名称 "ICalculator",也必须更新 Web.config 中对 "ICalculator" 的引用。
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
}

 

修改 Calculator.svc.cs 代码:

 namespace WcfService1
{
    // 注意: 如果更改此处的类名 "Calculator",也必须更新 Web.config 中对 "Calculator" 的引用。
    //[ServiceBehavior(
    //AutomaticSessionShutdown = true,
    //ConcurrencyMode = ConcurrencyMode.Single,
    //InstanceContextMode = InstanceContextMode.PerSession,
    //IncludeExceptionDetailInFaults = false,
    //UseSynchronizationContext = true,
    //ValidateMustUnderstand = true)]

    public class Calculator : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }
}

3::添加新建项 -- Visual c# --代码 -- 代码文件-- 把整个名称改为 service.svc -- 确定

这样创建了一个空的文档

输入代码:

<%@ServiceHostlanguage="C#" Debug="true" Service="WcfService1.Calculator"%>

 

//注意去匹配 加粗 颜色一样的部分

4: 配置 Web.config

其他部分勿动 添加:

<system.serviceModel>
  <services>
   <service behaviorConfiguration="WcfService1.CalculatorBehavior" name="WcfService1.Calculator">
    <endpoint address="" binding="wsHttpBinding" contract="WcfService1.ICalculator" bindingConfiguration="B1">
     <identity>
      <dns value="localhost" />
     </identity>
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
   </service>
  </services>
  <behaviors>
   <serviceBehaviors>
    <behavior name="WcfService1.CalculatorBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  </behaviors>

 <!-- ++++++++++++++ -->
    <bindings>
      <wsHttpBinding>
        <binding name="B1">

         <!-- name=随意命名,但要与上面的bindingConfiguration="B1"对应 安全模式设置成None 是为了谁都能访问 -->
          <security mode="None"></security>
        </binding>
      </wsHttpBinding>
    </bindings>

 <!-- ++++++++++++++ -->
  </system.serviceModel>

到此 wcf 创建完成 

5:发布WCF到服务器空间 

 
输入网址:http://www.XXX.com/service.svc 具体根据你的服务器的地址 
记住后面一定要加 service.svc  
 
到此wcf服务发布成功

 

创建asp.net web 应用程序

1:Visual c# 

 
 

2:Default.aspx

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

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference1.CalculatorClient client = new ServiceReference1.CalculatorClient();
        double value1 = 100.00D;
        double value2 = 15.99D;
        double result = client.Add(value1, value2);
        //Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
        Label1.Text += "<br/>" + string.Format("Add({0},{1}) = {2}", value1, value2, result);


        // Call the Subtract service operation.
        value1 = 145.00D;
        value2 = 76.54D;
        result = client.Subtract(value1, value2);
        Label1.Text += "<br/>" + string.Format("Subtract({0},{1}) = {2}", value1, value2, result);

        // Call the Multiply service operation.
        value1 = 24.00D;
        value2 = 81.25D;
        result = client.Multiply(value1, value2);
        Label1.Text += "<br/>" + string.Format("Multiply({0},{1}) = {2}", value1, value2, result);

        // Call the Divide service operation.
        value1 = 22.00D;
        value2 = 7.00D;
        result = client.Divide(value1, value2);
        Label1.Text += "<br/>" + string.Format("Divide({0},{1}) = {2}", value1, value2, result);

        //Closing the client releases all communication resources.
        client.Close();
    }
}

0 0
原创粉丝点击