zoukankan      html  css  js  c++  java
  • 1.简单的WCF模型

    1.结构

    2.契约:

     

    using System.ServiceModel;
    using Rhythmk.Entity;
    namespace Rhythmk.Contracts
    {
    [ServiceContract(Namespace
    ="http://wwww.wangkun.com")]

    publicinterface ICalculate
    {
    [OperationContract(Name
    ="AddServices")]
    double Add(double x, double y);

    /*
    Name :则体现在客户端调用此方法时候 显示的真实方法名
    IsOneway:这意味着客户端仅仅是向服务端发送一个运算的请求
    并不会通过回复消息得到任何运算结果。

    */
    [OperationContract(Name
    ="AddStrServices")]
    string Add(string x, string y);


    }
    }

    3. 服务

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Rhythmk.Contracts;
    using Rhythmk.Entity;
    using System.ServiceModel;
    using System.ServiceModel.Activation;

    namespace Rhythmk.Services
    {
    ///<summary>
    /// 计算器
    ///</summary>
    ///
    /*
    通过
    ADO.NET 数据服务[.svc] 建立服务宿主的时候 需要引用
    *using System.ServiceModel.Activation;
    *同时在服务类前面需要配置
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
    */
    [AspNetCompatibilityRequirements(RequirementsMode
    =AspNetCompatibilityRequirementsMode.Required)]
    publicclass Calculate:ICalculate
    {
    publicdouble Add(double x, double y)
    {

    return x + y;
    }

    publicstring Add(string x, string y)
    {
    return"Result"+ x + y;
    }

    }
    }

    4.控制台进行寄宿

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.serviceModel>
    <behaviors>
    <serviceBehaviors>
    <behavior name="metaBehavior">
    <serviceMetadata httpGetEnabled="true"/>
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <services>
    <service name="Rhythmk.Services.Calculate" behaviorConfiguration="metaBehavior">
    <endpoint address=""
    binding
    ="basicHttpBinding" bindingConfiguration="" contract="Rhythmk.Contracts.ICalculate">
    </endpoint>
    <host>
    <baseAddresses>
    <add baseAddress="http://127.0.0.1:1234/Rhythmk.Services.Calculate"/>
    </baseAddresses>
    </host>
    </service>
    </services>
    </system.serviceModel>
    </configuration>

    代码:

    代码:
    using System;
    using System.ServiceModel;

    using Rhythmk.Services;

    namespace Rhythmk.ConsoleApp
    {
    class Program
    {
    staticvoid Main(string[] args)
    {
    CreateCalculateService();
    }
    staticvoid CreateCalculateService()
    {
    Console.WriteLine(
    "----------CreateCalculateService---Star---------");
    using (ServiceHost host =new ServiceHost(typeof(Calculate)))
    {
    host.Opened
    +=delegate { Console.WriteLine("CalculateService已经启动,按任意键终止服务!"); };
    host.Open();
    Console.Read();
    }

    }
    }
    }

    5.ADO.NET 数据服务[.svc] 建立服务宿主

    Rhythmk.WebHostMonitor\Calculate\Calculate.svc

    <%@ ServiceHost Language="C#"  Debug="true" Service="Rhythmk.Services.Calculate" %>

    6.客户端调用

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.serviceModel>
    <client>
    <endpoint address="http://wcf.wangkun.w/Calculate/Calculate.svc"
    binding
    ="basicHttpBinding" contract="Rhythmk.Contracts.ICalculate"
    name
    ="CalculateEndPoint">
    </endpoint>
    </client>
    </system.serviceModel>
    </configuration>

    代码:

    using Rhythmk.Contracts;
    using System.ServiceModel;
    using System.Web.Services;

    ChannelFactory
    <ICalculate> calculatorChannelFactory =new ChannelFactory<ICalculate>("CalculateEndPoint");
    ICalculate proxy
    = calculatorChannelFactory.CreateChannel();
    Utitl.Alert(proxy.Add(
    10, 20).ToString());
    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    HDU4731+找规律
    Unable to open c
    珠宝
    allegro添加多个过孔
    STM32硬件复位时间
    Android 系统的四层结构
    AIDL与stub
    devfs,proc,udev
    cdev[典]
    Linux设备管理之权限倾斜——mem、proc、devfs、sysfs、udev(下)
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2056610.html
Copyright © 2011-2022 走看看